[Spring Boot] AsynchronousCommandBus.shutdown()

109 views
Skip to first unread message

Troy Hart

unread,
Dec 19, 2017, 7:07:29 PM12/19/17
to Axon Framework Users
In the axon "Command Dispatching" documentation (https://docs.axonframework.org/v/3.1/part3/command-dispatching.html#asynchronouscommandbus) there is some advice to shut down the bus when stopping the application. I came up with a solution, but it seems a little contrived and was hoping someone could steer me in the right direction. 

Below is a snippet from my axon configuration class. I have defined a custom extension of AsynchronousCommandBus with a @PreDestroy annotated method which I configure as the command bus:

-----------------------------
  public static final class CustomAsynchronousCommandBus extends AsynchronousCommandBus {
    @PreDestroy
    @Override
    public void shutdown() {
      super.shutdown();
    }
  }

  @Bean
  public BeanValidationInterceptor<Message<?>> beanValidationInteceptor() {
    return new BeanValidationInterceptor<>();
  }

  @Bean
  public AsynchronousCommandBus commandBus() {
    AsynchronousCommandBus cb = new CustomAsynchronousCommandBus();
    cb.registerDispatchInterceptor(beanValidationInteceptor());
    return cb;
  }
-----------------------------

Allard Buijze

unread,
Dec 21, 2017, 9:53:21 AM12/21/17
to axonfr...@googlegroups.com
Hi,

one the @Bean annotation, you can also specify a destroyMethod. No need to override it, just mention @Bean(destroyMethod="shutdown") on top of the commandBus() method.

However, according to the documentation on @Bean, methods named "close" and "shutdown" are invoked automatically when detected on a bean:
From the Javadoc:
As a convenience to the user, the container will attempt to infer a destroy method against an object returned from the @Bean method. For example, given an @Bean method returning an Apache Commons DBCP BasicDataSource, the container will notice the close() method available on that object and automatically register it as the destroyMethod. This 'destroy method inference' is currently limited to detecting only public, no-arg methods named 'close' or 'shutdown'. The method may be declared at any level of the inheritance hierarchy and will be detected regardless of the return type of the @Bean method (i.e., detection occurs reflectively against the bean instance itself at creation time).

Cheers,

Allard

Op wo 20 dec. 2017 om 01:07 schreef Troy Hart <troy...@gmail.com>:
--
You received this message because you are subscribed to the Google Groups "Axon Framework Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to axonframewor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Reply all
Reply to author
Forward
0 new messages