EventStore / EventStoreDB-Client-Java

Official Asynchronous Java 8+ Client Library for EventStoreDB 20.6+
https://eventstore.com
Apache License 2.0
61 stars 19 forks source link

Async PersistentSubscription event processing with Java ES DB Client #277

Open mmanco89 opened 8 months ago

mmanco89 commented 8 months ago

Hi, I was doing some hands-on testing with Java EventStoreDBClient around PersistentSubscriptionListener. Mine goal was achieve a concurrent event processing withing the same group on the same stream.

so what I did is I have created a class hierarchy NonBlockingEventSubscriber -> AbstractTopicalEventSubscriber -> PersistentSubscriptionListener

AbstractTopicalEventSubscriber:

    @Override
    public void onEvent(PersistentSubscription subscription, int retryCount, ResolvedEvent event) {
        try {
            T param = classToType(event.getEvent().getEventData(), defineEventClass());
            processEvent(param);

            subscription.ack(event);
        } catch (Exception e) {
            subscription.nack(NackAction.Park, e.getMessage(), event);
        }
    }

and in NonBlockingEventSubscriber I re-wrap the parent's definition in the async task executor:

    @Override
    public void onEvent(PersistentSubscription subscription, int retryCount, ResolvedEvent event) {
        executorService.execute(() -> super.onEvent(subscription, retryCount, event));
    }

and basically I was awaiting to see a very rapid execution, which I saw for a bit:

20:45:09.660 [pool-4-thread-3] INFO  processing quick Payload(number=4, title=asdf4)
20:45:09.660 [pool-4-thread-2] INFO  processing quick Payload(number=2, title=asdf2)
20:45:09.660 [pool-4-thread-1] INFO  processing quick Payload(number=0, title=asdf0)
20:45:09.662 [pool-4-thread-2] INFO  processing quick Payload(number=10, title=asdf10)
20:45:09.662 [pool-4-thread-3] INFO  processing quick Payload(number=8, title=asdf8)
20:45:09.662 [pool-4-thread-1] INFO  processing quick Payload(number=6, title=asdf6)
20:45:09.663 [pool-4-thread-1] INFO  processing quick Payload(number=12, title=asdf12)
20:45:09.663 [pool-4-thread-3] INFO  processing quick Payload(number=14, title=asdf14)
20:45:09.663 [pool-4-thread-3] INFO  processing quick Payload(number=1, title=asdf1)
20:45:09.663 [pool-4-thread-1] INFO  processing quick Payload(number=3, title=asdf3)

which is what got processed (just written on the console) before EventStore hung my connection and kindly announced:

20:45:09.663 [esdb-client-39a1f889-91ea-43d0-9768-6b24875e56fd] INFO  Received a shutdown request, closing connection to endpoint [localhost/127.0.0.1:2113]
20:45:09.666 [esdb-client-39a1f889-91ea-43d0-9768-6b24875e56fd] INFO  Connection to endpoint [localhost/127.0.0.1:2113] was closed successfully

no idea why it's saying that it have 'received a request' certainly not from my.

I was doing a bit of a research of this topic on the forums and I think the answer is this is impossible to do, but before I call this a wrap, can you confirm this is not possible, please? or might it be the valid usage but something else is to be investigated?

Thanks for your inputs.

I'm running latest ES docker image which is Version: 23.10.0.0 and com.eventstore::db-client-java::5.1.1 java client