Search in sources :

Example 1 with TestApplicationException

use of org.jboss.as.test.xts.base.TestApplicationException in project wildfly by wildfly.

the class ATSuperService method invokeWithCallName.

/**
     * Adding 2 participants - Volatile and Durable
     *
     * @param callName        call name works for differentiate several calls to the same webservice
     *                        if you don't want care pass null (or overloaded method :)
     * @param serviceCommands service commands that service will react on
     * @throws WrongStateException
     * @throws com.arjuna.wst.SystemException
     * @throws UnknownTransactionException
     * @throws SecurityException
     * @throws javax.transaction.SystemException
     * @throws IllegalStateException
     */
public void invokeWithCallName(String callName, ServiceCommand[] serviceCommands) throws TestApplicationException {
    log.infof("[AT SERVICE] web method invoke(%s) with eventLog %s", callName, eventLog);
    eventLog.foundEventLogName(callName);
    UserTransaction userTransaction;
    try {
        userTransaction = UserTransactionFactory.userTransaction();
        String transactionId = userTransaction.transactionIdentifier();
        log.debug("RestaurantServiceAT transaction id =" + transactionId);
        // Enlist the Durable Participant for this service
        TransactionManager transactionManager = TransactionManagerFactory.transactionManager();
        ATDurableParticipant durableParticipant = new ATDurableParticipant(serviceCommands, callName, eventLog, transactionId);
        log.trace("[SERVICE] Enlisting a Durable2PC participant into the AT");
        transactionManager.enlistForDurableTwoPhase(durableParticipant, "ATServiceDurable:" + new Uid().toString());
        // Enlist the Volatile Participant for this service
        ATVolatileParticipant volatileParticipant = new ATVolatileParticipant(serviceCommands, callName, eventLog, transactionId);
        log.trace("[SERVICE] Enlisting a VolatilePC participant into the AT");
        transactionManager.enlistForVolatileTwoPhase(volatileParticipant, "ATServiceVolatile:" + new Uid().toString());
    } catch (Exception e) {
        throw new RuntimeException("Error when enlisting participants", e);
    }
    if (ServiceCommand.isPresent(APPLICATION_EXCEPTION, serviceCommands)) {
        throw new TestApplicationException("Intentionally thrown Application Exception - service command was set to: " + APPLICATION_EXCEPTION);
    }
    if (ServiceCommand.isPresent(ROLLBACK_ONLY, serviceCommands)) {
        log.trace("Intentionally the service settings transaction to rollback only - service command was set to: " + ROLLBACK_ONLY);
        try {
            userTransaction.rollback();
        } catch (Exception e) {
            throw new RuntimeException("The rollback is not possible", e);
        }
    }
    // There will be some business logic here normally
    log.trace("|AT SERVICE] I'm working on nothing...");
}
Also used : UserTransaction(com.arjuna.mw.wst11.UserTransaction) Uid(com.arjuna.ats.arjuna.common.Uid) TestApplicationException(org.jboss.as.test.xts.base.TestApplicationException) TransactionManager(com.arjuna.mw.wst11.TransactionManager) TestApplicationException(org.jboss.as.test.xts.base.TestApplicationException) UnknownTransactionException(com.arjuna.wst.UnknownTransactionException) WrongStateException(com.arjuna.wst.WrongStateException)

Example 2 with TestApplicationException

use of org.jboss.as.test.xts.base.TestApplicationException 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 3 with TestApplicationException

use of org.jboss.as.test.xts.base.TestApplicationException 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)

Aggregations

Uid (com.arjuna.ats.arjuna.common.Uid)3 TestApplicationException (org.jboss.as.test.xts.base.TestApplicationException)3 BusinessActivityManager (com.arjuna.mw.wst11.BusinessActivityManager)2 SystemException (com.arjuna.wst.SystemException)2 BAParticipantManager (com.arjuna.wst11.BAParticipantManager)2 TransactionManager (com.arjuna.mw.wst11.TransactionManager)1 UserTransaction (com.arjuna.mw.wst11.UserTransaction)1 UnknownTransactionException (com.arjuna.wst.UnknownTransactionException)1 WrongStateException (com.arjuna.wst.WrongStateException)1