Search in sources :

Example 1 with TransactionNotification

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());
}
Also used : TransactionNotification(org.mule.runtime.api.notification.TransactionNotification) IntegerAction(org.mule.runtime.api.notification.IntegerAction) AbstractSingleResourceTransaction(org.mule.runtime.core.privileged.transaction.AbstractSingleResourceTransaction) MuleContextWithRegistries(org.mule.runtime.core.internal.context.MuleContextWithRegistries) NotificationListenerRegistry(org.mule.runtime.api.notification.NotificationListenerRegistry) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with TransactionNotification

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);
    }
}
Also used : TransactionNotification(org.mule.runtime.api.notification.TransactionNotification) IllegalTransactionStateException(org.mule.runtime.core.privileged.transaction.xa.IllegalTransactionStateException)

Example 3 with TransactionNotification

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();
    }
}
Also used : TransactionNotification(org.mule.runtime.api.notification.TransactionNotification)

Example 4 with TransactionNotification

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()));
}
Also used : TransactionNotification(org.mule.runtime.api.notification.TransactionNotification)

Aggregations

TransactionNotification (org.mule.runtime.api.notification.TransactionNotification)4 CountDownLatch (java.util.concurrent.CountDownLatch)1 Test (org.junit.Test)1 IntegerAction (org.mule.runtime.api.notification.IntegerAction)1 NotificationListenerRegistry (org.mule.runtime.api.notification.NotificationListenerRegistry)1 MuleContextWithRegistries (org.mule.runtime.core.internal.context.MuleContextWithRegistries)1 AbstractSingleResourceTransaction (org.mule.runtime.core.privileged.transaction.AbstractSingleResourceTransaction)1 IllegalTransactionStateException (org.mule.runtime.core.privileged.transaction.xa.IllegalTransactionStateException)1