Search in sources :

Example 6 with MessageReference

use of org.apache.activemq.artemis.core.server.MessageReference in project activemq-artemis by apache.

the class ServerConsumerImpl method removeReferenceByID.

@Override
public synchronized MessageReference removeReferenceByID(final long messageID) throws Exception {
    if (browseOnly) {
        return null;
    }
    synchronized (lock) {
        // But first we need to make sure deliveringRefs isn't empty
        if (deliveringRefs.isEmpty()) {
            return null;
        }
        if (deliveringRefs.peek().getMessage().getMessageID() == messageID) {
            return deliveringRefs.poll();
        }
        Iterator<MessageReference> iter = deliveringRefs.iterator();
        MessageReference ref = null;
        while (iter.hasNext()) {
            MessageReference theRef = iter.next();
            if (theRef.getMessage().getMessageID() == messageID) {
                iter.remove();
                ref = theRef;
                break;
            }
        }
        return ref;
    }
}
Also used : MessageReference(org.apache.activemq.artemis.core.server.MessageReference)

Example 7 with MessageReference

use of org.apache.activemq.artemis.core.server.MessageReference in project activemq-artemis by apache.

the class ServerConsumerImpl method cancelRefs.

@Override
public LinkedList<MessageReference> cancelRefs(final boolean failed, final boolean lastConsumedAsDelivered, final Transaction tx) throws Exception {
    boolean performACK = lastConsumedAsDelivered;
    try {
        LargeMessageDeliverer pendingLargeMessageDeliverer = largeMessageDeliverer;
        if (pendingLargeMessageDeliverer != null) {
            pendingLargeMessageDeliverer.finish();
        }
    } catch (Throwable e) {
        ActiveMQServerLogger.LOGGER.errorResttingLargeMessage(e, largeMessageDeliverer);
    } finally {
        largeMessageDeliverer = null;
    }
    LinkedList<MessageReference> refs = new LinkedList<>();
    synchronized (lock) {
        if (!deliveringRefs.isEmpty()) {
            for (MessageReference ref : deliveringRefs) {
                if (performACK) {
                    ref.acknowledge(tx);
                    performACK = false;
                } else {
                    refs.add(ref);
                    updateDeliveryCountForCanceledRef(ref, failed);
                }
                if (logger.isTraceEnabled()) {
                    logger.trace("ServerConsumerImpl::" + this + " Preparing Cancelling list for messageID = " + ref.getMessage().getMessageID() + ", ref = " + ref);
                }
            }
            deliveringRefs.clear();
        }
    }
    return refs;
}
Also used : MessageReference(org.apache.activemq.artemis.core.server.MessageReference) LinkedList(java.util.LinkedList)

Example 8 with MessageReference

use of org.apache.activemq.artemis.core.server.MessageReference in project activemq-artemis by apache.

the class ServerConsumerImpl method close.

@Override
public void close(final boolean failed) throws Exception {
    if (logger.isTraceEnabled()) {
        logger.trace("ServerConsumerImpl::" + this + " being closed with failed=" + failed, new Exception("trace"));
    }
    if (server.hasBrokerPlugins()) {
        server.callBrokerPlugins(plugin -> plugin.beforeCloseConsumer(this, failed));
    }
    setStarted(false);
    LargeMessageDeliverer del = largeMessageDeliverer;
    if (del != null) {
        del.finish();
    }
    removeItself();
    LinkedList<MessageReference> refs = cancelRefs(failed, false, null);
    Iterator<MessageReference> iter = refs.iterator();
    Transaction tx = new TransactionImpl(storageManager);
    while (iter.hasNext()) {
        MessageReference ref = iter.next();
        if (logger.isTraceEnabled()) {
            logger.trace("ServerConsumerImpl::" + this + " cancelling reference " + ref);
        }
        ref.getQueue().cancel(tx, ref, true);
    }
    tx.rollback();
    messageQueue.recheckRefCount(session.getSessionContext());
    if (!browseOnly) {
        TypedProperties props = new TypedProperties();
        props.putSimpleStringProperty(ManagementHelper.HDR_ADDRESS, binding.getAddress());
        props.putSimpleStringProperty(ManagementHelper.HDR_CLUSTER_NAME, binding.getClusterName());
        props.putSimpleStringProperty(ManagementHelper.HDR_ROUTING_NAME, binding.getRoutingName());
        props.putSimpleStringProperty(ManagementHelper.HDR_FILTERSTRING, filter == null ? null : filter.getFilterString());
        props.putIntProperty(ManagementHelper.HDR_DISTANCE, binding.getDistance());
        props.putIntProperty(ManagementHelper.HDR_CONSUMER_COUNT, messageQueue.getConsumerCount());
        // HORNETQ-946
        props.putSimpleStringProperty(ManagementHelper.HDR_USER, SimpleString.toSimpleString(session.getUsername()));
        props.putSimpleStringProperty(ManagementHelper.HDR_REMOTE_ADDRESS, SimpleString.toSimpleString(((ServerSessionImpl) session).getRemotingConnection().getRemoteAddress()));
        props.putSimpleStringProperty(ManagementHelper.HDR_SESSION_NAME, SimpleString.toSimpleString(session.getName()));
        Notification notification = new Notification(null, CoreNotificationType.CONSUMER_CLOSED, props);
        managementService.sendNotification(notification);
    }
    if (server.hasBrokerPlugins()) {
        server.callBrokerPlugins(plugin -> plugin.afterCloseConsumer(this, failed));
    }
}
Also used : Transaction(org.apache.activemq.artemis.core.transaction.Transaction) TransactionImpl(org.apache.activemq.artemis.core.transaction.impl.TransactionImpl) MessageReference(org.apache.activemq.artemis.core.server.MessageReference) TypedProperties(org.apache.activemq.artemis.utils.collections.TypedProperties) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQIllegalStateException(org.apache.activemq.artemis.api.core.ActiveMQIllegalStateException) Notification(org.apache.activemq.artemis.core.server.management.Notification)

Example 9 with MessageReference

use of org.apache.activemq.artemis.core.server.MessageReference in project activemq-artemis by apache.

the class ServerConsumerImpl method acknowledge.

@Override
public synchronized void acknowledge(Transaction tx, final long messageID) throws Exception {
    if (browseOnly) {
        return;
    }
    // Acknowledge acknowledges all refs delivered by the consumer up to and including the one explicitly
    // acknowledged
    // We use a transaction here as if the message is not found, we should rollback anything done
    // This could eventually happen on retries during transactions, and we need to make sure we don't ACK things we are not supposed to acknowledge
    boolean startedTransaction = false;
    if (tx == null) {
        startedTransaction = true;
        tx = new TransactionImpl(storageManager);
    }
    try {
        MessageReference ref;
        do {
            synchronized (lock) {
                ref = deliveringRefs.poll();
            }
            if (logger.isTraceEnabled()) {
                logger.trace("ACKing ref " + ref + " on tx= " + tx + ", consumer=" + this);
            }
            if (ref == null) {
                ActiveMQIllegalStateException ils = ActiveMQMessageBundle.BUNDLE.consumerNoReference(id, messageID, messageQueue.getName());
                tx.markAsRollbackOnly(ils);
                throw ils;
            }
            ref.acknowledge(tx);
            acks++;
        } while (ref.getMessageID() != messageID);
        if (startedTransaction) {
            tx.commit();
        }
    } catch (ActiveMQException e) {
        if (startedTransaction) {
            tx.rollback();
        } else {
            tx.markAsRollbackOnly(e);
        }
        throw e;
    } catch (Throwable e) {
        ActiveMQServerLogger.LOGGER.errorAckingMessage((Exception) e);
        ActiveMQException activeMQIllegalStateException = new ActiveMQIllegalStateException(e.getMessage());
        if (startedTransaction) {
            tx.rollback();
        } else {
            tx.markAsRollbackOnly(activeMQIllegalStateException);
        }
        throw activeMQIllegalStateException;
    }
}
Also used : ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQIllegalStateException(org.apache.activemq.artemis.api.core.ActiveMQIllegalStateException) TransactionImpl(org.apache.activemq.artemis.core.transaction.impl.TransactionImpl) MessageReference(org.apache.activemq.artemis.core.server.MessageReference) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQIllegalStateException(org.apache.activemq.artemis.api.core.ActiveMQIllegalStateException)

Example 10 with MessageReference

use of org.apache.activemq.artemis.core.server.MessageReference in project activemq-artemis by apache.

the class ServerConsumerImpl method reject.

@Override
public synchronized void reject(final long messageID) throws Exception {
    if (browseOnly) {
        return;
    }
    MessageReference ref = removeReferenceByID(messageID);
    if (ref == null) {
        // nothing to be done
        return;
    }
    ref.getQueue().sendToDeadLetterAddress(null, ref);
}
Also used : MessageReference(org.apache.activemq.artemis.core.server.MessageReference)

Aggregations

MessageReference (org.apache.activemq.artemis.core.server.MessageReference)82 ArrayList (java.util.ArrayList)29 QueueImpl (org.apache.activemq.artemis.core.server.impl.QueueImpl)26 Test (org.junit.Test)26 FakeConsumer (org.apache.activemq.artemis.tests.unit.core.server.impl.fakes.FakeConsumer)18 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)17 Transaction (org.apache.activemq.artemis.core.transaction.Transaction)14 Message (org.apache.activemq.artemis.api.core.Message)12 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)11 TransactionImpl (org.apache.activemq.artemis.core.transaction.impl.TransactionImpl)11 Queue (org.apache.activemq.artemis.core.server.Queue)10 HashMap (java.util.HashMap)9 NoSuchElementException (java.util.NoSuchElementException)9 Map (java.util.Map)8 Filter (org.apache.activemq.artemis.core.filter.Filter)7 LinkedList (java.util.LinkedList)5 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)5 ActiveMQIllegalStateException (org.apache.activemq.artemis.api.core.ActiveMQIllegalStateException)4 BindingsTransactionImpl (org.apache.activemq.artemis.core.transaction.impl.BindingsTransactionImpl)4 HashSet (java.util.HashSet)3