FileUtils.deleteDirectory(eventsDir);
// we create a serializer, so we can ensure the event store and the upcasters use the same configuration
Serializer serializer = new XStreamSerializer();
// initialize a FileSystem Event Store
FileSystemEventStore eventStore = new FileSystemEventStore(serializer, new SimpleEventFileResolver(eventsDir));
// initialize the upcaster chain with our upcaster
eventStore.setUpcasterChain(new LazyUpcasterChain(serializer,
Collections.<Upcaster>singletonList(new ToDoItemUpcaster())));
// we append some events. Notice we append a "ToDoItemCreatedEvent".
eventStore.appendEvents("UpcasterSample", new SimpleDomainEventStream(
new GenericDomainEventMessage("todo1", 0, new ToDoItemCreatedEvent("todo1", "I need to do this today")),
new GenericDomainEventMessage("todo1", 1, new ToDoItemCompletedEvent("todo1"))
));
eventStore.appendEvents("UpcasterSample", new SimpleDomainEventStream(
new GenericDomainEventMessage("todo2", 0, new ToDoItemCreatedEvent("todo2", "I also need to do this"))
));
// now, we read the events from the "todo1" stream
DomainEventStream eventStream = eventStore.readEvents("UpcasterSample", "todo1");
while (eventStream.hasNext()) {
// and print them, so that we can see what we ended up with
System.out.println(eventStream.next().getPayload().toString());
}
IOUtils.closeQuietlyIfCloseable(eventStream);