use of org.mule.runtime.api.notification.TransactionNotification in project mule by mulesoft.
the class TransactionNotificationsTestCase method testTransactionNotifications.
@Test
public void testTransactionNotifications() throws Exception {
final CountDownLatch latch = new CountDownLatch(3);
// the code is simple and deceptive :) The trick is this dummy transaction is handled by
// a global TransactionCoordination instance, which binds it to the current thread.
Transaction transaction = new DummyTransaction(muleContext);
((MuleContextWithRegistries) muleContext).getRegistry().lookupObject(NotificationListenerRegistry.class).registerListener(new TransactionNotificationListener<TransactionNotification>() {
@Override
public boolean isBlocking() {
return false;
}
@Override
public void onNotification(TransactionNotification notification) {
if (new IntegerAction(TRANSACTION_BEGAN).equals(notification.getAction())) {
assertEquals("begin", notification.getActionName());
latch.countDown();
} else if (new IntegerAction(TRANSACTION_COMMITTED).equals(notification.getAction())) {
assertEquals("commit", notification.getActionName());
latch.countDown();
} else if (new IntegerAction(TRANSACTION_ROLLEDBACK).equals(notification.getAction())) {
assertEquals("rollback", notification.getActionName());
latch.countDown();
}
}
}, notification -> transaction.getId().equals(notification.getResourceIdentifier()));
transaction.begin();
transaction.commit();
transaction.rollback();
// Wait for the notifcation event to be fired as they are queued
latch.await(2000, MILLISECONDS);
assertEquals("There are still some notifications left unfired.", 0, latch.getCount());
}
use of org.mule.runtime.api.notification.TransactionNotification in project mule by mulesoft.
the class AbstractTransaction method commit.
@Override
public void commit() throws TransactionException {
try {
logger.debug("Committing transaction " + identityHashCode(this));
if (isRollbackOnly()) {
throw new IllegalTransactionStateException(transactionMarkedForRollback());
}
doCommit();
fireNotification(new TransactionNotification(getId(), TRANSACTION_COMMITTED, getApplicationName()));
} finally {
TransactionCoordination.getInstance().unbindTransaction(this);
}
}
use of org.mule.runtime.api.notification.TransactionNotification in project mule by mulesoft.
the class AbstractTransaction method rollback.
@Override
public void rollback() throws TransactionException {
try {
logger.debug("Rolling back transaction " + identityHashCode(this));
setRollbackOnly();
doRollback();
fireNotification(new TransactionNotification(getId(), TRANSACTION_ROLLEDBACK, getApplicationName()));
} finally {
unbindTransaction();
}
}
use of org.mule.runtime.api.notification.TransactionNotification in project mule by mulesoft.
the class AbstractTransaction method begin.
@Override
public void begin() throws TransactionException {
logger.debug("Beginning transaction " + identityHashCode(this));
doBegin();
TransactionCoordination.getInstance().bindTransaction(this);
fireNotification(new TransactionNotification(getId(), TRANSACTION_BEGAN, getApplicationName()));
}
Aggregations