0

In spring, org.springframework.amqp.rabbit package has made easier integration of RabbitMQ. To move to ActiveMQ, I couldn't figure to directly support ActiveMQ in spring axion framework, but only with JMS as an interface.

Now in sender side, I explicitly call Sender in @EventSourcingHandler by which I am able to receive events in Receiver.

The only thing I can't figure out is, how to call axon's @EventHandler from Receiver whenever there is a new event.

Receiver Class:

public class Receiver {

    @JmsListener(destination = "myEventQueue")
    public void receive(AvailableQuantityInitializedEvent event) {
        // Here I need to trigger some axion's EventHandler
    }
}

Sender Class:

public class Sender {

    @Autowired
    private JmsTemplate jmsTemplate;

    public void send(AvailableQuantityInitializedEvent event) {
        jmsTemplate.convertAndSend("myEventQueue", event);
    }
}

EventSourcingHandler, where the sender sends the event.

@NoArgsConstructor
@Aggregate
@Data
@ComponentScan
public class SomeAggregate {

    @EventSourcingHandler
    protected void on(SomeEvent event, Sender sender) {
        this.x = event.xx();
        this.y = event.yy();
        sender.send(event);
    }
}

@EventHandler which I need to trigger from Receiver:

@ProcessingGroup(value = "name")
@AllArgsConstructor
@Component
public class SomeProjection {

    @EventHandler
    public void on(SomeEvent evt, @Timestamp Instant timestamp) {
        System.out.println("EventHandler:SomeEvent");
    }

}
Mukarram Ali
  • 387
  • 5
  • 24

1 Answers1

1

Firstly, I'd like to suggest not to send new events from an @EventSourcingHandler annotated methods. Such a method is meant to recreate the state of an Aggregate - thus when you're sourcing that Aggregate from all the events it has published.

What you are now effectively doing is sending message every time you are event sourcing your Aggregate. You've thus introduced side effects which are unwanted behavior.

Secondly, I'd like to understand what you're trying to achieve in the first place. If you're trying to publish the Events from your Axon application on a queue, it would be better suited to introduce a component which will perform that for any event, instead of the fine grained approach per Event Handler. I'd suggest you take a look at how the Spring AMQP extension or the Kafka extension provided by Axon work.

If all of this is still in the same application however, I'd suggest to simply use the EventStore/EventBus set up provided by the framework itself. Basing your application on messaging should allow you to separate the SomeProjection class at a latter stage as well without to much hassle.

Lastly I'd like to note that Axon Server provides a terrific means to distribute any message, albeit a Command, Event or Query, between your applications with ease.

Hope this helps!

Steven
  • 6,936
  • 1
  • 16
  • 31
  • Thank you for referring to the side effect. I agree with you, it is just a workaround, which might bite later. But Spring AMQP extension has given support for only RabbitMQ. Now to replicate the same logic in ActiveMQ, I am getting a hard time. – Mukarram Ali May 08 '19 at 18:30
  • I tried using RabbitMQ client and just change the source as ActiveMQ. But ActiveMQ supports AMQP 1.0, but spring amqp package has 0.9 version. – Mukarram Ali May 08 '19 at 19:51
  • 1
    That's correct what you've noticed there Mukarram. I also didn't intent for you to use the Spring AMQP extension instead, I was more so referring to it to show you what the best solution would be to implement this under ActiveMQ if you'd be either looking for a purpose built solution. Agreed, ideally there would be another extension for ActiveMQ support. I believe there already is an issue filed to update the extension to cope with AMQP 1.0. Otherwise, I suggest you create one under the extension's repository. – Steven May 09 '19 at 14:44