Read All Events In EventStore

Hi All,

Is there any way to read all Events that is stored in EmbeddedEventStore . Below code is based on ID

eventStore.readEvents(id).asStream().map(ss -> ss.getPayload()).collect(Collectors.toList());

I tried below Code in @Aggregate class

private void domainEventToAggregateHistory(DomainEventMessage<?> event){

System.out.println(event.getPayload()+"**payLoad");

System.out.println(event.getSequenceNumber()+"**payLoad");

System.out.println(event.getPayloadType().getSimpleName()+"**payLoad");

System.out.println(event.getTimestamp()+"**payLoad");

}

But it’s through the class cast exception

I need to read the ALL Events

Hi Saranga,

To read all events, the readEvents(String) is not the right method.
As the Javadoc on that method points out, the function is meant to retrieve all the domain events for a given Aggregate.

To retrieve all the events in the store, you can use the EventStore#openStream(TrackingToken) method.
If you use null as the given TrackingToken, it will start reading from the beginning of the stream.

Another approach to reading all events from the store which does not require you to directly utilize the EventStore, is by building an Event Handler where the first parameter type is Object.
The first parameter of an @EventHandler annotated method is always the event payload, thus the event you’ve created in your code base.
Axon will always call the event handler function with the most specific implementation of a given event.
Thus, as all your events will eventually implement Object, having a single Event Handler where the first parameter is Object will give you just that.

Note, that you should not perform this operation with a given Aggregate instance itself.
The Aggregate should be tasked with your business logic, thus handling a command which express some intent to be performed and publishing events as facts that the given operation occurred.
Retrieving the stream of all events to me does not sound like domain specific logic, more so infrastructure or delegation work.

Hope this sheds some light on the situation.

Cheers,
Steven