I was wondering how I prevent or detect duplicate events when I have a configured a Custom MessageSource. I have a custom MessageSource sending events to GCP Pub Sub topics. If I make a call to eventGateway.publish(event) and I have an @EventHandler to consume the event, the handler is called once directly and once when consumed from the custom MessageSource.
Firstly, I believe this is your first post here, @cneuwirt, so welcome to the forum!
Secondly, I am afraid I need a little more information before I can help you.
So, let me fire a bunch of questions at you:
What is this custom Message Source? Are you implementing some interface of Axon Framework for this? If so, how is this custom Message Source configured inside your application?
You state the “custom MessageSource sends events to GCP Pub Sub topics.” To be frank with you, when I think of a Message Source, I think of a component that provides me messages. The act of sending that to something else is what an Event Handler would do. So, to be clear, are you talking about an Event Handler here that handles events from the custom Message Source and sends them to GCP Pub Sub?
Can you share a bit more code on how you are invoking EventGateway#publish? Are you performing this task in an Axon Framework message handler, or is this your own service entirely?
How is the @EventHandler that consumes the event configured in your application? Could you share some code about this?
So, in short, it is not clear to me what your message flow looks like, @cneuwirt. Without knowing that is pretty hard to state where you need to do some deduplication.
There is, however, a statement that’s always true in my book. Whenever possible, make your event handlers idempotent. When doing so, your event handler’s implementation is automatically resilient against duplicate event handling.
Basically, I implemented an extension-gcp-pubsub like the extension-amp which supports publishing and consuming events to and from GCP Pub Sub Topics/Subscriptions.
The main components are the GcpPubSubEventPublisher and GcpPubSubMessageSource.
The flow is EventGateway#publish → GcpPubSubEventPublisher#send → [GCP Topic] → [GCP Subscription] → GcpPubSubMessageSource → @EventHandler
My initial concern was that EventGateway#publish would both deliver the event to local @EventHandler methods as well as publish to GCP Topic/Subscription which would then be consumed via GcpPubSubMessageSource and redelivered to same @EventHandler.
I was initially confused on the Axon Event Flow worked but once I better understood the “Processing Groups” to “Event Processors” relationship, there was not problem and everything worked as expected.