Search in sources :

Example 1 with TransactionContext

use of javax.resource.spi.work.TransactionContext in project wildfly by wildfly.

the class TransactionInflowResourceAdapter method endpointActivation.

public void endpointActivation(MessageEndpointFactory endpointFactory, ActivationSpec spec) throws ResourceException {
    Xid xid = TransactionInflowXid.getUniqueXid(42);
    TransactionInflowWork work = new TransactionInflowWork(endpointFactory, MSG);
    TransactionContext txnCtx = new TransactionContext();
    txnCtx.setXid(xid);
    TransactionInflowWorkListener workListener = new TransactionInflowWorkListener();
    try {
        bootstrapContext.getWorkManager().startWork(work, WorkManager.IMMEDIATE, txnCtx, workListener);
    } catch (WorkException e) {
        throw new IllegalStateException("Can't start work " + work + " with txn " + txnCtx);
    }
    // start Work blocks until the execution starts but not until its completion
    // timeout 10 seconds
    int timeout = TimeoutUtil.adjust(10_000);
    long start = System.currentTimeMillis();
    while (!workListener.isCompleted() && (System.currentTimeMillis() - start < timeout)) {
        // active waiting
        Thread.yield();
    }
    if (!workListener.isCompleted())
        throw new IllegalStateException("Work " + work + " of xid " + xid + " does not finish.");
    try {
        bootstrapContext.getXATerminator().prepare(xid);
        // depends on value in spec we commit or roll-back
        TransactionInflowRaSpec activationSpec = (TransactionInflowRaSpec) spec;
        if (activationSpec.getAction().equals(ACTION_COMMIT)) {
            bootstrapContext.getXATerminator().commit(xid, false);
        } else if (activationSpec.getAction().equals(ACTION_ROLLBACK)) {
            bootstrapContext.getXATerminator().rollback(xid);
        } else {
            new IllegalStateException("Spec '" + activationSpec + "' defines unknown action");
        }
    } catch (XAException xae) {
        throw new IllegalStateException("Can't process prepare/commit/rollback calls for xid: " + xid, xae);
    }
}
Also used : Xid(javax.transaction.xa.Xid) XAException(javax.transaction.xa.XAException) TransactionContext(javax.resource.spi.work.TransactionContext) WorkException(javax.resource.spi.work.WorkException)

Aggregations

TransactionContext (javax.resource.spi.work.TransactionContext)1 WorkException (javax.resource.spi.work.WorkException)1 XAException (javax.transaction.xa.XAException)1 Xid (javax.transaction.xa.Xid)1