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);
}
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() {
}
});
}
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);
}
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);
}
}
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;
}
}
Aggregations