Mukarram Ali
Mukarram Ali

Reputation: 427

Trigger Axon's @EventHandler from JmsListener

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");
    }

}

Upvotes: 0

Views: 342

Answers (1)

Steven
Steven

Reputation: 7275

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!

Upvotes: 1

Related Questions