use of org.qi4j.api.unitofwork.UnitOfWorkCompletionException in project qi4j-sdk by Qi4j.
the class UnitOfWorkInstance method applyChanges.
private List<StateCommitter> applyChanges() throws UnitOfWorkCompletionException {
List<StateCommitter> committers = new ArrayList<>();
for (EntityStoreUnitOfWork entityStoreUnitOfWork : storeUnitOfWork.values()) {
try {
StateCommitter committer = entityStoreUnitOfWork.applyChanges();
committers.add(committer);
} catch (Exception e) {
// Cancel all previously prepared stores
for (StateCommitter committer : committers) {
committer.cancel();
}
if (e instanceof ConcurrentEntityStateModificationException) {
// If we cancelled due to concurrent modification, then create the proper exception for it!
ConcurrentEntityStateModificationException mee = (ConcurrentEntityStateModificationException) e;
Collection<EntityReference> modifiedEntityIdentities = mee.modifiedEntities();
Collection<EntityComposite> modifiedEntities = new ArrayList<>();
for (EntityReference modifiedEntityIdentity : modifiedEntityIdentities) {
Collection<EntityInstance> instances = instanceCache.values();
for (EntityInstance instance : instances) {
if (instance.identity().equals(modifiedEntityIdentity)) {
modifiedEntities.add(instance.<EntityComposite>proxy());
}
}
}
throw new ConcurrentEntityModificationException(modifiedEntities);
} else {
throw new UnitOfWorkCompletionException(e);
}
}
}
return committers;
}
use of org.qi4j.api.unitofwork.UnitOfWorkCompletionException 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;
}
use of org.qi4j.api.unitofwork.UnitOfWorkCompletionException 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.api.unitofwork.UnitOfWorkCompletionException in project qi4j-sdk by Qi4j.
the class DebuggingServiceMixin method debug.
@Override
public void debug(Composite composite, String message) {
UnitOfWork uow = uowf.newUnitOfWork();
try {
List<Serializable> paramsList = new ArrayList<Serializable>();
createDebugRecord(uow, composite, message, paramsList);
uow.complete();
} catch (ConcurrentEntityModificationException e) {
// ignore for now. Perhaps discard() and try again.
} catch (UnitOfWorkCompletionException e) {
// ignore for now. Perhaps discard() and try again.
}
}
use of org.qi4j.api.unitofwork.UnitOfWorkCompletionException in project qi4j-sdk by Qi4j.
the class WicketQi4jApplication method handleUnitOfWork.
private void handleUnitOfWork() {
getRequestCycleListeners().add(new AbstractRequestCycleListener() {
@Override
public void onBeginRequest(final RequestCycle requestCycle) {
super.onBeginRequest(requestCycle);
logger.debug("================================");
logger.debug("REQUEST start");
logger.debug(requestCycle.getRequest().toString());
logger.debug(requestCycle.getRequest().getRequestParameters().toString());
UnitOfWork uow = uowf.newUnitOfWork(UsecaseBuilder.newUsecase("REQUEST"));
logger.debug(" ### NEW " + uow + " ### MODULE: " + qi4jModule);
}
@Override
public void onEndRequest(final RequestCycle requestCycle) {
UnitOfWork uow = uowf.currentUnitOfWork();
if (uow != null) {
try {
if ("POST".equals(((HttpServletRequest) requestCycle.getRequest().getContainerRequest()).getMethod())) {
// "Save"
logger.debug(" ### COMPLETE " + uow + " ### MODULE: " + qi4jModule);
uow.complete();
} else {
// GET requests
logger.debug(" ### DISCARD " + uow + " ### MODULE: " + qi4jModule);
uow.discard();
}
} catch (ConcurrentEntityModificationException e) {
logger.error(" ### DISCARD " + uow + " ### MODULE: " + qi4jModule);
uow.discard();
e.printStackTrace();
} catch (UnitOfWorkCompletionException e) {
logger.error(" ### DISCARD " + uow + " ### MODULE: " + qi4jModule);
uow.discard();
e.printStackTrace();
}
}
logger.debug("REQUEST end");
logger.debug("------------------------------------");
}
});
}
Aggregations