Search in sources :

Example 1 with PendingTask

use of org.apache.activemq.artemis.utils.PendingTask in project activemq-artemis by apache.

the class StompSession method sendMessage.

@Override
public int sendMessage(MessageReference ref, Message serverMessage, final ServerConsumer consumer, int deliveryCount) {
    ICoreMessage coreMessage = serverMessage.toCore();
    LargeServerMessageImpl largeMessage = null;
    ICoreMessage newServerMessage = serverMessage.toCore();
    try {
        StompSubscription subscription = subscriptions.get(consumer.getID());
        // subscription might be null if the consumer was closed
        if (subscription == null)
            return 0;
        StompFrame frame;
        ActiveMQBuffer buffer = coreMessage.getDataBuffer();
        frame = connection.createStompMessage(newServerMessage, buffer, subscription, deliveryCount);
        int length = frame.getEncodedSize();
        if (subscription.getAck().equals(Stomp.Headers.Subscribe.AckModeValues.AUTO)) {
            if (manager.send(connection, frame)) {
                final long messageID = newServerMessage.getMessageID();
                final long consumerID = consumer.getID();
                // this will be called after the delivery is complete
                // we can't call session.ack within the delivery
                // as it could dead lock.
                afterDeliveryTasks.offer(new PendingTask() {

                    @Override
                    public void run() throws Exception {
                        // we ack and commit only if the send is successful
                        session.acknowledge(consumerID, messageID);
                        session.commit();
                    }
                });
            }
        } else {
            messagesToAck.put(newServerMessage.getMessageID(), new Pair<>(consumer.getID(), length));
            // Must send AFTER adding to messagesToAck - or could get acked from client BEFORE it's been added!
            manager.send(connection, frame);
        }
        return length;
    } catch (Exception e) {
        if (ActiveMQStompProtocolLogger.LOGGER.isDebugEnabled()) {
            ActiveMQStompProtocolLogger.LOGGER.debug(e);
        }
        return 0;
    } finally {
        if (largeMessage != null) {
            largeMessage.releaseResources();
            largeMessage = null;
        }
    }
}
Also used : ICoreMessage(org.apache.activemq.artemis.api.core.ICoreMessage) LargeServerMessageImpl(org.apache.activemq.artemis.core.persistence.impl.journal.LargeServerMessageImpl) PendingTask(org.apache.activemq.artemis.utils.PendingTask) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer)

Aggregations

ActiveMQBuffer (org.apache.activemq.artemis.api.core.ActiveMQBuffer)1 ICoreMessage (org.apache.activemq.artemis.api.core.ICoreMessage)1 LargeServerMessageImpl (org.apache.activemq.artemis.core.persistence.impl.journal.LargeServerMessageImpl)1 PendingTask (org.apache.activemq.artemis.utils.PendingTask)1