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;
}
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
}
}
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;
}
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);
}
}
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;
}
Aggregations