Search in sources :

Example 1 with Visitor

use of org.apache.qpid.server.protocol.v0_8.UnacknowledgedMessageMap.Visitor in project qpid-broker-j by apache.

the class AMQChannel method requeue.

/**
 * Called to attempt re-delivery all outstanding unacknowledged messages on the channel. May result in delivery to
 * this same channel or to other subscribers.
 */
private void requeue() {
    final Map<Long, MessageConsumerAssociation> copy = new LinkedHashMap<>();
    _unacknowledgedMessageMap.visit(new Visitor() {

        @Override
        public boolean callback(final long deliveryTag, final MessageConsumerAssociation messageConsumerPair) {
            copy.put(deliveryTag, messageConsumerPair);
            return false;
        }

        @Override
        public void visitComplete() {
        }
    });
    if (!copy.isEmpty()) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Requeuing {} unacked messages", copy.size());
        }
    }
    for (Map.Entry<Long, MessageConsumerAssociation> entry : copy.entrySet()) {
        MessageInstance unacked = entry.getValue().getMessageInstance();
        MessageInstanceConsumer consumer = entry.getValue().getConsumer();
        // Mark message redelivered
        unacked.setRedelivered();
        // here we wish to restore credit
        _unacknowledgedMessageMap.remove(entry.getKey(), true);
        // Ensure message is released for redelivery
        unacked.release(consumer);
    }
}
Also used : MessageInstance(org.apache.qpid.server.message.MessageInstance) Visitor(org.apache.qpid.server.protocol.v0_8.UnacknowledgedMessageMap.Visitor) MessageInstanceConsumer(org.apache.qpid.server.message.MessageInstanceConsumer) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with Visitor

use of org.apache.qpid.server.protocol.v0_8.UnacknowledgedMessageMap.Visitor in project qpid-broker-j by apache.

the class AMQChannel method resend.

/**
 * Called to resend all outstanding unacknowledged messages to this same channel.
 */
private void resend() {
    final Map<Long, MessageConsumerAssociation> msgToRequeue = new LinkedHashMap<>();
    final Map<Long, MessageConsumerAssociation> msgToResend = new LinkedHashMap<>();
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Unacknowledged messages: {}", _unacknowledgedMessageMap.size());
    }
    _unacknowledgedMessageMap.visit(new Visitor() {

        @Override
        public boolean callback(final long deliveryTag, final MessageConsumerAssociation association) {
            if (association.getConsumer().isClosed()) {
                // consumer has gone
                msgToRequeue.put(deliveryTag, association);
            } else {
                // Consumer still exists
                msgToResend.put(deliveryTag, association);
            }
            return false;
        }

        @Override
        public void visitComplete() {
        }
    });
    for (Map.Entry<Long, MessageConsumerAssociation> entry : msgToResend.entrySet()) {
        long deliveryTag = entry.getKey();
        MessageInstance message = entry.getValue().getMessageInstance();
        MessageInstanceConsumer consumer = entry.getValue().getConsumer();
        // Without any details from the client about what has been processed we have to mark
        // all messages in the unacked map as redelivered.
        message.setRedelivered();
        if (message.makeAcquisitionUnstealable(consumer)) {
            message.decrementDeliveryCount();
            consumer.getTarget().send(consumer, message, false);
            // remove from unacked map - don't want to restore credit though(!)
            _unacknowledgedMessageMap.remove(deliveryTag, false);
        } else {
            msgToRequeue.put(deliveryTag, entry.getValue());
        }
    }
    // Process Messages to Requeue at the front of the queue
    for (Map.Entry<Long, MessageConsumerAssociation> entry : msgToRequeue.entrySet()) {
        long deliveryTag = entry.getKey();
        MessageInstance message = entry.getValue().getMessageInstance();
        MessageInstanceConsumer consumer = entry.getValue().getConsumer();
        // Amend the delivery counter as the client hasn't seen these messages yet.
        message.decrementDeliveryCount();
        // here we do wish to restore credit
        _unacknowledgedMessageMap.remove(deliveryTag, true);
        message.setRedelivered();
        message.release(consumer);
    }
}
Also used : MessageInstance(org.apache.qpid.server.message.MessageInstance) Visitor(org.apache.qpid.server.protocol.v0_8.UnacknowledgedMessageMap.Visitor) MessageInstanceConsumer(org.apache.qpid.server.message.MessageInstanceConsumer) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 MessageInstance (org.apache.qpid.server.message.MessageInstance)2 MessageInstanceConsumer (org.apache.qpid.server.message.MessageInstanceConsumer)2 Visitor (org.apache.qpid.server.protocol.v0_8.UnacknowledgedMessageMap.Visitor)2