Search in sources :

Example 1 with ClientFutureSynchronization

use of org.apache.activemq.transport.amqp.client.util.ClientFutureSynchronization in project activemq-artemis by apache.

the class AmqpTransactionContext method rollback.

/**
 * Rollback any transacted work performed under the current transaction.
 *
 * @throws Exception if an error occurs during the rollback operation.
 */
public void rollback() throws Exception {
    if (transactionId == null) {
        throw new IllegalStateException("Rollback called with no active Transaction.");
    }
    preRollback();
    final ClientFuture request = new ClientFuture(new ClientFutureSynchronization() {

        @Override
        public void onPendingSuccess() {
            transactionId = null;
            postRollback();
        }

        @Override
        public void onPendingFailure(Throwable cause) {
            transactionId = null;
            postRollback();
        }
    });
    LOG.debug("Rollback on TX[{}] initiated", transactionId);
    session.getScheduler().execute(new Runnable() {

        @Override
        public void run() {
            try {
                LOG.info("Attempting to roll back TX:[{}]", transactionId);
                coordinator.discharge(transactionId, request, false);
                session.pumpToProtonTransport(request);
            } catch (Exception e) {
                request.onFailure(e);
            }
        }
    });
    request.sync();
}
Also used : ClientFutureSynchronization(org.apache.activemq.transport.amqp.client.util.ClientFutureSynchronization) ClientFuture(org.apache.activemq.transport.amqp.client.util.ClientFuture) IOException(java.io.IOException)

Example 2 with ClientFutureSynchronization

use of org.apache.activemq.transport.amqp.client.util.ClientFutureSynchronization in project activemq-artemis by apache.

the class AmqpTransactionContext method commit.

/**
 * Commit this transaction which then ends the lifetime of the transacted operation.
 *
 * @throws Exception if an error occurs while performing the commit
 */
public void commit() throws Exception {
    if (transactionId == null) {
        throw new IllegalStateException("Commit called with no active Transaction.");
    }
    preCommit();
    final ClientFuture request = new ClientFuture(new ClientFutureSynchronization() {

        @Override
        public void onPendingSuccess() {
            transactionId = null;
            postCommit();
        }

        @Override
        public void onPendingFailure(Throwable cause) {
            transactionId = null;
            postCommit();
        }
    });
    LOG.debug("Commit on TX[{}] initiated", transactionId);
    session.getScheduler().execute(new Runnable() {

        @Override
        public void run() {
            try {
                LOG.info("Attempting to commit TX:[{}]", transactionId);
                coordinator.discharge(transactionId, request, true);
                session.pumpToProtonTransport(request);
            } catch (Exception e) {
                request.onFailure(e);
            }
        }
    });
    request.sync();
}
Also used : ClientFutureSynchronization(org.apache.activemq.transport.amqp.client.util.ClientFutureSynchronization) ClientFuture(org.apache.activemq.transport.amqp.client.util.ClientFuture) IOException(java.io.IOException)

Example 3 with ClientFutureSynchronization

use of org.apache.activemq.transport.amqp.client.util.ClientFutureSynchronization in project activemq-artemis by apache.

the class AmqpTransactionContext method begin.

/**
 * Begins a new transaction scoped to the target session.
 *
 * @throws Exception if an error occurs while starting the transaction.
 */
public void begin() throws Exception {
    if (transactionId != null) {
        throw new IOException("Begin called while a TX is still Active.");
    }
    final AmqpTransactionId txId = session.getConnection().getNextTransactionId();
    final ClientFuture request = new ClientFuture(new ClientFutureSynchronization() {

        @Override
        public void onPendingSuccess() {
            transactionId = txId;
        }

        @Override
        public void onPendingFailure(Throwable cause) {
            transactionId = null;
        }
    });
    LOG.info("Attempting to Begin TX:[{}]", txId);
    session.getScheduler().execute(new Runnable() {

        @Override
        public void run() {
            if (coordinator == null || coordinator.isClosed()) {
                LOG.info("Creating new Coordinator for TX:[{}]", txId);
                coordinator = new AmqpTransactionCoordinator(session);
                coordinator.open(new AsyncResult() {

                    @Override
                    public void onSuccess() {
                        try {
                            LOG.info("Attempting to declare TX:[{}]", txId);
                            coordinator.declare(txId, request);
                        } catch (Exception e) {
                            request.onFailure(e);
                        }
                    }

                    @Override
                    public void onFailure(Throwable result) {
                        request.onFailure(result);
                    }

                    @Override
                    public boolean isComplete() {
                        return request.isComplete();
                    }
                });
            } else {
                try {
                    LOG.info("Attempting to declare TX:[{}]", txId);
                    coordinator.declare(txId, request);
                } catch (Exception e) {
                    request.onFailure(e);
                }
            }
            session.pumpToProtonTransport(request);
        }
    });
    request.sync();
}
Also used : ClientFutureSynchronization(org.apache.activemq.transport.amqp.client.util.ClientFutureSynchronization) ClientFuture(org.apache.activemq.transport.amqp.client.util.ClientFuture) IOException(java.io.IOException) AsyncResult(org.apache.activemq.transport.amqp.client.util.AsyncResult) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)3 ClientFuture (org.apache.activemq.transport.amqp.client.util.ClientFuture)3 ClientFutureSynchronization (org.apache.activemq.transport.amqp.client.util.ClientFutureSynchronization)3 AsyncResult (org.apache.activemq.transport.amqp.client.util.AsyncResult)1