Search in sources :

Example 1 with Declare

use of org.apache.qpid.proton.amqp.transaction.Declare in project activemq-artemis by apache.

the class ProtonTransactionHandler method onMessage.

@Override
public void onMessage(Delivery delivery) throws ActiveMQAMQPException {
    final Receiver receiver;
    try {
        receiver = ((Receiver) delivery.getLink());
        if (!delivery.isReadable()) {
            return;
        }
        ByteBuffer buffer;
        MessageImpl msg;
        connection.lock();
        try {
            // transaction declare and discahrge operations.
            if (receiver.getCredit() < amqpLowMark) {
                receiver.flow(amqpCredit);
            }
            // the incoming request is to big just use a scratch buffer.
            if (delivery.available() > DECODE_BUFFER.capacity()) {
                buffer = ByteBuffer.allocate(delivery.available());
            } else {
                buffer = (ByteBuffer) DECODE_BUFFER.clear();
            }
            // Update Buffer for the next incoming command.
            buffer.limit(receiver.recv(buffer.array(), buffer.arrayOffset(), buffer.capacity()));
            receiver.advance();
            msg = decodeMessage(buffer);
        } finally {
            connection.unlock();
        }
        Object action = ((AmqpValue) msg.getBody()).getValue();
        if (action instanceof Declare) {
            Binary txID = sessionSPI.newTransaction();
            Declared declared = new Declared();
            declared.setTxnId(txID);
            IOCallback ioAction = new IOCallback() {

                @Override
                public void done() {
                    connection.lock();
                    try {
                        delivery.settle();
                        delivery.disposition(declared);
                    } finally {
                        connection.unlock();
                        connection.flush();
                    }
                }

                @Override
                public void onError(int errorCode, String errorMessage) {
                }
            };
            sessionSPI.afterIO(ioAction);
        } else if (action instanceof Discharge) {
            Discharge discharge = (Discharge) action;
            Binary txID = discharge.getTxnId();
            ProtonTransactionImpl tx = (ProtonTransactionImpl) sessionSPI.getTransaction(txID, true);
            tx.discharge();
            IOCallback ioAction = new IOCallback() {

                @Override
                public void done() {
                    connection.lock();
                    try {
                        delivery.settle();
                        delivery.disposition(new Accepted());
                    } finally {
                        connection.unlock();
                        connection.flush();
                    }
                }

                @Override
                public void onError(int errorCode, String errorMessage) {
                }
            };
            if (discharge.getFail()) {
                sessionSPI.withinContext(() -> tx.rollback());
                sessionSPI.afterIO(ioAction);
            } else {
                sessionSPI.withinContext(() -> tx.commit());
                sessionSPI.afterIO(ioAction);
            }
        }
    } catch (ActiveMQAMQPException amqpE) {
        log.warn(amqpE.getMessage(), amqpE);
        connection.lock();
        try {
            delivery.settle();
            delivery.disposition(createRejected(amqpE.getAmqpError(), amqpE.getMessage()));
        } finally {
            connection.unlock();
        }
        connection.flush();
    } catch (Throwable e) {
        log.warn(e.getMessage(), e);
        connection.lock();
        try {
            delivery.settle();
            delivery.disposition(createRejected(Symbol.getSymbol("failed"), e.getMessage()));
        } finally {
            connection.unlock();
        }
        connection.flush();
    }
}
Also used : Receiver(org.apache.qpid.proton.engine.Receiver) Declare(org.apache.qpid.proton.amqp.transaction.Declare) ByteBuffer(java.nio.ByteBuffer) IOCallback(org.apache.activemq.artemis.core.io.IOCallback) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) Declared(org.apache.qpid.proton.amqp.transaction.Declared) Discharge(org.apache.qpid.proton.amqp.transaction.Discharge) Accepted(org.apache.qpid.proton.amqp.messaging.Accepted) ActiveMQAMQPException(org.apache.activemq.artemis.protocol.amqp.exceptions.ActiveMQAMQPException) Binary(org.apache.qpid.proton.amqp.Binary) MessageImpl(org.apache.qpid.proton.message.impl.MessageImpl)

Example 2 with Declare

use of org.apache.qpid.proton.amqp.transaction.Declare in project activemq-artemis by apache.

the class AmqpTransactionCoordinator method declare.

public void declare(AmqpTransactionId txId, AsyncResult request) throws Exception {
    if (txId.getRemoteTxId() != null) {
        throw new IllegalStateException("Declare called while a TX is still Active.");
    }
    if (isClosed()) {
        request.onFailure(new JMSException("Cannot start new transaction: Coordinator remotely closed"));
        return;
    }
    Message message = Message.Factory.create();
    Declare declare = new Declare();
    message.setBody(new AmqpValue(declare));
    Delivery pendingDelivery = getEndpoint().delivery(tagGenerator.getNextTag());
    pendingDelivery.setContext(txId);
    // Store away for completion
    pendingDeliveries.add(pendingDelivery);
    pendingRequests.put(txId, request);
    sendTxCommand(message);
}
Also used : IllegalStateException(javax.jms.IllegalStateException) Message(org.apache.qpid.proton.message.Message) JMSException(javax.jms.JMSException) Delivery(org.apache.qpid.proton.engine.Delivery) Declare(org.apache.qpid.proton.amqp.transaction.Declare) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue)

Aggregations

AmqpValue (org.apache.qpid.proton.amqp.messaging.AmqpValue)2 Declare (org.apache.qpid.proton.amqp.transaction.Declare)2 ByteBuffer (java.nio.ByteBuffer)1 IllegalStateException (javax.jms.IllegalStateException)1 JMSException (javax.jms.JMSException)1 IOCallback (org.apache.activemq.artemis.core.io.IOCallback)1 ActiveMQAMQPException (org.apache.activemq.artemis.protocol.amqp.exceptions.ActiveMQAMQPException)1 Binary (org.apache.qpid.proton.amqp.Binary)1 Accepted (org.apache.qpid.proton.amqp.messaging.Accepted)1 Declared (org.apache.qpid.proton.amqp.transaction.Declared)1 Discharge (org.apache.qpid.proton.amqp.transaction.Discharge)1 Delivery (org.apache.qpid.proton.engine.Delivery)1 Receiver (org.apache.qpid.proton.engine.Receiver)1 Message (org.apache.qpid.proton.message.Message)1 MessageImpl (org.apache.qpid.proton.message.impl.MessageImpl)1