Search in sources :

Example 1 with WorkException

use of javax.resource.spi.work.WorkException in project teiid by teiid.

the class TransactionServerImpl method start.

/**
 * Global Transaction
 */
public void start(final String threadId, final XidImpl xid, int flags, int timeout, boolean singleTM) throws XATransactionException {
    TransactionContext tc = null;
    switch(flags) {
        case XAResource.TMNOFLAGS:
            {
                try {
                    checkXAState(threadId, xid, false, false);
                    tc = transactions.getOrCreateTransactionContext(threadId);
                    if (tc.getTransactionType() != TransactionContext.Scope.NONE) {
                        throw new XATransactionException(QueryPlugin.Event.TEIID30517, XAException.XAER_PROTO, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30517));
                    }
                    tc.setTransactionTimeout(timeout);
                    tc.setXid(xid);
                    tc.setTransactionType(TransactionContext.Scope.GLOBAL);
                    if (singleTM) {
                        tc.setTransaction(transactionManager.getTransaction());
                        if (tc.getTransaction() == null) {
                            // in theory we could inflow the txn and then change all of the methods to check singleTM off of the context
                            throw new XATransactionException(QueryPlugin.Event.TEIID30590, XAException.XAER_INVAL, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30590));
                        }
                    } else {
                        FutureWork<Transaction> work = new FutureWork<Transaction>(new Callable<Transaction>() {

                            @Override
                            public Transaction call() throws Exception {
                                return transactionManager.getTransaction();
                            }
                        }, 0);
                        workManager.doWork(work, WorkManager.INDEFINITE, tc, null);
                        tc.setTransaction(work.get());
                    }
                } catch (NotSupportedException e) {
                    throw new XATransactionException(QueryPlugin.Event.TEIID30512, XAException.XAER_INVAL, e);
                } catch (WorkException e) {
                    throw new XATransactionException(QueryPlugin.Event.TEIID30512, XAException.XAER_INVAL, e);
                } catch (InterruptedException e) {
                    throw new XATransactionException(QueryPlugin.Event.TEIID30512, XAException.XAER_INVAL, e);
                } catch (ExecutionException e) {
                    throw new XATransactionException(QueryPlugin.Event.TEIID30512, XAException.XAER_INVAL, e);
                } catch (SystemException e) {
                    throw new XATransactionException(QueryPlugin.Event.TEIID30512, XAException.XAER_INVAL, e);
                }
                break;
            }
        case XAResource.TMJOIN:
        case XAResource.TMRESUME:
            {
                tc = checkXAState(threadId, xid, true, false);
                TransactionContext threadContext = transactions.getOrCreateTransactionContext(threadId);
                if (threadContext.getTransactionType() != TransactionContext.Scope.NONE) {
                    throw new XATransactionException(QueryPlugin.Event.TEIID30517, XAException.XAER_PROTO, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30517));
                }
                if (flags == XAResource.TMRESUME && !tc.getSuspendedBy().remove(threadId)) {
                    throw new XATransactionException(QueryPlugin.Event.TEIID30518, XAException.XAER_PROTO, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30518, new Object[] { xid, threadId }));
                }
                break;
            }
        default:
            throw new XATransactionException(QueryPlugin.Event.TEIID30519, XAException.XAER_INVAL, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30519));
    }
    tc.setThreadId(threadId);
    transactions.addTransactionContext(tc);
}
Also used : Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) TransactionContext(org.teiid.dqp.service.TransactionContext) WorkException(javax.resource.spi.work.WorkException) XATransactionException(org.teiid.client.xa.XATransactionException) NotSupportedException(javax.resource.NotSupportedException) ExecutionException(java.util.concurrent.ExecutionException) Callable(java.util.concurrent.Callable)

Example 2 with WorkException

use of javax.resource.spi.work.WorkException in project javaee7-samples by javaee-samples.

the class WatchingThread method invoke.

private void invoke(final MessageEndpoint endpoint, final Method beanClassMethod, final Path path) throws WorkException {
    out.println("Watch thread scheduling endpoint call via workmanager for method: " + beanClassMethod.getName() + " and file" + path.getFileName());
    resourceAdapter.getBootstrapContext().getWorkManager().scheduleWork(new Work() {

        @Override
        public void run() {
            try {
                try {
                    endpoint.beforeDelivery(beanClassMethod);
                    beanClassMethod.invoke(endpoint, path.toFile());
                } finally {
                    endpoint.afterDelivery();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void release() {
        }
    });
}
Also used : Work(javax.resource.spi.work.Work) ClosedWatchServiceException(java.nio.file.ClosedWatchServiceException) WorkException(javax.resource.spi.work.WorkException)

Example 3 with WorkException

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

the class CommandDispatcherTransport method broadcast.

private void broadcast(Command<Void, CommandDispatcherTransport> command) throws WorkException {
    CommandDispatcher<CommandDispatcherTransport> dispatcher = this.dispatcher;
    ExceptionRunnable<WorkException> task = new ExceptionRunnable<WorkException>() {

        @Override
        public void run() throws WorkException {
            try {
                for (Map.Entry<Node, CompletionStage<Void>> entry : dispatcher.executeOnGroup(command).entrySet()) {
                    // Verify that command executed successfully on all nodes
                    try {
                        entry.getValue().toCompletableFuture().join();
                    } catch (CancellationException e) {
                    // Ignore
                    } catch (CompletionException e) {
                        throw new WorkException(e);
                    }
                }
            } catch (CommandDispatcherException e) {
                throw new WorkException(e);
            }
        }
    };
    this.executor.execute(task);
}
Also used : ExceptionRunnable(org.wildfly.common.function.ExceptionRunnable) CancellationException(java.util.concurrent.CancellationException) CommandDispatcherException(org.wildfly.clustering.dispatcher.CommandDispatcherException) WorkException(javax.resource.spi.work.WorkException) Node(org.wildfly.clustering.group.Node) CompletionException(java.util.concurrent.CompletionException) HashMap(java.util.HashMap) Map(java.util.Map) CompletionStage(java.util.concurrent.CompletionStage)

Example 4 with WorkException

use of javax.resource.spi.work.WorkException 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)

Example 5 with WorkException

use of javax.resource.spi.work.WorkException in project Payara by payara.

the class TransactionManagerImpl method recreate.

/**
 * Recreate a transaction based on the Xid. This call causes the calling
 * thread to be associated with the specified transaction.
 *
 * @param xid the Xid object representing a transaction.
 * @param timeout positive, non-zero value for transaction timeout.
 */
public static void recreate(Xid xid, long timeout) throws WorkException {
    // check if xid is valid
    if (xid == null || xid.getFormatId() == 0 || xid.getBranchQualifier() == null || xid.getGlobalTransactionId() == null) {
        WorkException workExc = new WorkCompletedException("Invalid Xid");
        workExc.setErrorCode(WorkException.TX_RECREATE_FAILED);
        throw workExc;
    }
    // has TransactionService been initialized?
    if (!DefaultTransactionService.isActive()) {
        WorkException workExc = new WorkCompletedException("Transaction Manager unavailable");
        workExc.setErrorCode(WorkException.TX_RECREATE_FAILED);
        throw workExc;
    }
    // recreate the transaction
    GlobalTID tid = new GlobalTID(xid);
    try {
        CurrentTransaction.recreate(tid, (int) ((timeout <= 0) ? 0 : timeout));
    } catch (Throwable exc) {
        String errorCode = WorkException.TX_RECREATE_FAILED;
        if (exc instanceof INVALID_TRANSACTION && (((INVALID_TRANSACTION) exc).minor == MinorCode.TX_CONCURRENT_WORK_DISALLOWED)) {
            errorCode = WorkException.TX_CONCURRENT_WORK_DISALLOWED;
        }
        WorkException workExc = new WorkCompletedException(exc);
        workExc.setErrorCode(errorCode);
        throw workExc;
    }
}
Also used : WorkException(javax.resource.spi.work.WorkException) GlobalTID(com.sun.jts.CosTransactions.GlobalTID) WorkCompletedException(javax.resource.spi.work.WorkCompletedException)

Aggregations

WorkException (javax.resource.spi.work.WorkException)12 WorkEvent (javax.resource.spi.work.WorkEvent)4 WorkRejectedException (javax.resource.spi.work.WorkRejectedException)4 CancellationException (java.util.concurrent.CancellationException)2 CompletionException (java.util.concurrent.CompletionException)2 Work (javax.resource.spi.work.Work)2 WorkCompletedException (javax.resource.spi.work.WorkCompletedException)2 TaskRejectedException (org.springframework.core.task.TaskRejectedException)2 TaskTimeoutException (org.springframework.core.task.TaskTimeoutException)2 CommandDispatcherException (org.wildfly.clustering.dispatcher.CommandDispatcherException)2 JavaEETransactionManager (com.sun.enterprise.transaction.api.JavaEETransactionManager)1 GlobalTID (com.sun.jts.CosTransactions.GlobalTID)1 Serializable (java.io.Serializable)1 ClosedWatchServiceException (java.nio.file.ClosedWatchServiceException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Callable (java.util.concurrent.Callable)1 CompletionStage (java.util.concurrent.CompletionStage)1 ExecutionException (java.util.concurrent.ExecutionException)1