Search in sources :

Example 1 with IOCallback

use of org.apache.activemq.artemis.core.io.IOCallback in project activemq-artemis by apache.

the class PostOfficeImpl method schedulePageDelivery.

/**
 * This will kick a delivery async on the queue, so the queue may have a chance to depage messages
 *
 * @param tx
 * @param entry
 */
private void schedulePageDelivery(Transaction tx, Map.Entry<SimpleString, RouteContextList> entry) {
    if (tx != null) {
        PageDelivery delivery = (PageDelivery) tx.getProperty(TransactionPropertyIndexes.PAGE_DELIVERY);
        if (delivery == null) {
            delivery = new PageDelivery();
            tx.putProperty(TransactionPropertyIndexes.PAGE_DELIVERY, delivery);
            tx.addOperation(delivery);
        }
        delivery.addQueues(entry.getValue().getDurableQueues());
        delivery.addQueues(entry.getValue().getNonDurableQueues());
    } else {
        List<Queue> durableQueues = entry.getValue().getDurableQueues();
        List<Queue> nonDurableQueues = entry.getValue().getNonDurableQueues();
        final List<Queue> queues = new ArrayList<>(durableQueues.size() + nonDurableQueues.size());
        queues.addAll(durableQueues);
        queues.addAll(nonDurableQueues);
        storageManager.afterCompleteOperations(new IOCallback() {

            @Override
            public void onError(int errorCode, String errorMessage) {
            }

            @Override
            public void done() {
                for (Queue queue : queues) {
                    // in case of paging, we need to kick asynchronous delivery to try delivering
                    queue.deliverAsync();
                }
            }
        });
    }
}
Also used : ArrayList(java.util.ArrayList) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) Queue(org.apache.activemq.artemis.core.server.Queue) IOCallback(org.apache.activemq.artemis.core.io.IOCallback)

Example 2 with IOCallback

use of org.apache.activemq.artemis.core.io.IOCallback in project activemq-artemis by apache.

the class Redistributor method ackRedistribution.

private void ackRedistribution(final MessageReference reference, final Transaction tx) throws Exception {
    reference.handled();
    queue.acknowledge(tx, reference);
    tx.commit();
    storageManager.afterCompleteOperations(new IOCallback() {

        @Override
        public void onError(final int errorCode, final String errorMessage) {
            ActiveMQServerLogger.LOGGER.ioErrorRedistributing(errorCode, errorMessage);
        }

        @Override
        public void done() {
            execPrompter();
        }
    });
}
Also used : IOCallback(org.apache.activemq.artemis.core.io.IOCallback)

Example 3 with IOCallback

use of org.apache.activemq.artemis.core.io.IOCallback in project activemq-artemis by apache.

the class AMQPSessionCallback method rejectMessage.

private void rejectMessage(Delivery delivery, Symbol errorCondition, String errorMessage) {
    ErrorCondition condition = new ErrorCondition();
    condition.setCondition(errorCondition);
    condition.setDescription(errorMessage);
    Rejected rejected = new Rejected();
    rejected.setError(condition);
    afterIO(new IOCallback() {

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

        @Override
        public void onError(int errorCode, String errorMessage) {
        }
    });
}
Also used : ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) Rejected(org.apache.qpid.proton.amqp.messaging.Rejected) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) IOCallback(org.apache.activemq.artemis.core.io.IOCallback)

Example 4 with IOCallback

use of org.apache.activemq.artemis.core.io.IOCallback in project activemq-artemis by apache.

the class AMQPSessionCallback method serverSend.

private void serverSend(final Transaction transaction, final Message message, final Delivery delivery, final Receiver receiver) throws Exception {
    message.setConnectionID(receiver.getSession().getConnection().getRemoteContainer());
    invokeIncoming((AMQPMessage) message, (ActiveMQProtonRemotingConnection) transportConnection.getProtocolConnection());
    serverSession.send(transaction, message, false, false);
    afterIO(new IOCallback() {

        @Override
        public void done() {
            connection.lock();
            try {
                if (delivery.getRemoteState() instanceof TransactionalState) {
                    TransactionalState txAccepted = new TransactionalState();
                    txAccepted.setOutcome(Accepted.getInstance());
                    txAccepted.setTxnId(((TransactionalState) delivery.getRemoteState()).getTxnId());
                    delivery.disposition(txAccepted);
                } else {
                    delivery.disposition(Accepted.getInstance());
                }
                delivery.settle();
            } finally {
                connection.unlock();
            }
            connection.flush();
        }

        @Override
        public void onError(int errorCode, String errorMessage) {
            connection.lock();
            try {
                receiver.setCondition(new ErrorCondition(AmqpError.ILLEGAL_STATE, errorCode + ":" + errorMessage));
                connection.flush();
            } finally {
                connection.unlock();
            }
        }
    });
}
Also used : ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) IOCallback(org.apache.activemq.artemis.core.io.IOCallback) TransactionalState(org.apache.qpid.proton.amqp.transaction.TransactionalState)

Example 5 with IOCallback

use of org.apache.activemq.artemis.core.io.IOCallback 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)

Aggregations

IOCallback (org.apache.activemq.artemis.core.io.IOCallback)27 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)12 Test (org.junit.Test)9 CountDownLatch (java.util.concurrent.CountDownLatch)7 ArrayList (java.util.ArrayList)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 ByteBuffer (java.nio.ByteBuffer)4 ExecutorService (java.util.concurrent.ExecutorService)3 ActiveMQIllegalStateException (org.apache.activemq.artemis.api.core.ActiveMQIllegalStateException)3 OperationContext (org.apache.activemq.artemis.core.persistence.OperationContext)3 JournalStorageManager (org.apache.activemq.artemis.core.persistence.impl.journal.JournalStorageManager)3 OperationContextImpl (org.apache.activemq.artemis.core.persistence.impl.journal.OperationContextImpl)3 IOException (java.io.IOException)2 ActiveMQQueueExistsException (org.apache.activemq.artemis.api.core.ActiveMQQueueExistsException)2 Message (org.apache.activemq.artemis.api.core.Message)2 Journal (org.apache.activemq.artemis.core.journal.Journal)2 SimpleWaitIOCallback (org.apache.activemq.artemis.core.journal.impl.SimpleWaitIOCallback)2 StorageManager (org.apache.activemq.artemis.core.persistence.StorageManager)2 ReplicatedJournal (org.apache.activemq.artemis.core.replication.ReplicatedJournal)2 MessageReference (org.apache.activemq.artemis.core.server.MessageReference)2