How to use Axon with an EventStore for event sourcing and an EventBus for Hibernate repository persistence?

1,732 views
Skip to first unread message

Bruno Mendonça

unread,
Apr 20, 2017, 9:54:06 PM4/20/17
to Axon Framework Users
Hello all,


I have extended https://github.com/Qyotta/axon-eventstore (Greg Young's EventStore Java client with Axon wrapper) to support Axon 3.0.4 and included as a reference in a spring boot POC I am creating.

In a configuration file I added:

// EventStore extends EventBus 
@Bean
public EventStore eventStore() {
final EsEventStore esEventStore = new EsEventStore(new EventStoreClient(new EsContextDefaultImpl(EventStoreSettings.withDefaults()
.host("http://127.0.0.1:2113")
.build())));

return esEventStore;
}

Since I'm using JPA to persist the data to a postgresSQL database I got an error saying I needed an EventBus (assuming axon spring boot starter configuration detected this), so I added to the same config file:

// Since EventStore extends EventBus, now we have 2 buses
@Bean(name = "eventBus")
public EventBus eventBus() {
return new SimpleEventBus();
}

The code now compiles and runs fine and I can submit requests to the API and see the events being generated in the Event Store!

The problem is that I can't get the data to be persisted to the SQL database... I have an event listener class like this: 

@Component
public class MyEntityEventListener {
private MyEntityRepository repository;

@Autowired
public MyEntityEventListener(MyEntityRepository repository) {
this.repository = repository;
}

@EventHandler
public void on(MyEntityCreatedEvent event) {
// THIS CODE IS NEVER EXECUTED :( :( :(
repository.save(...);
}
}

The event handler is never fired :(

I commented the configuration file, so that I only use the postgresSQL db for both event souring and data persistence and it get's fired... (assuming that's because there is only one EventBus in this scenario).

Any insights on how get the handler to fire when there are 2 EventBuses?

Regards,
Bruno

Bruno Mendonça

unread,
Apr 21, 2017, 10:16:12 AM4/21/17
to Axon Framework Users
I tried removing the SimpleEventBus bean and change my EventStore bean to:

@Bean
public EventStore eventBus() {

final EsEventStore esEventStore = new EsEventStore(new EventStoreClient(new EsContextDefaultImpl(EventStoreSettings.withDefaults()
.host("http://127.0.0.1:2113")
.build())));

return esEventStore;
}

I no longer get the error of a missing EventBus, but I still don't get the event handler to fire :(

Allard Buijze

unread,
Apr 21, 2017, 4:36:32 PM4/21/17
to Axon Framework Users
Can you confirm the handlers are properly subscribed to your EventStore (actually, a processor will subscribe on behalf of the handlers)?
--
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.

Bruno Mendonça

unread,
Apr 23, 2017, 8:04:10 PM4/23/17
to Axon Framework Users
I actually have been thinking about that exactly... I was assuming Axon would magically take care or that, but now I'm thinking maybe the EventStore implementation (in my case EsEventStore) is responsible for that, or at least for providing an implementation or the subscribe method (I believe added to SubscribableMessageSource interface which EventBus extends).

The library that I extended to implement Axon 3.0.4 didn't override the subscribe method, since that didn't exit in Axon 2.4.x I suppose. I thought I could wing it by not implementing that method and I was going to return new NotImplementedException, but I just left the auto-generated "return null;". Now that I replaced it with the exception I get it thrown just as I start the application.

I will attempt to add a proper implementation to the subscribe method and go from there...

Thanks,
Bruno

Bruno Mendonça

unread,
Apr 24, 2017, 11:26:18 AM4/24/17
to Axon Framework Users
Implementing the subscribe method did the trick. My event handlers are now firing.

I'm also using AMQP in this POC and it is also working now that EsEventStore implements the subscribe method correctly. Although, it sends a message to rabbitMQ that contains all the data in the aggregate... which is not the desired functionality. I would want the message to contain the entity guid. Rabbit will then push the message to another service that will use the query api to get the object. We don't want it in the rabbitMQ message because this queue will be public.

I was wondering if I could intercept the message before it is sent to the queue and change what is being sent, but without affecting what is being sent to the EventStore. Not sure if MessageDispatchInterceptor is the best candidate for this and if there is any example out there on how to use it...

Regards,
Bruno

Bruno Mendonça

unread,
Apr 24, 2017, 7:04:42 PM4/24/17
to Axon Framework Users
I have created a separate thread for this second question:

Message has been deleted

Siamak Haschemi

unread,
Apr 30, 2017, 9:15:40 AM4/30/17
to Axon Framework Users
Hi Bruno. My team is maintaining the EventStore Java Lib on github. I am happy it worked well for you. Would you mind to share your changes in order the library to work with Axon 3 as a pull request?
Many thanks in advance.

- siamak

Bruno Mendonça

unread,
May 3, 2017, 12:12:57 PM5/3/17
to Axon Framework Users
Hi Siamak,

I'd be happy to share the code with you. There are a few things I think we still need to implement to handle transaction I believe. But I'm yet to understand how transactions work in Axon. I am actually going to post another thread on this group about that exactly.

Also the code I wrote was for a POC, just to get us going, so it may be a bit too naive... but I'd be glad to have your input on it.

If it's ok with you, I will create a branch and commit the code to that branch.

I had emailed you a few weeks ago about this, but maybe you thought it was spam or something. I'm glad you found this thread. I colleague of mine also posted an issue on github regarding licensing. Apparently, if there is no License document stating that the code is free to use, we actually can't use it. MIT is a good simple template for licensing. Hope you don't mind adding that on there...

Regards,
Bruno
Reply all
Reply to author
Forward
0 new messages