Search in sources :

Example 1 with UnitOfWorkCallback

use of org.qi4j.api.unitofwork.UnitOfWorkCallback in project qi4j-sdk by Qi4j.

the class UnitOfWorkInstance method complete.

public void complete() throws UnitOfWorkCompletionException {
    checkOpen();
    // Copy list so that it cannot be modified during completion
    List<UnitOfWorkCallback> currentCallbacks = callbacks == null ? null : new ArrayList<>(callbacks);
    // Commit state to EntityStores
    List<StateCommitter> committers = applyChanges();
    // Check callbacks
    notifyBeforeCompletion(currentCallbacks);
    // Commit all changes
    for (StateCommitter committer : committers) {
        committer.commit();
    }
    close();
    // Call callbacks
    notifyAfterCompletion(currentCallbacks, COMPLETED);
    callbacks = currentCallbacks;
}
Also used : UnitOfWorkCallback(org.qi4j.api.unitofwork.UnitOfWorkCallback) StateCommitter(org.qi4j.spi.entitystore.StateCommitter)

Example 2 with UnitOfWorkCallback

use of org.qi4j.api.unitofwork.UnitOfWorkCallback in project qi4j-sdk by Qi4j.

the class UnitOfWorkInstance method notifyAfterCompletion.

private void notifyAfterCompletion(List<UnitOfWorkCallback> callbacks, final UnitOfWorkCallback.UnitOfWorkStatus status) {
    if (callbacks != null) {
        for (UnitOfWorkCallback callback : callbacks) {
            try {
                callback.afterCompletion(status);
            } catch (Exception e) {
            // Ignore
            }
        }
    }
    // Notify entities
    try {
        for (EntityInstance instance : instanceCache.values()) {
            boolean isCallback = instance.proxy() instanceof UnitOfWorkCallback;
            boolean isNotRemoved = !instance.status().equals(EntityStatus.REMOVED);
            if (isCallback && isNotRemoved) {
                UnitOfWorkCallback callback = UnitOfWorkCallback.class.cast(instance.proxy());
                callback.afterCompletion(status);
            }
        }
    } catch (Exception e) {
    // Ignore
    }
}
Also used : UnitOfWorkCallback(org.qi4j.api.unitofwork.UnitOfWorkCallback) EntityInstance(org.qi4j.runtime.entity.EntityInstance) ConcurrentEntityModificationException(org.qi4j.api.unitofwork.ConcurrentEntityModificationException) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException) UnitOfWorkException(org.qi4j.api.unitofwork.UnitOfWorkException) EntityTypeNotFoundException(org.qi4j.api.unitofwork.EntityTypeNotFoundException) NoSuchEntityException(org.qi4j.api.unitofwork.NoSuchEntityException) ConcurrentEntityStateModificationException(org.qi4j.spi.entitystore.ConcurrentEntityStateModificationException) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException)

Example 3 with UnitOfWorkCallback

use of org.qi4j.api.unitofwork.UnitOfWorkCallback in project qi4j-sdk by Qi4j.

the class UnitOfWorkInstance method discard.

public void discard() {
    if (!isOpen()) {
        return;
    }
    close();
    // Copy list so that it cannot be modified during completion
    List<UnitOfWorkCallback> currentCallbacks = callbacks == null ? null : new ArrayList<>(callbacks);
    // Call callbacks
    notifyAfterCompletion(currentCallbacks, DISCARDED);
    for (EntityStoreUnitOfWork entityStoreUnitOfWork : storeUnitOfWork.values()) {
        entityStoreUnitOfWork.discard();
    }
    callbacks = currentCallbacks;
}
Also used : UnitOfWorkCallback(org.qi4j.api.unitofwork.UnitOfWorkCallback) EntityStoreUnitOfWork(org.qi4j.spi.entitystore.EntityStoreUnitOfWork)

Example 4 with UnitOfWorkCallback

use of org.qi4j.api.unitofwork.UnitOfWorkCallback in project qi4j-sdk by Qi4j.

the class UnitOfWorkInstance method notifyBeforeCompletion.

private void notifyBeforeCompletion(List<UnitOfWorkCallback> callbacks) throws UnitOfWorkCompletionException {
    // Notify explicitly registered callbacks
    if (callbacks != null) {
        for (UnitOfWorkCallback callback : callbacks) {
            callback.beforeCompletion();
        }
    }
    // Notify entities
    try {
        for (EntityInstance instance : instanceCache.values()) {
            boolean isCallback = instance.proxy() instanceof UnitOfWorkCallback;
            boolean isNotRemoved = !instance.status().equals(EntityStatus.REMOVED);
            if (isCallback && isNotRemoved) {
                UnitOfWorkCallback callback = UnitOfWorkCallback.class.cast(instance.proxy());
                callback.beforeCompletion();
            }
        }
    } catch (UnitOfWorkCompletionException e) {
        throw e;
    } catch (Exception e) {
        throw new UnitOfWorkCompletionException(e);
    }
}
Also used : UnitOfWorkCallback(org.qi4j.api.unitofwork.UnitOfWorkCallback) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException) EntityInstance(org.qi4j.runtime.entity.EntityInstance) ConcurrentEntityModificationException(org.qi4j.api.unitofwork.ConcurrentEntityModificationException) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException) UnitOfWorkException(org.qi4j.api.unitofwork.UnitOfWorkException) EntityTypeNotFoundException(org.qi4j.api.unitofwork.EntityTypeNotFoundException) NoSuchEntityException(org.qi4j.api.unitofwork.NoSuchEntityException) ConcurrentEntityStateModificationException(org.qi4j.spi.entitystore.ConcurrentEntityStateModificationException) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException)

Example 5 with UnitOfWorkCallback

use of org.qi4j.api.unitofwork.UnitOfWorkCallback in project qi4j-sdk by Qi4j.

the class TransactionNotificationConcern method createEvent.

@Override
public ApplicationEvent createEvent(String name, Object[] args) {
    final UnitOfWork unitOfWork = uowf.currentUnitOfWork();
    ApplicationEvent event = next.createEvent(name, args);
    // Add event to list in UoW
    UnitOfWorkApplicationEvents events = unitOfWork.metaInfo(UnitOfWorkApplicationEvents.class);
    if (events == null) {
        events = new UnitOfWorkApplicationEvents();
        unitOfWork.setMetaInfo(events);
        unitOfWork.addUnitOfWorkCallback(new UnitOfWorkCallback() {

            @Override
            public void beforeCompletion() throws UnitOfWorkCompletionException {
            }

            @Override
            public void afterCompletion(UnitOfWorkStatus status) {
                if (status.equals(UnitOfWorkStatus.COMPLETED)) {
                    UnitOfWorkApplicationEvents events = unitOfWork.metaInfo(UnitOfWorkApplicationEvents.class);
                    try {
                        eventStore.storeEvents(events.getEvents());
                    } catch (IOException e) {
                        logger.error("Could not store events", e);
                    // How do we handle this? This is a major error!
                    }
                }
            }
        });
    }
    events.add(event);
    return event;
}
Also used : UnitOfWorkCallback(org.qi4j.api.unitofwork.UnitOfWorkCallback) UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) UnitOfWorkCompletionException(org.qi4j.api.unitofwork.UnitOfWorkCompletionException) ApplicationEvent(org.qi4j.library.eventsourcing.application.api.ApplicationEvent) IOException(java.io.IOException)

Aggregations

UnitOfWorkCallback (org.qi4j.api.unitofwork.UnitOfWorkCallback)6 UnitOfWorkCompletionException (org.qi4j.api.unitofwork.UnitOfWorkCompletionException)4 IOException (java.io.IOException)2 ConcurrentEntityModificationException (org.qi4j.api.unitofwork.ConcurrentEntityModificationException)2 EntityTypeNotFoundException (org.qi4j.api.unitofwork.EntityTypeNotFoundException)2 NoSuchEntityException (org.qi4j.api.unitofwork.NoSuchEntityException)2 UnitOfWork (org.qi4j.api.unitofwork.UnitOfWork)2 UnitOfWorkException (org.qi4j.api.unitofwork.UnitOfWorkException)2 EntityInstance (org.qi4j.runtime.entity.EntityInstance)2 ConcurrentEntityStateModificationException (org.qi4j.spi.entitystore.ConcurrentEntityStateModificationException)2 EntityNotFoundException (org.qi4j.spi.entitystore.EntityNotFoundException)2 ValueBuilder (org.qi4j.api.value.ValueBuilder)1 ApplicationEvent (org.qi4j.library.eventsourcing.application.api.ApplicationEvent)1 DomainEventValue (org.qi4j.library.eventsourcing.domain.api.DomainEventValue)1 UnitOfWorkDomainEventsValue (org.qi4j.library.eventsourcing.domain.api.UnitOfWorkDomainEventsValue)1 UnitOfWorkEventsVisitor (org.qi4j.library.eventsourcing.domain.source.UnitOfWorkEventsVisitor)1 EntityStoreUnitOfWork (org.qi4j.spi.entitystore.EntityStoreUnitOfWork)1 StateCommitter (org.qi4j.spi.entitystore.StateCommitter)1