When The Context Closes

To invoke a method when the context is closed, use the javax.annotation.PreDestroy annotation:

  1. import jakarta.annotation.PreDestroy; (1)
  2. import jakarta.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 jakarta.annotation.PreDestroy (1)
  2. import jakarta.inject.Singleton
  3. import java.util.concurrent.atomic.AtomicBoolean
  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 jakarta.annotation.PreDestroy (1)
  2. import jakarta.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 tells Micronaut which method to invoke.

  1. import io.micronaut.context.annotation.Bean;
  2. import io.micronaut.context.annotation.Factory;
  3. import jakarta.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 jakarta.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 jakarta.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
Simply implementing the Closeable or AutoCloseable interface is not enough for a bean to be closed with the context. One of the above methods must be used.