use of org.qi4j.library.eventsourcing.domain.api.DomainEventValue in project qi4j-sdk by Qi4j.
the class UnitOfWorkRouterTest method testRouterException.
@Test(expected = IOException.class)
public void testRouterException() throws IOException {
final List<String> matched = new ArrayList<String>();
UnitOfWorkRouter<IOException> router = new UnitOfWorkRouter<IOException>();
router.route(Events.withUsecases("Test2"), new Receiver<UnitOfWorkDomainEventsValue, IOException>() {
@Override
public void receive(UnitOfWorkDomainEventsValue item) throws IOException {
throw new IOException("Failed");
}
});
EventRouter<IOException> eventRouter = new EventRouter<IOException>();
eventRouter.defaultReceiver(new Receiver<DomainEventValue, IOException>() {
@Override
public void receive(DomainEventValue item) throws IOException {
System.out.println(item);
}
});
router.defaultReceiver(eventRouter);
Inputs.iterable(list).transferTo(router);
Assert.assertThat(matched.toString(), CoreMatchers.equalTo("[Test]"));
}
use of org.qi4j.library.eventsourcing.domain.api.DomainEventValue in project qi4j-sdk by Qi4j.
the class DomainEventCreationConcern method invoke.
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (DomainEvents.currentEvent() == null) {
// Create eventValue
DomainEventValue eventValue = domainEventFactory.createEvent(entity, method.getName(), args);
DomainEvents.setCurrentEvent(eventValue);
try {
return next.invoke(proxy, method, args);
} finally {
DomainEvents.setCurrentEvent(null);
}
} else {
// This is probably a replay call
return next.invoke(proxy, method, args);
}
}
use of org.qi4j.library.eventsourcing.domain.api.DomainEventValue in project qi4j-sdk by Qi4j.
the class UnitOfWorkNotificationConcern method createEvent.
@Override
public DomainEventValue createEvent(EntityComposite entity, String name, Object[] args) {
final UnitOfWork unitOfWork = uowf.currentUnitOfWork();
DomainEventValue eventValue = next.createEvent(api.dereference(entity), name, args);
// Add eventValue to list in UoW
UnitOfWorkEvents events = unitOfWork.metaInfo(UnitOfWorkEvents.class);
if (events == null) {
events = new UnitOfWorkEvents();
unitOfWork.setMetaInfo(events);
unitOfWork.addUnitOfWorkCallback(new UnitOfWorkCallback() {
String user;
@Override
public void beforeCompletion() throws UnitOfWorkCompletionException {
user = currentUser.getCurrentUser();
}
@Override
public void afterCompletion(UnitOfWorkStatus status) {
if (status.equals(UnitOfWorkStatus.COMPLETED)) {
UnitOfWorkEvents events = unitOfWork.metaInfo(UnitOfWorkEvents.class);
ValueBuilder<UnitOfWorkDomainEventsValue> builder = vbf.newValueBuilder(UnitOfWorkDomainEventsValue.class);
builder.prototype().user().set(user);
builder.prototype().timestamp().set(System.currentTimeMillis());
builder.prototype().usecase().set(unitOfWork.usecase().name());
builder.prototype().version().set(version);
builder.prototype().events().get().addAll(events.getEventValues());
try {
final UnitOfWorkDomainEventsValue unitOfWorkDomainValue = builder.newInstance();
Inputs.iterable(Iterables.iterable(unitOfWorkDomainValue)).transferTo(eventOutput);
for (UnitOfWorkEventsVisitor unitOfWorkEventsVisitor : transactionVisitors) {
try {
unitOfWorkEventsVisitor.visit(unitOfWorkDomainValue);
} catch (Exception e) {
logger.warn("Could not deliver events", e);
}
}
} catch (IOException e) {
logger.error("Could not store events", e);
// How do we handle this? This is a major error!
}
}
}
});
}
events.add(eventValue);
return eventValue;
}
use of org.qi4j.library.eventsourcing.domain.api.DomainEventValue in project qi4j-sdk by Qi4j.
the class EventRouterTest method testRouter.
@Test
public void testRouter() throws IOException, JSONException {
final List<DomainEventValue> matched = new ArrayList<DomainEventValue>();
EventRouter<IOException> router = new EventRouter<IOException>();
router.route(Events.withNames("Test1", "Test2"), new Receiver<DomainEventValue, IOException>() {
@Override
public void receive(DomainEventValue item) throws IOException {
matched.add(item);
}
});
Inputs.iterable(Events.events(list)).transferTo(router);
assertEquals(2, matched.size());
jsonObjectsEquals(new JSONObject(matched.get(0).toString()), new JSONObject("{\"name\":\"Test1\",\"entityType\":\"Foo\",\"entityId\":\"123\",\"parameters\":\"{}\"}"));
jsonObjectsEquals(new JSONObject(matched.get(1).toString()), new JSONObject("{\"name\":\"Test2\",\"entityType\":\"Foo\",\"entityId\":\"123\",\"parameters\":\"{}\"}"));
}
Aggregations