Search in sources :

Example 76 with MessageReference

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

the class ServerConsumerImpl method individualCancel.

@Override
public synchronized void individualCancel(final long messageID, boolean failed) throws Exception {
    if (browseOnly) {
        return;
    }
    MessageReference ref = removeReferenceByID(messageID);
    if (ref == null) {
        throw new IllegalStateException("Cannot find ref to ack " + messageID);
    }
    if (!failed) {
        ref.decrementDeliveryCount();
    }
    ref.getQueue().cancel(ref, System.currentTimeMillis());
}
Also used : ActiveMQIllegalStateException(org.apache.activemq.artemis.api.core.ActiveMQIllegalStateException) MessageReference(org.apache.activemq.artemis.core.server.MessageReference)

Example 77 with MessageReference

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

the class ServerConsumerImpl method individualAcknowledge.

@Override
public synchronized void individualAcknowledge(Transaction tx, final long messageID) throws Exception {
    if (browseOnly) {
        return;
    }
    boolean startedTransaction = false;
    if (logger.isTraceEnabled()) {
        logger.trace("individualACK messageID=" + messageID);
    }
    if (tx == null) {
        if (logger.isTraceEnabled()) {
            logger.trace("individualACK starting new TX");
        }
        startedTransaction = true;
        tx = new TransactionImpl(storageManager);
    }
    try {
        MessageReference ref;
        ref = removeReferenceByID(messageID);
        if (logger.isTraceEnabled()) {
            logger.trace("ACKing ref " + ref + " on tx= " + tx + ", consumer=" + this);
        }
        if (ref == null) {
            ActiveMQIllegalStateException ils = new ActiveMQIllegalStateException("Cannot find ref to ack " + messageID);
            tx.markAsRollbackOnly(ils);
            throw ils;
        }
        ref.acknowledge(tx);
        acks++;
        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);
        ActiveMQIllegalStateException hqex = new ActiveMQIllegalStateException(e.getMessage());
        if (startedTransaction) {
            tx.rollback();
        } else {
            tx.markAsRollbackOnly(hqex);
        }
        throw hqex;
    }
}
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 78 with MessageReference

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

the class ServerConsumerImpl method getDeliveringReferencesBasedOnProtocol.

/**
 * Remove references based on the protocolData.
 * there will be an interval defined between protocolDataStart and protocolDataEnd.
 * This method will fetch the delivering references, remove them from the delivering list and return a list.
 *
 * This will be useful for other protocols that will need this such as openWire or MQTT.
 */
@Override
public synchronized List<MessageReference> getDeliveringReferencesBasedOnProtocol(boolean remove, Object protocolDataStart, Object protocolDataEnd) {
    LinkedList<MessageReference> retReferences = new LinkedList<>();
    boolean hit = false;
    synchronized (lock) {
        Iterator<MessageReference> referenceIterator = deliveringRefs.iterator();
        while (referenceIterator.hasNext()) {
            MessageReference reference = referenceIterator.next();
            if (!hit) {
                hit = reference.getProtocolData() != null && reference.getProtocolData().equals(protocolDataStart);
            }
            // notice: this is not an else clause, this is also valid for the first hit
            if (hit) {
                if (remove) {
                    referenceIterator.remove();
                }
                retReferences.add(reference);
                // even on the first hit
                if (reference.getProtocolData() != null && reference.getProtocolData().equals(protocolDataEnd)) {
                    break;
                }
            }
        }
    }
    return retReferences;
}
Also used : MessageReference(org.apache.activemq.artemis.core.server.MessageReference) LinkedList(java.util.LinkedList)

Example 79 with MessageReference

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

the class RefsOperation method afterRollback.

@Override
public void afterRollback(final Transaction tx) {
    Map<QueueImpl, LinkedList<MessageReference>> queueMap = new HashMap<>();
    long timeBase = System.currentTimeMillis();
    // add any already acked refs, this means that they have been transferred via a producer.send() and have no
    // previous state persisted.
    List<MessageReference> ackedRefs = new ArrayList<>();
    for (MessageReference ref : refsToAck) {
        ref.emptyConsumerID();
        if (logger.isTraceEnabled()) {
            logger.trace("rolling back " + ref);
        }
        try {
            if (ref.isAlreadyAcked()) {
                ackedRefs.add(ref);
            }
            rollbackRedelivery(tx, ref, timeBase, queueMap);
        } catch (Exception e) {
            ActiveMQServerLogger.LOGGER.errorCheckingDLQ(e);
        }
    }
    for (Map.Entry<QueueImpl, LinkedList<MessageReference>> entry : queueMap.entrySet()) {
        LinkedList<MessageReference> refs = entry.getValue();
        QueueImpl queue = entry.getKey();
        synchronized (queue) {
            queue.postRollback(refs);
        }
    }
    if (!ackedRefs.isEmpty()) {
        // message references
        try {
            Transaction ackedTX = new TransactionImpl(storageManager);
            for (MessageReference ref : ackedRefs) {
                Message message = ref.getMessage();
                if (message.isDurable()) {
                    int durableRefCount = message.incrementDurableRefCount();
                    if (durableRefCount == 1) {
                        storageManager.storeMessageTransactional(ackedTX.getID(), message);
                    }
                    Queue queue = ref.getQueue();
                    storageManager.storeReferenceTransactional(ackedTX.getID(), queue.getID(), message.getMessageID());
                    ackedTX.setContainsPersistent();
                }
                message.incrementRefCount();
            }
            ackedTX.commit(true);
        } catch (Exception e) {
            ActiveMQServerLogger.LOGGER.failedToProcessMessageReferenceAfterRollback(e);
        }
    }
}
Also used : Message(org.apache.activemq.artemis.api.core.Message) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TransactionImpl(org.apache.activemq.artemis.core.transaction.impl.TransactionImpl) MessageReference(org.apache.activemq.artemis.core.server.MessageReference) LinkedList(java.util.LinkedList) Transaction(org.apache.activemq.artemis.core.transaction.Transaction) HashMap(java.util.HashMap) Map(java.util.Map) Queue(org.apache.activemq.artemis.core.server.Queue)

Example 80 with MessageReference

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

the class LastValueQueue method replaceLVQMessage.

private void replaceLVQMessage(MessageReference ref, HolderReference hr) {
    MessageReference oldRef = hr.getReference();
    referenceHandled(ref);
    try {
        oldRef.acknowledge();
    } catch (Exception e) {
        ActiveMQServerLogger.LOGGER.errorAckingOldReference(e);
    }
    hr.setReference(ref);
}
Also used : MessageReference(org.apache.activemq.artemis.core.server.MessageReference) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException)

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