Axon 3 : Extract all events for an aggregate

Hi all,

what is the best solution/pattern to extract all events for an aggregate ?

I’ve write something like this :

`

List retour = new ArrayList<>();
DomainEventStream domainEventStream = eventStore.readEvents(id);
while(domainEventStream.hasNext()) {
DomainEventMessage<?> event = domainEventStream.next();
retour.add(event.getPayloadType().toString());

}
return retour;

`

  • Is it better to use the EventStore or EventStorageEngine interface ?

  • Is it a good idea to use the EventStore or do i need to implement my own history solution ?

Thanks

Hi Philippe,

your code is fine, except that the DomainEventStream may eventually contain more items than you’d want to put in a List. You can also work with java Streams by using domainEventStream.asStream().

In any case, you’d want to use the EventStore interface for this. Leave the EventStorageEngine as a technical storage detail as much as possible.

Cheers,

Allard

Hi Allard,

thanks for your response.
I’ve wrote this endpoint to access of the history of an aggregrate (for the moment i haven’t implemented items limit)

`

@RequestMapping(method = GET, value = “/{id}/history”, produces = MediaType.APPLICATION_JSON_VALUE)
public List history(
@PathVariable String id)
{
return eventStore.readEvents(id).asStream().map(this::domainEventToAggregateHistory).collect(Collectors.toList());
}

private AggregateHistoryDTO domainEventToAggregateHistory(DomainEventMessage<?> event)
{
return new AggregateHistoryDTO(event.getPayloadType().getSimpleName(), (BopEvent) event.getPayload(), event.getSequenceNumber(),
event.getTimestamp());
}

`

`

@AllArgsConstructor
@Getter
public class AggregateHistoryDTO
{
private String name;

private BopEvent event; // All my events extends BopEvent

private long sequenceNumber;

private Instant timestamp;
}

`

Cheers