use of org.mule.runtime.core.api.execution.ExecutionTemplate in project mule by mulesoft.
the class AbstractTxThreadAssociationTestCase method testNoneXaTransactionSuspendResume.
/**
* NONE action suspends current transaction and begins a new one.
*
* @throws Exception if any error
*/
@Test
public void testNoneXaTransactionSuspendResume() throws Exception {
muleContext.setTransactionManager(tm);
assertNull("There should be no current transaction associated.", tm.getTransaction());
// don't wait for ages, has to be set before TX is begun
tm.setTransactionTimeout(TRANSACTION_TIMEOUT_SECONDS);
// this is one component with a TX always begin
TransactionConfig config = new MuleTransactionConfig(TransactionConfig.ACTION_ALWAYS_BEGIN);
config.setFactory(new XaTransactionFactory());
ExecutionTemplate<Void> executionTemplate = TransactionalExecutionTemplate.createTransactionalExecutionTemplate(muleContext, config);
// and the callee component which should begin new transaction, current must be suspended
final TransactionConfig nestedConfig = new MuleTransactionConfig(TransactionConfig.ACTION_NONE);
nestedConfig.setFactory(new XaTransactionFactory());
// start the call chain
executionTemplate.execute(new ExecutionCallback<Void>() {
@Override
public Void process() throws Exception {
// the callee executes within its own TX template, but uses the same global XA transaction,
// bound to the current thread of execution via a ThreadLocal
ExecutionTemplate<Void> nestedExecutionTemplate = TransactionalExecutionTemplate.createTransactionalExecutionTemplate(muleContext, nestedConfig);
final Transaction firstTx = tm.getTransaction();
assertNotNull(firstTx);
assertEquals(firstTx.getStatus(), Status.STATUS_ACTIVE);
return nestedExecutionTemplate.execute(new ExecutionCallback<Void>() {
@Override
public Void process() throws Exception {
Transaction secondTx = tm.getTransaction();
assertNull(secondTx);
assertEquals(firstTx.getStatus(), Status.STATUS_ACTIVE);
try {
tm.resume(firstTx);
assertEquals(firstTx, tm.getTransaction());
assertEquals(firstTx.getStatus(), Status.STATUS_ACTIVE);
Transaction a = tm.suspend();
assertTrue(a.equals(firstTx));
} catch (Exception e) {
fail("Error: " + e);
}
// do not care about the return really
return null;
}
});
}
});
assertNull("Committing via TX Manager should have disassociated TX from the current thread.", tm.getTransaction());
}
use of org.mule.runtime.core.api.execution.ExecutionTemplate in project mule by mulesoft.
the class AbstractTxThreadAssociationTestCase method testAlwaysBeginXaTransactionSuspendResume.
/**
* AlwaysBegin action suspends current transaction and begins a new one.
*
* @throws Exception if any error
*/
@Test
public void testAlwaysBeginXaTransactionSuspendResume() throws Exception {
muleContext.setTransactionManager(tm);
assertNull("There should be no current transaction associated.", tm.getTransaction());
// don't wait for ages, has to be set before TX is begun
tm.setTransactionTimeout(TRANSACTION_TIMEOUT_SECONDS);
// this is one component with a TX always begin
TransactionConfig config = new MuleTransactionConfig(TransactionConfig.ACTION_ALWAYS_BEGIN);
config.setFactory(new XaTransactionFactory());
ExecutionTemplate<Void> executionTemplate = TransactionalExecutionTemplate.createTransactionalExecutionTemplate(muleContext, config);
// and the callee component which should begin new transaction, current must be suspended
final TransactionConfig nestedConfig = new MuleTransactionConfig(TransactionConfig.ACTION_ALWAYS_BEGIN);
nestedConfig.setFactory(new XaTransactionFactory());
// start the call chain
executionTemplate.execute(new ExecutionCallback<Void>() {
@Override
public Void process() throws Exception {
// the callee executes within its own TX template, but uses the same global XA transaction,
// bound to the current thread of execution via a ThreadLocal
ExecutionTemplate<Void> innerExecutionTemplate = TransactionalExecutionTemplate.createTransactionalExecutionTemplate(muleContext, nestedConfig);
final Transaction firstTx = tm.getTransaction();
assertNotNull(firstTx);
assertEquals(firstTx.getStatus(), Status.STATUS_ACTIVE);
return innerExecutionTemplate.execute(new ExecutionCallback<Void>() {
@Override
public Void process() throws Exception {
Transaction secondTx = tm.getTransaction();
assertNotNull(secondTx);
assertEquals(firstTx.getStatus(), Status.STATUS_ACTIVE);
assertEquals(secondTx.getStatus(), Status.STATUS_ACTIVE);
try {
tm.resume(firstTx);
fail("Second transaction must be active");
} catch (java.lang.IllegalStateException e) {
// expected
// Thrown if the thread is already associated with another transaction.
// Second tx is associated with the current thread
}
try {
Transaction currentTx = tm.suspend();
assertTrue(currentTx.equals(secondTx));
tm.resume(firstTx);
assertEquals(firstTx, tm.getTransaction());
assertEquals(firstTx.getStatus(), Status.STATUS_ACTIVE);
assertEquals(secondTx.getStatus(), Status.STATUS_ACTIVE);
Transaction a = tm.suspend();
assertTrue(a.equals(firstTx));
tm.resume(secondTx);
} catch (Exception e) {
fail("Error: " + e);
}
// do not care about the return really
return null;
}
});
}
});
assertNull("Committing via TX Manager should have disassociated TX from the current thread.", tm.getTransaction());
}
use of org.mule.runtime.core.api.execution.ExecutionTemplate in project mule by mulesoft.
the class AbstractTxThreadAssociationTestCase method testNoNestedTxStarted.
/**
* This is a former TransactionTemplateTestCase. http://mule.mulesoft.org/jira/browse/MULE-1494
*
* @throws Exception in case of any error
*/
@Test
public void testNoNestedTxStarted() throws Exception {
muleContext.setTransactionManager(tm);
assertNull("There should be no current transaction associated.", tm.getTransaction());
// don't wait for ages, has to be set before TX is begun
tm.setTransactionTimeout(TRANSACTION_TIMEOUT_SECONDS);
// this is one service with a TX always begin
TransactionConfig config = new MuleTransactionConfig(TransactionConfig.ACTION_ALWAYS_BEGIN);
config.setFactory(new XaTransactionFactory());
ExecutionTemplate<Void> executionTemplate = TransactionalExecutionTemplate.createTransactionalExecutionTemplate(muleContext, config);
// and the callee service which should join the current XA transaction, not begin a nested one
final TransactionConfig nestedConfig = new MuleTransactionConfig(TransactionConfig.ACTION_BEGIN_OR_JOIN);
nestedConfig.setFactory(new XaTransactionFactory());
// start the call chain
executionTemplate.execute(new ExecutionCallback<Void>() {
@Override
public Void process() throws Exception {
// the callee executes within its own TX template, but uses the same global XA transaction,
// bound to the current thread of execution via a ThreadLocal
ExecutionTemplate<Void> nestedExecutionTemplate = TransactionalExecutionTemplate.createTransactionalExecutionTemplate(muleContext, nestedConfig);
return nestedExecutionTemplate.execute(new ExecutionCallback<Void>() {
@Override
public Void process() throws Exception {
// do not care about the return really
return null;
}
});
}
});
}
use of org.mule.runtime.core.api.execution.ExecutionTemplate in project mule by mulesoft.
the class TryScope method process.
@Override
public CoreEvent process(final CoreEvent event) throws MuleException {
if (nestedChain == null) {
return event;
} else {
ExecutionTemplate<CoreEvent> executionTemplate = createScopeTransactionalExecutionTemplate(muleContext, transactionConfig);
ExecutionCallback<CoreEvent> processingCallback = () -> {
try {
CoreEvent e = processToApply(event, p -> from(p).flatMap(request -> processWithChildContext(request, nestedChain, ofNullable(getLocation()), messagingExceptionHandler)));
return e;
} catch (Exception e) {
throw e;
}
};
try {
return executionTemplate.execute(processingCallback);
} catch (MuleException e) {
throw e;
} catch (Exception e) {
throw new DefaultMuleException(errorInvokingMessageProcessorWithinTransaction(nestedChain, transactionConfig), e);
}
}
}
Aggregations