private boolean invokeExistingSagas( EventProxy<?> event, Class<? extends Saga> sagaType, Collection<AssociationValue> associationValues) { Set<String> sagas = new TreeSet<>(); for (AssociationValue associationValue : associationValues) { sagas.addAll(sagaRepository.find(sagaType, associationValue)); } for (Saga sagaInCreation : sagasInCreation.values()) { if (sagaType.isInstance(sagaInCreation) && containsAny(sagaInCreation.getAssociationValues(), associationValues)) { sagas.add(sagaInCreation.getSagaIdentifier()); } } boolean sagaOfTypeInvoked = false; for (final String sagaId : sagas) { if (synchronizeSagaAccess) { lock.obtainLock(sagaId); Saga invokedSaga = null; try { invokedSaga = loadAndInvoke(event, sagaId, associationValues); if (invokedSaga != null) { sagaOfTypeInvoked = true; } } finally { doReleaseLock(sagaId, invokedSaga); } } else { loadAndInvoke(event, sagaId, associationValues); } } return sagaOfTypeInvoked; }
private void doInvokeSaga(EventProxy<?> event, Saga saga) { try { saga.handle(event); } catch (RuntimeException e) { if (suppressExceptions) { logger.error( format( "An exception occurred while a Saga [%s] was handling an Event [%s]:", saga.getClass().getSimpleName(), event.getPayloadType().getSimpleName()), e); } else { throw e; } } }
private Saga loadAndInvoke( EventProxy<?> event, String sagaId, Collection<AssociationValue> associations) { Saga saga = sagasInCreation.get(sagaId); if (saga == null) { saga = sagaRepository.load(sagaId); } if (saga == null || !saga.isActive() || !containsAny(saga.getAssociationValues(), associations)) { return null; } preProcessSaga(saga); try { doInvokeSaga(event, saga); } finally { commit(saga); } return saga; }
private void startNewSaga( EventProxy<?> event, Class<? extends Saga> sagaType, AssociationValue associationValue) { Saga newSaga = sagaFactory.createSaga(sagaType); newSaga.getAssociationValues().add(associationValue); preProcessSaga(newSaga); sagasInCreation.put(newSaga.getSagaIdentifier(), newSaga); try { if (synchronizeSagaAccess) { lock.obtainLock(newSaga.getSagaIdentifier()); try { doInvokeSaga(event, newSaga); } finally { try { sagaRepository.add(newSaga); } finally { doReleaseLock(newSaga.getSagaIdentifier(), newSaga); } } } else { try { doInvokeSaga(event, newSaga); } finally { sagaRepository.add(newSaga); } } } finally { removeEntry(newSaga.getSagaIdentifier(), sagasInCreation); } }