0

I'm trying to create a simple microservices project to learn working with the Axon Framework.

I've set up messaging through RabbitMQ with the following code:

@Bean
public Exchange exchange() {
    return ExchangeBuilder.fanoutExchange("Exchange").build();
}

@Bean
public Queue queue() {
    return QueueBuilder.durable("QueueA").build();
}

@Bean
public Binding binding() {
    return BindingBuilder.bind(queue()).to(exchange()).with("*").noargs();
}

@Autowired
public void configure(AmqpAdmin admin) {
    admin.declareExchange(exchange());
    admin.declareQueue(queue());
    admin.declareBinding(binding());
}

And the folling in my application.properties:

axon.amqp.exchange=Exchange

With this configuration all events published through the Axon Framework will be sent to QueueA. But now I want to make all EventA events go to QueueA and all EventB events go to QueueB. How can I do that?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Daantie
  • 951
  • 10
  • 13

2 Answers2

3

By default, Axon Framework uses the package name of the event as the AMQP Routing Key. This means you can bind queues to topic exchanges using patterns to match against these routing keys. See https://www.rabbitmq.com/tutorials/tutorial-five-java.html for more information.

You can customize Axon's behavior, by providing a custom RoutingKeyResolver (a simple Function that returns a String for a given EventMessage). This is then configured in the AMQPMessageConverter, which is responsible for creating an AMQP Message based on an Axon EventMessage (and vice versa). You can use the DefaultAMQPMessageConverter if you're fine with the default AMQP Message format.

Allard
  • 2,640
  • 13
  • 13
  • Thanks for pointing me in the right direction. Providing a `RoutingKeyResolver` works like a charm. I can get the event information from the `EventMessage` payload and use this to get routing keys which suits my needs. When all separate events go to separate queues it's also possible to use a direct exchange instead of a topic exhange. – Daantie Jun 14 '18 at 08:56
0

however you are using fanoutExchange so it will put events to all Queue you have to just create another Queue and bind with sameExchange and Query-side you can handle event

Anant
  • 1
  • 2
  • Yes, but that is not the way I would like it to work. Allard's answer gave me exactly what I needed. – Daantie Jun 14 '18 at 09:01