Search in sources :

Example 1 with DomainEventValue

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]"));
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) UnitOfWorkDomainEventsValue(org.qi4j.library.eventsourcing.domain.api.UnitOfWorkDomainEventsValue) DomainEventValue(org.qi4j.library.eventsourcing.domain.api.DomainEventValue) Test(org.junit.Test)

Example 2 with DomainEventValue

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);
    }
}
Also used : DomainEventValue(org.qi4j.library.eventsourcing.domain.api.DomainEventValue)

Example 3 with DomainEventValue

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;
}
Also used : ValueBuilder(org.qi4j.api.value.ValueBuilder) UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) IOException(java.io.IOException) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException) IOException(java.io.IOException) UnitOfWorkDomainEventsValue(org.qi4j.library.eventsourcing.domain.api.UnitOfWorkDomainEventsValue) UnitOfWorkEventsVisitor(org.qi4j.library.eventsourcing.domain.source.UnitOfWorkEventsVisitor) UnitOfWorkCallback(org.qi4j.api.unitofwork.UnitOfWorkCallback) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException) DomainEventValue(org.qi4j.library.eventsourcing.domain.api.DomainEventValue)

Example 4 with DomainEventValue

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\":\"{}\"}"));
}
Also used : JSONObject(org.json.JSONObject) DomainEventValue(org.qi4j.library.eventsourcing.domain.api.DomainEventValue) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

DomainEventValue (org.qi4j.library.eventsourcing.domain.api.DomainEventValue)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 UnitOfWorkDomainEventsValue (org.qi4j.library.eventsourcing.domain.api.UnitOfWorkDomainEventsValue)2 JSONObject (org.json.JSONObject)1 UnitOfWork (org.qi4j.api.unitofwork.UnitOfWork)1 UnitOfWorkCallback (org.qi4j.api.unitofwork.UnitOfWorkCallback)1 UnitOfWorkCompletionException (org.qi4j.api.unitofwork.UnitOfWorkCompletionException)1 ValueBuilder (org.qi4j.api.value.ValueBuilder)1 UnitOfWorkEventsVisitor (org.qi4j.library.eventsourcing.domain.source.UnitOfWorkEventsVisitor)1