Search in sources :

Example 1 with BAParticipantManager

use of com.arjuna.wst11.BAParticipantManager in project wildfly by wildfly.

the class BAParticipantCompletionSuperService method saveData.

/**
     * Add an item to a set Enrolls a Participant if necessary and passes the call through to the business logic.
     *
     * @param value the value to add to the set.
     * @throws
     * @throws AlreadyInSetException if value is already in the set
     * @throws SetServiceException   if an error occurred when attempting to add the item to the set.
     */
public void saveData(String value, ServiceCommand... serviceCommands) throws TestApplicationException {
    log.trace("[BA PARTICIPANT COMPL SERVICE] invoked saveData('" + value + "')");
    eventLog.foundEventLogName(value);
    BAParticipantManager participantManager;
    BusinessActivityManager activityManager = BusinessActivityManagerFactory.businessActivityManager();
    String txid;
    try {
        txid = activityManager.currentTransaction().toString();
    } catch (SystemException se) {
        throw new RuntimeException("Error on getting TX id from BusinessActivityManager", se);
    }
    if (participantRegistry.keySet().contains(txid) && ServiceCommand.isPresent(REUSE_BA_PARTICIPANT, serviceCommands)) {
        log.trace("[BA PARTICIPANT COMPL SERVICE] Reusing BA participant manager - command: " + REUSE_BA_PARTICIPANT);
        participantManager = participantRegistry.get(txid);
    } else {
        try {
            // Enlist the Participant for this service:
            BAParticipantCompletionParticipant participant = new BAParticipantCompletionParticipant(serviceCommands, eventLog, value);
            log.trace("[BA PARTICIPANT COMPL SERVICE] Enlisting a participant into the BA");
            participantManager = activityManager.enlistForBusinessAgreementWithParticipantCompletion(participant, "BAParticipantCompletition:" + new Uid().toString());
            participantRegistry.put(txid, participantManager);
        } catch (Exception e) {
            log.error("[BA PARTICIPANT COMPL SERVICE]  Participant enlistment failed", e);
            throw new RuntimeException("Error enlisting participant", e);
        }
    }
    if (ServiceCommand.isPresent(APPLICATION_EXCEPTION, serviceCommands)) {
        throw new TestApplicationException("Intentionally thrown Application Exception - service command set to: " + APPLICATION_EXCEPTION);
    }
    // There could be invoked some back-end business logic here
    /*
         * 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.trace("[BA PARTICIPANT COMPL SERVICE] Prepare the backend resource and if successful notify the coordinator that we have completed our work");
    if (ServiceCommand.isPresent(DO_COMPLETE, serviceCommands)) {
        try {
            // Tell the coordinator manager we have finished our work
            log.trace("[BA PARTICIPANT COMPL 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. */
            log.error("[BA PARTICIPANT COMPL SERVICE] 'completed' callback failed");
            throw new RuntimeException("Error when notifying the coordinator that the work is completed", e);
        }
    }
    if (ServiceCommand.isPresent(CANNOT_COMPLETE, serviceCommands)) {
        try {
            // Tell the participant manager we cannot complete. This will force the activity to fail.
            log.trace("[BA PARTICIPANT COMPL SERVICE] Prepared fail, notifying coordinator that we cannot complete");
            participantManager.cannotComplete();
            return;
        } catch (Exception e) {
            log.error("[BA PARTICIPANT COMPL SERVICE] 'cannotComplete' callback failed");
            throw new RuntimeException("Error when notifying the coordinator that the work is cannot be completed", e);
        }
    }
}
Also used : Uid(com.arjuna.ats.arjuna.common.Uid) TestApplicationException(org.jboss.as.test.xts.base.TestApplicationException) SystemException(com.arjuna.wst.SystemException) BusinessActivityManager(com.arjuna.mw.wst11.BusinessActivityManager) BAParticipantManager(com.arjuna.wst11.BAParticipantManager) TestApplicationException(org.jboss.as.test.xts.base.TestApplicationException) SystemException(com.arjuna.wst.SystemException)

Example 2 with BAParticipantManager

use of com.arjuna.wst11.BAParticipantManager in project wildfly by wildfly.

the class BusinessActivityExecutionService method enlistParticipant.

@Override
public void enlistParticipant() throws Exception {
    assert currentActivity != null : "No active business activity";
    LOGGER.infof("trying to enlist participant to the business activity %s", currentActivity);
    BusinessActivityManager.getBusinessActivityManager().resume(currentActivity);
    BusinessActivityParticipant businessActivityParticipant = new BusinessActivityParticipant(new Uid().stringForm());
    BAParticipantManager participantManager = BusinessActivityManager.getBusinessActivityManager().enlistForBusinessAgreementWithParticipantCompletion(businessActivityParticipant, businessActivityParticipant.getId());
    participantManager.completed();
    currentActivity = BusinessActivityManager.getBusinessActivityManager().suspend();
    LOGGER.infof("enlisted participant %s", businessActivityParticipant);
}
Also used : Uid(com.arjuna.ats.arjuna.common.Uid) BAParticipantManager(com.arjuna.wst11.BAParticipantManager)

Example 3 with BAParticipantManager

use of com.arjuna.wst11.BAParticipantManager in project wildfly by wildfly.

the class BusinessActivityRemoteService method execute.

@Override
public void execute() throws Exception {
    LOGGER.infof("trying to enlist participant to the business activity %s", BusinessActivityManager.getBusinessActivityManager().currentTransaction());
    String participantId = new Uid().stringForm();
    BusinessActivityParticipant businessActivityParticipant = new BusinessActivityParticipant(participantId);
    BAParticipantManager participantManager = BusinessActivityManager.getBusinessActivityManager().enlistForBusinessAgreementWithParticipantCompletion(businessActivityParticipant, businessActivityParticipant.getId());
    participantManager.completed();
    LOGGER.infof("enlisted participant %s", businessActivityParticipant);
}
Also used : Uid(com.arjuna.ats.arjuna.common.Uid) BAParticipantManager(com.arjuna.wst11.BAParticipantManager)

Example 4 with BAParticipantManager

use of com.arjuna.wst11.BAParticipantManager in project wildfly by wildfly.

the class BACoordinatorCompletionSuperService method saveData.

/**
     * Add an item to a set and enroll a Participant if necessary then pass the call through to the business logic.
     *
     * @param value the value to add to the set.
     * @throws AlreadyInSetException if value is already in the set
     * @throws SetServiceException   if an error occurred when attempting to add the item to the set.
     */
public void saveData(String value, ServiceCommand... serviceCommands) throws TestApplicationException {
    log.trace("[BA COORDINATOR COMPL SERVICE] web method saveData('" + value + "')");
    eventLog.foundEventLogName(value);
    BusinessActivityManager activityManager = BusinessActivityManagerFactory.businessActivityManager();
    // transaction context associated with this thread
    String transactionId;
    try {
        transactionId = activityManager.currentTransaction().toString();
    } catch (SystemException e) {
        throw new RuntimeException("Unable to lookup existing business activity", e);
    }
    // Lookup existing participant or register new participant (
    BACoordinationCompletionParticipant participantBA = BACoordinationCompletionParticipant.getSomeParticipant(transactionId);
    if (participantBA != null && ServiceCommand.isPresent(REUSE_BA_PARTICIPANT, serviceCommands)) {
        log.trace("[BA COORDINATOR COMPL SERVICE] Re-using the existing participant, already registered for this BA - command set to: " + REUSE_BA_PARTICIPANT);
    } else {
        try {
            // enlist the Participant for this service:
            participantBA = new BACoordinationCompletionParticipant(serviceCommands, eventLog, transactionId, value);
            BACoordinationCompletionParticipant.recordParticipant(transactionId, participantBA);
            log.trace("[BA COORDINATOR COMPL SERVICE] Enlisting a participant into the BA");
            BAParticipantManager baParticipantManager = activityManager.enlistForBusinessAgreementWithCoordinatorCompletion(participantBA, "BACoordinatorCompletition:" + new Uid().toString());
            if (ServiceCommand.isPresent(CANNOT_COMPLETE, serviceCommands)) {
                baParticipantManager.cannotComplete();
                return;
            }
            if (ServiceCommand.isPresent(DO_COMPLETE, serviceCommands)) {
                throw new RuntimeException("Only ParticipantCompletion participants are supposed to call complete. " + "CoordinatorCompletion participants need to wait to be notified by the coordinator.");
            }
        } catch (Exception e) {
            log.error("[BA COORDINATOR COMPL SERVICE] Participant enlistment failed", e);
            throw new RuntimeException("Error enlisting participant", e);
        }
    }
    if (ServiceCommand.isPresent(APPLICATION_EXCEPTION, serviceCommands)) {
        throw new TestApplicationException("Intentionally thrown Application Exception - service command set to: " + APPLICATION_EXCEPTION);
    }
    // invoke the back-end business logic
    log.trace("[BA COORDINATOR COMPL SERVICE] Invoking the back-end business logic - saving value: " + value);
}
Also used : Uid(com.arjuna.ats.arjuna.common.Uid) TestApplicationException(org.jboss.as.test.xts.base.TestApplicationException) SystemException(com.arjuna.wst.SystemException) BusinessActivityManager(com.arjuna.mw.wst11.BusinessActivityManager) BAParticipantManager(com.arjuna.wst11.BAParticipantManager) TestApplicationException(org.jboss.as.test.xts.base.TestApplicationException) SystemException(com.arjuna.wst.SystemException)

Example 5 with BAParticipantManager

use of com.arjuna.wst11.BAParticipantManager in project quickstart by wildfly.

the class SetServiceBAImpl method addValueToSet.

/**
 * Add an item to a set Enrolls a Participant if necessary and passes the call through to the business logic.
 *
 * @param value the value to add to the set.
 * @throws AlreadyInSetException if value is already in the set
 * @throws SetServiceException if an error occurred when attempting to add the item to the set.
 */
@WebMethod
public void addValueToSet(String value) throws AlreadyInSetException, SetServiceException {
    System.out.println("[SERVICE] invoked addValueToSet('" + value + "')");
    BAParticipantManager participantManager;
    try {
        // enlist the Participant for this service:
        SetParticipantBA participant = new SetParticipantBA(value);
        BusinessActivityManager activityManager = BusinessActivityManagerFactory.businessActivityManager();
        System.out.println("[SERVICE] Enlisting a participant into the BA");
        participantManager = activityManager.enlistForBusinessAgreementWithParticipantCompletion(participant, "SetServiceBAImpl:" + UUID.randomUUID());
    } catch (Exception e) {
        System.err.println("Participant enlistment failed");
        e.printStackTrace(System.err);
        throw new SetServiceException("Error enlisting participant", e);
    }
    // invoke the back-end business logic
    System.out.println("[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.
         */
    System.out.println("[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
            System.out.println("[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);
            System.err.println("[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
                 */
            System.out.println("[SERVICE] Prepare failed, notifying coordinator that we cannot complete");
            participantManager.cannotComplete();
        } catch (Exception e) {
            System.err.println("'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");
    }
}
Also used : BusinessActivityManager(com.arjuna.mw.wst11.BusinessActivityManager) BAParticipantManager(com.arjuna.wst11.BAParticipantManager) WebMethod(javax.jws.WebMethod)

Aggregations

BAParticipantManager (com.arjuna.wst11.BAParticipantManager)5 Uid (com.arjuna.ats.arjuna.common.Uid)4 BusinessActivityManager (com.arjuna.mw.wst11.BusinessActivityManager)3 SystemException (com.arjuna.wst.SystemException)2 TestApplicationException (org.jboss.as.test.xts.base.TestApplicationException)2 WebMethod (javax.jws.WebMethod)1