When The Context Closes

If you wish for a particular method to be invoked when the context is closed then you can use the javax.annotation.PreDestroy annotation:

  1. import javax.annotation.PreDestroy; (1)
  2. import javax.inject.Singleton;
  3. import java.util.concurrent.atomic.AtomicBoolean;
  4. @Singleton
  5. public class PreDestroyBean implements AutoCloseable {
  6. AtomicBoolean stopped = new AtomicBoolean(false);
  7. @PreDestroy (2)
  8. @Override
  9. public void close() throws Exception {
  10. stopped.compareAndSet(false, true);
  11. }
  12. }
  1. import javax.annotation.PreDestroy (1)
  2. import java.util.concurrent.atomic.AtomicBoolean
  3. import javax.inject.Singleton
  4. @Singleton
  5. class PreDestroyBean implements AutoCloseable {
  6. AtomicBoolean stopped = new AtomicBoolean(false)
  7. @PreDestroy (2)
  8. @Override
  9. void close() throws Exception {
  10. stopped.compareAndSet(false, true)
  11. }
  12. }
  1. import javax.annotation.PreDestroy
  2. import javax.inject.Singleton
  3. import java.util.concurrent.atomic.AtomicBoolean
  4. @Singleton
  5. class PreDestroyBean : AutoCloseable {
  6. internal var stopped = AtomicBoolean(false)
  7. @PreDestroy (2)
  8. @Throws(Exception::class)
  9. override fun close() {
  10. stopped.compareAndSet(false, true)
  11. }
  12. }
1The PreDestroy annotation is imported
2A method is annotated with @PreDestroy and will be invoked when the context is closed.

For factory beans, the preDestroy value in the Bean annotation can be used to tell Micronaut which method to invoke.

  1. import io.micronaut.context.annotation.Bean;
  2. import io.micronaut.context.annotation.Factory;
  3. import javax.inject.Singleton;
  4. @Factory
  5. public class ConnectionFactory {
  6. @Bean(preDestroy = "stop") (1)
  7. @Singleton
  8. public Connection connection() {
  9. return new Connection();
  10. }
  11. }
  1. import io.micronaut.context.annotation.Bean
  2. import io.micronaut.context.annotation.Factory
  3. import javax.inject.Singleton
  4. @Factory
  5. class ConnectionFactory {
  6. @Bean(preDestroy = "stop") (1)
  7. @Singleton
  8. Connection connection() {
  9. new Connection()
  10. }
  11. }
  1. import io.micronaut.context.annotation.Bean
  2. import io.micronaut.context.annotation.Factory
  3. import javax.inject.Singleton
  4. @Factory
  5. class ConnectionFactory {
  6. @Bean(preDestroy = "stop") (1)
  7. @Singleton
  8. fun connection(): Connection {
  9. return Connection()
  10. }
  11. }

  1. import java.util.concurrent.atomic.AtomicBoolean;
  2. public class Connection {
  3. AtomicBoolean stopped = new AtomicBoolean(false);
  4. public void stop() { (2)
  5. stopped.compareAndSet(false, true);
  6. }
  7. }
  1. import java.util.concurrent.atomic.AtomicBoolean
  2. class Connection {
  3. AtomicBoolean stopped = new AtomicBoolean(false)
  4. void stop() { (2)
  5. stopped.compareAndSet(false, true)
  6. }
  7. }
  1. import java.util.concurrent.atomic.AtomicBoolean
  2. class Connection {
  3. internal var stopped = AtomicBoolean(false)
  4. fun stop() { (2)
  5. stopped.compareAndSet(false, true)
  6. }
  7. }
1The preDestroy value is set on the annotation
2The annotation value matches the method name
Just simply implementing the Closeable or AutoCloseable interfaces is not enough to get a bean to close with the context. One of the above methods must be used.