participantManager = activityManager.enlistForBusinessAgreementWithParticipantCompletion(participant,
"SetServiceBAImpl:" + new Uid().toString());
} catch (Exception e) {
log.error("Participant enlistment failed");
e.printStackTrace(System.err);
throw new SetServiceException("Error enlisting participant", e);
}
// invoke the back-end business logic
log.info("[SERVICE] Invoking the back-end business logic");
MockSetManager.add(value);
/*
* this service employs the participant completion protocol which means it decides when it wants to commit local
* changes. If the local changes (adding the item to the set) succeeded, we notify the coordinator that we have
* completed. Otherwise, we notify the coordinator that we cannot complete. If any other participant fails or the client
* decides to cancel we can rely upon being told to compensate.
*/
log.info("[SERVICE] Prepare the backend resource and if successful notify the coordinator that we have completed our work");
if (MockSetManager.prepare()) {
try {
// tell the coordinator manager we have finished our work
log.info("[SERVICE] Prepare successful, notifying coordinator of completion");
participantManager.completed();
} catch (Exception e) {
/*
* Failed to notify the coordinator that we have finished our work. Compensate the work and throw an Exception
* to notify the client that the add operation failed.
*/
MockSetManager.rollback(value);
log.error("[SERVICE] 'completed' callback failed");
throw new SetServiceException("Error when notifying the coordinator that the work is completed", e);
}
} else {
try {
/*
* tell the participant manager we cannot complete. this will force the activity to fail
*/
log.info("[SERVICE] Prepare failed, notifying coordinator that we cannot complete");
participantManager.cannotComplete();
} catch (Exception e) {
log.error("'cannotComplete' callback failed");
throw new SetServiceException("Error when notifying the coordinator that the work is cannot be completed", e);
}
throw new SetServiceException("Unable to prepare the back-end resource");
}
}