Search in sources :

Example 6 with TransactionalState

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

the class AmqpTransactionTest method testSentTransactionalMessageIsSettleWithTransactionalDisposition.

@Test(timeout = 30000)
public void testSentTransactionalMessageIsSettleWithTransactionalDisposition() throws Exception {
    AmqpClient client = createAmqpClient();
    AmqpConnection connection = addConnection(client.connect());
    AmqpSession session = connection.createSession();
    assertNotNull(session);
    AmqpSender sender = session.createSender(getQueueName());
    sender.setStateInspector(new AmqpValidator() {

        @Override
        public void inspectDeliveryUpdate(Sender sender, Delivery delivery) {
            if (delivery.remotelySettled()) {
                DeliveryState state = delivery.getRemoteState();
                if (state instanceof TransactionalState) {
                    LOG.debug("Remote settled with TX state: {}", state);
                } else {
                    LOG.warn("Remote settled with non-TX state: {}", state);
                    markAsInvalid("Remote did not settled with TransactionState.");
                }
            }
        }
    });
    session.begin();
    assertTrue(session.isInTransaction());
    AmqpMessage message = new AmqpMessage();
    message.setText("Test-Message");
    sender.send(message);
    session.commit();
    sender.getStateInspector().assertValid();
    connection.close();
}
Also used : AmqpSender(org.apache.activemq.transport.amqp.client.AmqpSender) Sender(org.apache.qpid.proton.engine.Sender) AmqpConnection(org.apache.activemq.transport.amqp.client.AmqpConnection) AmqpSession(org.apache.activemq.transport.amqp.client.AmqpSession) DeliveryState(org.apache.qpid.proton.amqp.transport.DeliveryState) AmqpClient(org.apache.activemq.transport.amqp.client.AmqpClient) Delivery(org.apache.qpid.proton.engine.Delivery) AmqpSender(org.apache.activemq.transport.amqp.client.AmqpSender) AmqpMessage(org.apache.activemq.transport.amqp.client.AmqpMessage) AmqpValidator(org.apache.activemq.transport.amqp.client.AmqpValidator) TransactionalState(org.apache.qpid.proton.amqp.transaction.TransactionalState) Test(org.junit.Test)

Example 7 with TransactionalState

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

the class AmqpSender method processDeliveryUpdates.

@Override
public void processDeliveryUpdates(AmqpConnection connection, Delivery updated) throws IOException {
    List<Delivery> toRemove = new ArrayList<>();
    for (Delivery delivery : pending) {
        DeliveryState state = delivery.getRemoteState();
        if (state == null) {
            continue;
        }
        doDeliveryUpdateInspection(delivery);
        Outcome outcome = null;
        if (state instanceof TransactionalState) {
            LOG.trace("State of delivery is Transactional, retrieving outcome: {}", state);
            outcome = ((TransactionalState) state).getOutcome();
        } else if (state instanceof Outcome) {
            outcome = (Outcome) state;
        } else {
            LOG.warn("Message send updated with unsupported state: {}", state);
            outcome = null;
        }
        AsyncResult request = (AsyncResult) delivery.getContext();
        Exception deliveryError = null;
        if (outcome instanceof Accepted) {
            LOG.trace("Outcome of delivery was accepted: {}", delivery);
            if (request != null && !request.isComplete()) {
                request.onSuccess();
            }
        } else if (outcome instanceof Rejected) {
            LOG.trace("Outcome of delivery was rejected: {}", delivery);
            ErrorCondition remoteError = ((Rejected) outcome).getError();
            if (remoteError == null) {
                remoteError = getEndpoint().getRemoteCondition();
            }
            deliveryError = AmqpSupport.convertToException(remoteError);
        } else if (outcome instanceof Released) {
            LOG.trace("Outcome of delivery was released: {}", delivery);
            deliveryError = new IOException("Delivery failed: released by receiver");
        } else if (outcome instanceof Modified) {
            LOG.trace("Outcome of delivery was modified: {}", delivery);
            deliveryError = new IOException("Delivery failed: failure at remote");
        }
        if (deliveryError != null) {
            if (request != null && !request.isComplete()) {
                request.onFailure(deliveryError);
            } else {
                connection.fireClientException(deliveryError);
            }
        }
        tagGenerator.returnTag(delivery.getTag());
        delivery.settle();
        toRemove.add(delivery);
    }
    pending.removeAll(toRemove);
}
Also used : Released(org.apache.qpid.proton.amqp.messaging.Released) Modified(org.apache.qpid.proton.amqp.messaging.Modified) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) ArrayList(java.util.ArrayList) Rejected(org.apache.qpid.proton.amqp.messaging.Rejected) IOException(java.io.IOException) InvalidDestinationException(javax.jms.InvalidDestinationException) IOException(java.io.IOException) Accepted(org.apache.qpid.proton.amqp.messaging.Accepted) TransactionalState(org.apache.qpid.proton.amqp.transaction.TransactionalState) DeliveryState(org.apache.qpid.proton.amqp.transport.DeliveryState) Outcome(org.apache.qpid.proton.amqp.messaging.Outcome) Delivery(org.apache.qpid.proton.engine.Delivery) AsyncResult(org.apache.activemq.transport.amqp.client.util.AsyncResult)

Example 8 with TransactionalState

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

the class ProtonServerSenderContext method onMessage.

@Override
public void onMessage(Delivery delivery) throws ActiveMQAMQPException {
    if (closed) {
        return;
    }
    OperationContext oldContext = sessionSPI.recoverContext();
    try {
        Message message = ((MessageReference) delivery.getContext()).getMessage();
        boolean preSettle = sender.getRemoteSenderSettleMode() == SenderSettleMode.SETTLED;
        DeliveryState remoteState;
        connection.lock();
        try {
            remoteState = delivery.getRemoteState();
        } finally {
            connection.unlock();
        }
        boolean settleImmediate = true;
        if (remoteState instanceof Accepted) {
            // acking again would show an exception but would have no negative effect but best to handle anyway.
            if (delivery.isSettled()) {
                return;
            }
            // from dealer, a perf hit but a must
            try {
                sessionSPI.ack(null, brokerConsumer, message);
            } catch (Exception e) {
                log.warn(e.toString(), e);
                throw ActiveMQAMQPProtocolMessageBundle.BUNDLE.errorAcknowledgingMessage(message.toString(), e.getMessage());
            }
        } else if (remoteState instanceof TransactionalState) {
            // When the message arrives with a TransactionState disposition the ack should
            // enlist the message into the transaction associated with the given txn ID.
            TransactionalState txState = (TransactionalState) remoteState;
            ProtonTransactionImpl tx = (ProtonTransactionImpl) this.sessionSPI.getTransaction(txState.getTxnId(), false);
            if (txState.getOutcome() != null) {
                settleImmediate = false;
                Outcome outcome = txState.getOutcome();
                if (outcome instanceof Accepted) {
                    if (!delivery.remotelySettled()) {
                        TransactionalState txAccepted = new TransactionalState();
                        txAccepted.setOutcome(Accepted.getInstance());
                        txAccepted.setTxnId(txState.getTxnId());
                        connection.lock();
                        try {
                            delivery.disposition(txAccepted);
                        } finally {
                            connection.unlock();
                        }
                    }
                    // from dealer, a perf hit but a must
                    try {
                        sessionSPI.ack(tx, brokerConsumer, message);
                        tx.addDelivery(delivery, this);
                    } catch (Exception e) {
                        throw ActiveMQAMQPProtocolMessageBundle.BUNDLE.errorAcknowledgingMessage(message.toString(), e.getMessage());
                    }
                }
            }
        } else if (remoteState instanceof Released) {
            try {
                sessionSPI.cancel(brokerConsumer, message, false);
            } catch (Exception e) {
                throw ActiveMQAMQPProtocolMessageBundle.BUNDLE.errorCancellingMessage(message.toString(), e.getMessage());
            }
        } else if (remoteState instanceof Rejected) {
            try {
                sessionSPI.reject(brokerConsumer, message);
            } catch (Exception e) {
                throw ActiveMQAMQPProtocolMessageBundle.BUNDLE.errorCancellingMessage(message.toString(), e.getMessage());
            }
        } else if (remoteState instanceof Modified) {
            try {
                Modified modification = (Modified) remoteState;
                if (Boolean.TRUE.equals(modification.getUndeliverableHere())) {
                    message.rejectConsumer(brokerConsumer.sequentialID());
                }
                if (Boolean.TRUE.equals(modification.getDeliveryFailed())) {
                    sessionSPI.cancel(brokerConsumer, message, true);
                } else {
                    sessionSPI.cancel(brokerConsumer, message, false);
                }
            } catch (Exception e) {
                throw ActiveMQAMQPProtocolMessageBundle.BUNDLE.errorCancellingMessage(message.toString(), e.getMessage());
            }
        } else {
            log.debug("Received null or unknown disposition for delivery update: " + remoteState);
            return;
        }
        if (!preSettle) {
            protonSession.replaceTag(delivery.getTag());
        }
        if (settleImmediate) {
            settle(delivery);
        }
    } finally {
        sessionSPI.afterIO(new IOCallback() {

            @Override
            public void done() {
                connection.flush();
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                connection.flush();
            }
        });
        sessionSPI.resetContext(oldContext);
    }
}
Also used : OperationContext(org.apache.activemq.artemis.core.persistence.OperationContext) Released(org.apache.qpid.proton.amqp.messaging.Released) Modified(org.apache.qpid.proton.amqp.messaging.Modified) AMQPMessage(org.apache.activemq.artemis.protocol.amqp.broker.AMQPMessage) Message(org.apache.activemq.artemis.api.core.Message) Rejected(org.apache.qpid.proton.amqp.messaging.Rejected) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) MessageReference(org.apache.activemq.artemis.core.server.MessageReference) IOCallback(org.apache.activemq.artemis.core.io.IOCallback) Accepted(org.apache.qpid.proton.amqp.messaging.Accepted) ActiveMQAMQPNotFoundException(org.apache.activemq.artemis.protocol.amqp.exceptions.ActiveMQAMQPNotFoundException) ActiveMQAMQPException(org.apache.activemq.artemis.protocol.amqp.exceptions.ActiveMQAMQPException) FilterException(org.apache.activemq.artemis.selector.filter.FilterException) ActiveMQAMQPInternalErrorException(org.apache.activemq.artemis.protocol.amqp.exceptions.ActiveMQAMQPInternalErrorException) ActiveMQQueueExistsException(org.apache.activemq.artemis.api.core.ActiveMQQueueExistsException) ActiveMQAMQPResourceLimitExceededException(org.apache.activemq.artemis.protocol.amqp.exceptions.ActiveMQAMQPResourceLimitExceededException) ActiveMQAMQPIllegalStateException(org.apache.activemq.artemis.protocol.amqp.exceptions.ActiveMQAMQPIllegalStateException) ActiveMQSecurityException(org.apache.activemq.artemis.api.core.ActiveMQSecurityException) TransactionalState(org.apache.qpid.proton.amqp.transaction.TransactionalState) DeliveryState(org.apache.qpid.proton.amqp.transport.DeliveryState) Outcome(org.apache.qpid.proton.amqp.messaging.Outcome) ProtonTransactionImpl(org.apache.activemq.artemis.protocol.amqp.proton.transaction.ProtonTransactionImpl)

Aggregations

TransactionalState (org.apache.qpid.proton.amqp.transaction.TransactionalState)8 Delivery (org.apache.qpid.proton.engine.Delivery)4 Accepted (org.apache.qpid.proton.amqp.messaging.Accepted)3 Rejected (org.apache.qpid.proton.amqp.messaging.Rejected)3 DeliveryState (org.apache.qpid.proton.amqp.transport.DeliveryState)3 ErrorCondition (org.apache.qpid.proton.amqp.transport.ErrorCondition)3 IOException (java.io.IOException)2 InvalidDestinationException (javax.jms.InvalidDestinationException)2 ActiveMQSecurityException (org.apache.activemq.artemis.api.core.ActiveMQSecurityException)2 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)2 IOCallback (org.apache.activemq.artemis.core.io.IOCallback)2 ActiveMQAMQPException (org.apache.activemq.artemis.protocol.amqp.exceptions.ActiveMQAMQPException)2 ActiveMQAMQPInternalErrorException (org.apache.activemq.artemis.protocol.amqp.exceptions.ActiveMQAMQPInternalErrorException)2 ActiveMQAMQPNotFoundException (org.apache.activemq.artemis.protocol.amqp.exceptions.ActiveMQAMQPNotFoundException)2 AmqpClient (org.apache.activemq.transport.amqp.client.AmqpClient)2 AmqpConnection (org.apache.activemq.transport.amqp.client.AmqpConnection)2 AmqpMessage (org.apache.activemq.transport.amqp.client.AmqpMessage)2 AmqpSender (org.apache.activemq.transport.amqp.client.AmqpSender)2 AmqpSession (org.apache.activemq.transport.amqp.client.AmqpSession)2 AmqpValidator (org.apache.activemq.transport.amqp.client.AmqpValidator)2