How to config multiple types of CommandBus ?


Each command bus has it’s pros and cons, so I want to use different command bus according to different command message.

I’m using Spring boot 2.2.4 + Axon 4.2.1.

I tried to add two @Bean annotated command bus in Spring config class(one is SimpleCommandBus ,another one AsynchronousCommandBus), but Axon raises exception because Axon expect one command bus but found two.

So , how can I use multiple command bus and customize the route policy for the command buses ?


Hi Sean,

Axon was built in such a way that components would only be aware of the command bus. That doesn’t mean you’re stuck to a single implementation, though.
You could build a CommandBus implementation that delegates to more than one bus, depending on the name of the command. By default, the command name is the fully qualified classname of the class that represents the command. When dispatching, a similar thing happens: you forward the command to the “underlying” instance that is supposed to handle it.

When defining multiple instances like this (you’ll have 3 in total), you can annotate the forwarding commandbus with @Primary. That will have Axon (Spring, to be exact) treat that as the instance to use, when no specific instance is requested (using qualifiers, for example).

Hope this makes sense.

Hi Allard,

Yes, that’s really make sense, thank you for your helping all the time.