001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.broker.region;
018
019import static org.apache.activemq.broker.region.cursors.AbstractStoreCursor.gotToTheStore;
020import static org.apache.activemq.transaction.Transaction.IN_USE_STATE;
021
022import java.io.IOException;
023import java.util.ArrayList;
024import java.util.Collection;
025import java.util.Collections;
026import java.util.Comparator;
027import java.util.HashSet;
028import java.util.Iterator;
029import java.util.LinkedHashMap;
030import java.util.LinkedHashSet;
031import java.util.LinkedList;
032import java.util.List;
033import java.util.Map;
034import java.util.Set;
035import java.util.concurrent.CancellationException;
036import java.util.concurrent.ConcurrentLinkedQueue;
037import java.util.concurrent.CountDownLatch;
038import java.util.concurrent.DelayQueue;
039import java.util.concurrent.Delayed;
040import java.util.concurrent.ExecutorService;
041import java.util.concurrent.TimeUnit;
042import java.util.concurrent.atomic.AtomicInteger;
043import java.util.concurrent.atomic.AtomicLong;
044import java.util.concurrent.locks.Lock;
045import java.util.concurrent.locks.ReentrantLock;
046import java.util.concurrent.locks.ReentrantReadWriteLock;
047
048import javax.jms.InvalidSelectorException;
049import javax.jms.JMSException;
050import javax.jms.ResourceAllocationException;
051
052import org.apache.activemq.broker.BrokerService;
053import org.apache.activemq.broker.BrokerStoppedException;
054import org.apache.activemq.broker.ConnectionContext;
055import org.apache.activemq.broker.ProducerBrokerExchange;
056import org.apache.activemq.broker.region.cursors.OrderedPendingList;
057import org.apache.activemq.broker.region.cursors.PendingList;
058import org.apache.activemq.broker.region.cursors.PendingMessageCursor;
059import org.apache.activemq.broker.region.cursors.PrioritizedPendingList;
060import org.apache.activemq.broker.region.cursors.QueueDispatchPendingList;
061import org.apache.activemq.broker.region.cursors.StoreQueueCursor;
062import org.apache.activemq.broker.region.cursors.VMPendingMessageCursor;
063import org.apache.activemq.broker.region.group.CachedMessageGroupMapFactory;
064import org.apache.activemq.broker.region.group.MessageGroupMap;
065import org.apache.activemq.broker.region.group.MessageGroupMapFactory;
066import org.apache.activemq.broker.region.policy.DeadLetterStrategy;
067import org.apache.activemq.broker.region.policy.DispatchPolicy;
068import org.apache.activemq.broker.region.policy.RoundRobinDispatchPolicy;
069import org.apache.activemq.broker.util.InsertionCountList;
070import org.apache.activemq.command.ActiveMQDestination;
071import org.apache.activemq.command.ConsumerId;
072import org.apache.activemq.command.ExceptionResponse;
073import org.apache.activemq.command.Message;
074import org.apache.activemq.command.MessageAck;
075import org.apache.activemq.command.MessageDispatchNotification;
076import org.apache.activemq.command.MessageId;
077import org.apache.activemq.command.ProducerAck;
078import org.apache.activemq.command.ProducerInfo;
079import org.apache.activemq.command.RemoveInfo;
080import org.apache.activemq.command.Response;
081import org.apache.activemq.filter.BooleanExpression;
082import org.apache.activemq.filter.MessageEvaluationContext;
083import org.apache.activemq.filter.NonCachedMessageEvaluationContext;
084import org.apache.activemq.selector.SelectorParser;
085import org.apache.activemq.state.ProducerState;
086import org.apache.activemq.store.IndexListener;
087import org.apache.activemq.store.ListenableFuture;
088import org.apache.activemq.store.MessageRecoveryListener;
089import org.apache.activemq.store.MessageStore;
090import org.apache.activemq.thread.Task;
091import org.apache.activemq.thread.TaskRunner;
092import org.apache.activemq.thread.TaskRunnerFactory;
093import org.apache.activemq.transaction.Synchronization;
094import org.apache.activemq.usage.Usage;
095import org.apache.activemq.usage.UsageListener;
096import org.apache.activemq.util.BrokerSupport;
097import org.apache.activemq.util.ThreadPoolUtils;
098import org.slf4j.Logger;
099import org.slf4j.LoggerFactory;
100import org.slf4j.MDC;
101
102/**
103 * The Queue is a List of MessageEntry objects that are dispatched to matching
104 * subscriptions.
105 */
106public class Queue extends BaseDestination implements Task, UsageListener, IndexListener {
107    protected static final Logger LOG = LoggerFactory.getLogger(Queue.class);
108    protected final TaskRunnerFactory taskFactory;
109    protected TaskRunner taskRunner;
110    private final ReentrantReadWriteLock consumersLock = new ReentrantReadWriteLock();
111    protected final List<Subscription> consumers = new ArrayList<Subscription>(50);
112    private final ReentrantReadWriteLock messagesLock = new ReentrantReadWriteLock();
113    protected PendingMessageCursor messages;
114    private final ReentrantReadWriteLock pagedInMessagesLock = new ReentrantReadWriteLock();
115    private final PendingList pagedInMessages = new OrderedPendingList();
116    // Messages that are paged in but have not yet been targeted at a subscription
117    private final ReentrantReadWriteLock pagedInPendingDispatchLock = new ReentrantReadWriteLock();
118    protected QueueDispatchPendingList dispatchPendingList = new QueueDispatchPendingList();
119    private AtomicInteger pendingSends = new AtomicInteger(0);
120    private MessageGroupMap messageGroupOwners;
121    private DispatchPolicy dispatchPolicy = new RoundRobinDispatchPolicy();
122    private MessageGroupMapFactory messageGroupMapFactory = new CachedMessageGroupMapFactory();
123    final Lock sendLock = new ReentrantLock();
124    private ExecutorService executor;
125    private final Map<MessageId, Runnable> messagesWaitingForSpace = new LinkedHashMap<MessageId, Runnable>();
126    private boolean useConsumerPriority = true;
127    private boolean strictOrderDispatch = false;
128    private final QueueDispatchSelector dispatchSelector;
129    private boolean optimizedDispatch = false;
130    private boolean iterationRunning = false;
131    private boolean firstConsumer = false;
132    private int timeBeforeDispatchStarts = 0;
133    private int consumersBeforeDispatchStarts = 0;
134    private CountDownLatch consumersBeforeStartsLatch;
135    private final AtomicLong pendingWakeups = new AtomicLong();
136    private boolean allConsumersExclusiveByDefault = false;
137
138    private volatile boolean resetNeeded;
139
140    private final Runnable sendMessagesWaitingForSpaceTask = new Runnable() {
141        @Override
142        public void run() {
143            asyncWakeup();
144        }
145    };
146    private final Runnable expireMessagesTask = new Runnable() {
147        @Override
148        public void run() {
149            expireMessages();
150        }
151    };
152
153    private final Object iteratingMutex = new Object();
154
155    // gate on enabling cursor cache to ensure no outstanding sync
156    // send before async sends resume
157    public boolean singlePendingSend() {
158        return pendingSends.get() <= 1;
159    }
160
161    class TimeoutMessage implements Delayed {
162
163        Message message;
164        ConnectionContext context;
165        long trigger;
166
167        public TimeoutMessage(Message message, ConnectionContext context, long delay) {
168            this.message = message;
169            this.context = context;
170            this.trigger = System.currentTimeMillis() + delay;
171        }
172
173        @Override
174        public long getDelay(TimeUnit unit) {
175            long n = trigger - System.currentTimeMillis();
176            return unit.convert(n, TimeUnit.MILLISECONDS);
177        }
178
179        @Override
180        public int compareTo(Delayed delayed) {
181            long other = ((TimeoutMessage) delayed).trigger;
182            int returnValue;
183            if (this.trigger < other) {
184                returnValue = -1;
185            } else if (this.trigger > other) {
186                returnValue = 1;
187            } else {
188                returnValue = 0;
189            }
190            return returnValue;
191        }
192    }
193
194    DelayQueue<TimeoutMessage> flowControlTimeoutMessages = new DelayQueue<TimeoutMessage>();
195
196    class FlowControlTimeoutTask extends Thread {
197
198        @Override
199        public void run() {
200            TimeoutMessage timeout;
201            try {
202                while (true) {
203                    timeout = flowControlTimeoutMessages.take();
204                    if (timeout != null) {
205                        synchronized (messagesWaitingForSpace) {
206                            if (messagesWaitingForSpace.remove(timeout.message.getMessageId()) != null) {
207                                ExceptionResponse response = new ExceptionResponse(
208                                        new ResourceAllocationException(
209                                                "Usage Manager Memory Limit reached. Stopping producer ("
210                                                        + timeout.message.getProducerId()
211                                                        + ") to prevent flooding "
212                                                        + getActiveMQDestination().getQualifiedName()
213                                                        + "."
214                                                        + " See http://activemq.apache.org/producer-flow-control.html for more info"));
215                                response.setCorrelationId(timeout.message.getCommandId());
216                                timeout.context.getConnection().dispatchAsync(response);
217                            }
218                        }
219                    }
220                }
221            } catch (InterruptedException e) {
222                LOG.debug(getName() + "Producer Flow Control Timeout Task is stopping");
223            }
224        }
225    }
226
227    private final FlowControlTimeoutTask flowControlTimeoutTask = new FlowControlTimeoutTask();
228
229    private final Comparator<Subscription> orderedCompare = new Comparator<Subscription>() {
230
231        @Override
232        public int compare(Subscription s1, Subscription s2) {
233            // We want the list sorted in descending order
234            int val = s2.getConsumerInfo().getPriority() - s1.getConsumerInfo().getPriority();
235            if (val == 0 && messageGroupOwners != null) {
236                // then ascending order of assigned message groups to favour less loaded consumers
237                // Long.compare in jdk7
238                long x = s1.getConsumerInfo().getAssignedGroupCount(destination);
239                long y = s2.getConsumerInfo().getAssignedGroupCount(destination);
240                val = (x < y) ? -1 : ((x == y) ? 0 : 1);
241            }
242            return val;
243        }
244    };
245
246    public Queue(BrokerService brokerService, final ActiveMQDestination destination, MessageStore store,
247            DestinationStatistics parentStats, TaskRunnerFactory taskFactory) throws Exception {
248        super(brokerService, store, destination, parentStats);
249        this.taskFactory = taskFactory;
250        this.dispatchSelector = new QueueDispatchSelector(destination);
251        if (store != null) {
252            store.registerIndexListener(this);
253        }
254    }
255
256    @Override
257    public List<Subscription> getConsumers() {
258        consumersLock.readLock().lock();
259        try {
260            return new ArrayList<Subscription>(consumers);
261        } finally {
262            consumersLock.readLock().unlock();
263        }
264    }
265
266    // make the queue easily visible in the debugger from its task runner
267    // threads
268    final class QueueThread extends Thread {
269        final Queue queue;
270
271        public QueueThread(Runnable runnable, String name, Queue queue) {
272            super(runnable, name);
273            this.queue = queue;
274        }
275    }
276
277    class BatchMessageRecoveryListener implements MessageRecoveryListener {
278        final LinkedList<Message> toExpire = new LinkedList<Message>();
279        final double totalMessageCount;
280        int recoveredAccumulator = 0;
281        int currentBatchCount;
282
283        BatchMessageRecoveryListener(int totalMessageCount) {
284            this.totalMessageCount = totalMessageCount;
285            currentBatchCount = recoveredAccumulator;
286        }
287
288        @Override
289        public boolean recoverMessage(Message message) {
290            recoveredAccumulator++;
291            if ((recoveredAccumulator % 10000) == 0) {
292                LOG.info("cursor for {} has recovered {} messages. {}% complete", new Object[]{ getActiveMQDestination().getQualifiedName(), recoveredAccumulator, new Integer((int) (recoveredAccumulator * 100 / totalMessageCount))});
293            }
294            // Message could have expired while it was being
295            // loaded..
296            message.setRegionDestination(Queue.this);
297            if (message.isExpired() && broker.isExpired(message)) {
298                toExpire.add(message);
299                return true;
300            }
301            if (hasSpace()) {
302                messagesLock.writeLock().lock();
303                try {
304                    try {
305                        messages.addMessageLast(message);
306                    } catch (Exception e) {
307                        LOG.error("Failed to add message to cursor", e);
308                    }
309                } finally {
310                    messagesLock.writeLock().unlock();
311                }
312                destinationStatistics.getMessages().increment();
313                return true;
314            }
315            return false;
316        }
317
318        @Override
319        public boolean recoverMessageReference(MessageId messageReference) throws Exception {
320            throw new RuntimeException("Should not be called.");
321        }
322
323        @Override
324        public boolean hasSpace() {
325            return true;
326        }
327
328        @Override
329        public boolean isDuplicate(MessageId id) {
330            return false;
331        }
332
333        public void reset() {
334            currentBatchCount = recoveredAccumulator;
335        }
336
337        public void processExpired() {
338            for (Message message: toExpire) {
339                messageExpired(createConnectionContext(), createMessageReference(message));
340                // drop message will decrement so counter
341                // balance here
342                destinationStatistics.getMessages().increment();
343            }
344            toExpire.clear();
345        }
346
347        public boolean done() {
348            return currentBatchCount == recoveredAccumulator;
349        }
350    }
351
352    @Override
353    public void setPrioritizedMessages(boolean prioritizedMessages) {
354        super.setPrioritizedMessages(prioritizedMessages);
355        dispatchPendingList.setPrioritizedMessages(prioritizedMessages);
356    }
357
358    @Override
359    public void initialize() throws Exception {
360
361        if (this.messages == null) {
362            if (destination.isTemporary() || broker == null || store == null) {
363                this.messages = new VMPendingMessageCursor(isPrioritizedMessages());
364            } else {
365                this.messages = new StoreQueueCursor(broker, this);
366            }
367        }
368
369        // If a VMPendingMessageCursor don't use the default Producer System
370        // Usage
371        // since it turns into a shared blocking queue which can lead to a
372        // network deadlock.
373        // If we are cursoring to disk..it's not and issue because it does not
374        // block due
375        // to large disk sizes.
376        if (messages instanceof VMPendingMessageCursor) {
377            this.systemUsage = brokerService.getSystemUsage();
378            memoryUsage.setParent(systemUsage.getMemoryUsage());
379        }
380
381        this.taskRunner = taskFactory.createTaskRunner(this, "Queue:" + destination.getPhysicalName());
382
383        super.initialize();
384        if (store != null) {
385            // Restore the persistent messages.
386            messages.setSystemUsage(systemUsage);
387            messages.setEnableAudit(isEnableAudit());
388            messages.setMaxAuditDepth(getMaxAuditDepth());
389            messages.setMaxProducersToAudit(getMaxProducersToAudit());
390            messages.setUseCache(isUseCache());
391            messages.setMemoryUsageHighWaterMark(getCursorMemoryHighWaterMark());
392            store.start();
393            final int messageCount = store.getMessageCount();
394            if (messageCount > 0 && messages.isRecoveryRequired()) {
395                BatchMessageRecoveryListener listener = new BatchMessageRecoveryListener(messageCount);
396                do {
397                   listener.reset();
398                   store.recoverNextMessages(getMaxPageSize(), listener);
399                   listener.processExpired();
400               } while (!listener.done());
401            } else {
402                destinationStatistics.getMessages().add(messageCount);
403            }
404        }
405    }
406
407    /*
408     * Holder for subscription that needs attention on next iterate browser
409     * needs access to existing messages in the queue that have already been
410     * dispatched
411     */
412    class BrowserDispatch {
413        QueueBrowserSubscription browser;
414
415        public BrowserDispatch(QueueBrowserSubscription browserSubscription) {
416            browser = browserSubscription;
417            browser.incrementQueueRef();
418        }
419
420        public QueueBrowserSubscription getBrowser() {
421            return browser;
422        }
423    }
424
425    ConcurrentLinkedQueue<BrowserDispatch> browserDispatches = new ConcurrentLinkedQueue<BrowserDispatch>();
426
427    @Override
428    public void addSubscription(ConnectionContext context, Subscription sub) throws Exception {
429        LOG.debug("{} add sub: {}, dequeues: {}, dispatched: {}, inflight: {}", new Object[]{ getActiveMQDestination().getQualifiedName(), sub, getDestinationStatistics().getDequeues().getCount(), getDestinationStatistics().getDispatched().getCount(), getDestinationStatistics().getInflight().getCount() });
430
431        super.addSubscription(context, sub);
432        // synchronize with dispatch method so that no new messages are sent
433        // while setting up a subscription. avoid out of order messages,
434        // duplicates, etc.
435        pagedInPendingDispatchLock.writeLock().lock();
436        try {
437
438            sub.add(context, this);
439
440            // needs to be synchronized - so no contention with dispatching
441            // consumersLock.
442            consumersLock.writeLock().lock();
443            try {
444                // set a flag if this is a first consumer
445                if (consumers.size() == 0) {
446                    firstConsumer = true;
447                    if (consumersBeforeDispatchStarts != 0) {
448                        consumersBeforeStartsLatch = new CountDownLatch(consumersBeforeDispatchStarts - 1);
449                    }
450                } else {
451                    if (consumersBeforeStartsLatch != null) {
452                        consumersBeforeStartsLatch.countDown();
453                    }
454                }
455
456                addToConsumerList(sub);
457                if (sub.getConsumerInfo().isExclusive() || isAllConsumersExclusiveByDefault()) {
458                    Subscription exclusiveConsumer = dispatchSelector.getExclusiveConsumer();
459                    if (exclusiveConsumer == null) {
460                        exclusiveConsumer = sub;
461                    } else if (sub.getConsumerInfo().getPriority() == Byte.MAX_VALUE ||
462                        sub.getConsumerInfo().getPriority() > exclusiveConsumer.getConsumerInfo().getPriority()) {
463                        exclusiveConsumer = sub;
464                    }
465                    dispatchSelector.setExclusiveConsumer(exclusiveConsumer);
466                }
467            } finally {
468                consumersLock.writeLock().unlock();
469            }
470
471            if (sub instanceof QueueBrowserSubscription) {
472                // tee up for dispatch in next iterate
473                QueueBrowserSubscription browserSubscription = (QueueBrowserSubscription) sub;
474                BrowserDispatch browserDispatch = new BrowserDispatch(browserSubscription);
475                browserDispatches.add(browserDispatch);
476            }
477
478            if (!this.optimizedDispatch) {
479                wakeup();
480            }
481        } finally {
482            pagedInPendingDispatchLock.writeLock().unlock();
483        }
484        if (this.optimizedDispatch) {
485            // Outside of dispatchLock() to maintain the lock hierarchy of
486            // iteratingMutex -> dispatchLock. - see
487            // https://issues.apache.org/activemq/browse/AMQ-1878
488            wakeup();
489        }
490    }
491
492    @Override
493    public void removeSubscription(ConnectionContext context, Subscription sub, long lastDeliveredSequenceId)
494            throws Exception {
495        super.removeSubscription(context, sub, lastDeliveredSequenceId);
496        // synchronize with dispatch method so that no new messages are sent
497        // while removing up a subscription.
498        pagedInPendingDispatchLock.writeLock().lock();
499        try {
500            LOG.debug("{} remove sub: {}, lastDeliveredSeqId: {}, dequeues: {}, dispatched: {}, inflight: {}, groups: {}", new Object[]{
501                    getActiveMQDestination().getQualifiedName(),
502                    sub,
503                    lastDeliveredSequenceId,
504                    getDestinationStatistics().getDequeues().getCount(),
505                    getDestinationStatistics().getDispatched().getCount(),
506                    getDestinationStatistics().getInflight().getCount(),
507                    sub.getConsumerInfo().getAssignedGroupCount(destination)
508            });
509            consumersLock.writeLock().lock();
510            try {
511                removeFromConsumerList(sub);
512                if (sub.getConsumerInfo().isExclusive()) {
513                    Subscription exclusiveConsumer = dispatchSelector.getExclusiveConsumer();
514                    if (exclusiveConsumer == sub) {
515                        exclusiveConsumer = null;
516                        for (Subscription s : consumers) {
517                            if (s.getConsumerInfo().isExclusive()
518                                    && (exclusiveConsumer == null || s.getConsumerInfo().getPriority() > exclusiveConsumer
519                                            .getConsumerInfo().getPriority())) {
520                                exclusiveConsumer = s;
521
522                            }
523                        }
524                        dispatchSelector.setExclusiveConsumer(exclusiveConsumer);
525                    }
526                } else if (isAllConsumersExclusiveByDefault()) {
527                    Subscription exclusiveConsumer = null;
528                    for (Subscription s : consumers) {
529                        if (exclusiveConsumer == null
530                                || s.getConsumerInfo().getPriority() > exclusiveConsumer
531                                .getConsumerInfo().getPriority()) {
532                            exclusiveConsumer = s;
533                                }
534                    }
535                    dispatchSelector.setExclusiveConsumer(exclusiveConsumer);
536                }
537                ConsumerId consumerId = sub.getConsumerInfo().getConsumerId();
538                getMessageGroupOwners().removeConsumer(consumerId);
539
540                // redeliver inflight messages
541
542                boolean markAsRedelivered = false;
543                MessageReference lastDeliveredRef = null;
544                List<MessageReference> unAckedMessages = sub.remove(context, this);
545
546                // locate last redelivered in unconsumed list (list in delivery rather than seq order)
547                if (lastDeliveredSequenceId > RemoveInfo.LAST_DELIVERED_UNSET) {
548                    for (MessageReference ref : unAckedMessages) {
549                        if (ref.getMessageId().getBrokerSequenceId() == lastDeliveredSequenceId) {
550                            lastDeliveredRef = ref;
551                            markAsRedelivered = true;
552                            LOG.debug("found lastDeliveredSeqID: {}, message reference: {}", lastDeliveredSequenceId, ref.getMessageId());
553                            break;
554                        }
555                    }
556                }
557
558                for (Iterator<MessageReference> unackedListIterator = unAckedMessages.iterator(); unackedListIterator.hasNext(); ) {
559                    MessageReference ref = unackedListIterator.next();
560                    // AMQ-5107: don't resend if the broker is shutting down
561                    if ( this.brokerService.isStopping() ) {
562                        break;
563                    }
564                    QueueMessageReference qmr = (QueueMessageReference) ref;
565                    if (qmr.getLockOwner() == sub) {
566                        qmr.unlock();
567
568                        // have no delivery information
569                        if (lastDeliveredSequenceId == RemoveInfo.LAST_DELIVERED_UNKNOWN) {
570                            qmr.incrementRedeliveryCounter();
571                        } else {
572                            if (markAsRedelivered) {
573                                qmr.incrementRedeliveryCounter();
574                            }
575                            if (ref == lastDeliveredRef) {
576                                // all that follow were not redelivered
577                                markAsRedelivered = false;
578                            }
579                        }
580                    }
581                    if (qmr.isDropped()) {
582                        unackedListIterator.remove();
583                    }
584                }
585                dispatchPendingList.addForRedelivery(unAckedMessages, strictOrderDispatch && consumers.isEmpty());
586                if (sub instanceof QueueBrowserSubscription) {
587                    ((QueueBrowserSubscription)sub).decrementQueueRef();
588                    browserDispatches.remove(sub);
589                }
590                // AMQ-5107: don't resend if the broker is shutting down
591                if (dispatchPendingList.hasRedeliveries() && (! this.brokerService.isStopping())) {
592                    doDispatch(new OrderedPendingList());
593                }
594            } finally {
595                consumersLock.writeLock().unlock();
596            }
597            if (!this.optimizedDispatch) {
598                wakeup();
599            }
600        } finally {
601            pagedInPendingDispatchLock.writeLock().unlock();
602        }
603        if (this.optimizedDispatch) {
604            // Outside of dispatchLock() to maintain the lock hierarchy of
605            // iteratingMutex -> dispatchLock. - see
606            // https://issues.apache.org/activemq/browse/AMQ-1878
607            wakeup();
608        }
609    }
610
611    @Override
612    public void send(final ProducerBrokerExchange producerExchange, final Message message) throws Exception {
613        final ConnectionContext context = producerExchange.getConnectionContext();
614        // There is delay between the client sending it and it arriving at the
615        // destination.. it may have expired.
616        message.setRegionDestination(this);
617        ProducerState state = producerExchange.getProducerState();
618        if (state == null) {
619            LOG.warn("Send failed for: {}, missing producer state for: {}", message, producerExchange);
620            throw new JMSException("Cannot send message to " + getActiveMQDestination() + " with invalid (null) producer state");
621        }
622        final ProducerInfo producerInfo = producerExchange.getProducerState().getInfo();
623        final boolean sendProducerAck = !message.isResponseRequired() && producerInfo.getWindowSize() > 0
624                && !context.isInRecoveryMode();
625        if (message.isExpired()) {
626            // message not stored - or added to stats yet - so chuck here
627            broker.getRoot().messageExpired(context, message, null);
628            if (sendProducerAck) {
629                ProducerAck ack = new ProducerAck(producerInfo.getProducerId(), message.getSize());
630                context.getConnection().dispatchAsync(ack);
631            }
632            return;
633        }
634        if (memoryUsage.isFull()) {
635            isFull(context, memoryUsage);
636            fastProducer(context, producerInfo);
637            if (isProducerFlowControl() && context.isProducerFlowControl()) {
638                if (isFlowControlLogRequired()) {
639                    LOG.info("Usage Manager Memory Limit ({}) reached on {}, size {}. Producers will be throttled to the rate at which messages are removed from this destination to prevent flooding it. See http://activemq.apache.org/producer-flow-control.html for more info.",
640                                memoryUsage.getLimit(), getActiveMQDestination().getQualifiedName(), destinationStatistics.getMessages().getCount());
641
642                }
643                if (!context.isNetworkConnection() && systemUsage.isSendFailIfNoSpace()) {
644                    throw new ResourceAllocationException("Usage Manager Memory Limit reached. Stopping producer ("
645                            + message.getProducerId() + ") to prevent flooding "
646                            + getActiveMQDestination().getQualifiedName() + "."
647                            + " See http://activemq.apache.org/producer-flow-control.html for more info");
648                }
649
650                // We can avoid blocking due to low usage if the producer is
651                // sending
652                // a sync message or if it is using a producer window
653                if (producerInfo.getWindowSize() > 0 || message.isResponseRequired()) {
654                    // copy the exchange state since the context will be
655                    // modified while we are waiting
656                    // for space.
657                    final ProducerBrokerExchange producerExchangeCopy = producerExchange.copy();
658                    synchronized (messagesWaitingForSpace) {
659                     // Start flow control timeout task
660                        // Prevent trying to start it multiple times
661                        if (!flowControlTimeoutTask.isAlive()) {
662                            flowControlTimeoutTask.setName(getName()+" Producer Flow Control Timeout Task");
663                            flowControlTimeoutTask.start();
664                        }
665                        messagesWaitingForSpace.put(message.getMessageId(), new Runnable() {
666                            @Override
667                            public void run() {
668
669                                try {
670                                    // While waiting for space to free up... the
671                                    // transaction may be done
672                                    if (message.isInTransaction()) {
673                                        if (context.getTransaction().getState() > IN_USE_STATE) {
674                                            throw new JMSException("Send transaction completed while waiting for space");
675                                        }
676                                    }
677
678                                    // the message may have expired.
679                                    if (message.isExpired()) {
680                                        LOG.error("message expired waiting for space");
681                                        broker.messageExpired(context, message, null);
682                                        destinationStatistics.getExpired().increment();
683                                    } else {
684                                        doMessageSend(producerExchangeCopy, message);
685                                    }
686
687                                    if (sendProducerAck) {
688                                        ProducerAck ack = new ProducerAck(producerInfo.getProducerId(), message
689                                                .getSize());
690                                        context.getConnection().dispatchAsync(ack);
691                                    } else {
692                                        Response response = new Response();
693                                        response.setCorrelationId(message.getCommandId());
694                                        context.getConnection().dispatchAsync(response);
695                                    }
696
697                                } catch (Exception e) {
698                                    if (!sendProducerAck && !context.isInRecoveryMode() && !brokerService.isStopping()) {
699                                        ExceptionResponse response = new ExceptionResponse(e);
700                                        response.setCorrelationId(message.getCommandId());
701                                        context.getConnection().dispatchAsync(response);
702                                    } else {
703                                        LOG.debug("unexpected exception on deferred send of: {}", message, e);
704                                    }
705                                } finally {
706                                    getDestinationStatistics().getBlockedSends().decrement();
707                                    producerExchangeCopy.blockingOnFlowControl(false);
708                                }
709                            }
710                        });
711
712                        getDestinationStatistics().getBlockedSends().increment();
713                        producerExchange.blockingOnFlowControl(true);
714                        if (!context.isNetworkConnection() && systemUsage.getSendFailIfNoSpaceAfterTimeout() != 0) {
715                            flowControlTimeoutMessages.add(new TimeoutMessage(message, context, systemUsage
716                                    .getSendFailIfNoSpaceAfterTimeout()));
717                        }
718
719                        registerCallbackForNotFullNotification();
720                        context.setDontSendReponse(true);
721                        return;
722                    }
723
724                } else {
725
726                    if (memoryUsage.isFull()) {
727                        waitForSpace(context, producerExchange, memoryUsage, "Usage Manager Memory Limit reached. Producer ("
728                                + message.getProducerId() + ") stopped to prevent flooding "
729                                + getActiveMQDestination().getQualifiedName() + "."
730                                + " See http://activemq.apache.org/producer-flow-control.html for more info");
731                    }
732
733                    // The usage manager could have delayed us by the time
734                    // we unblock the message could have expired..
735                    if (message.isExpired()) {
736                        LOG.debug("Expired message: {}", message);
737                        broker.getRoot().messageExpired(context, message, null);
738                        return;
739                    }
740                }
741            }
742        }
743        doMessageSend(producerExchange, message);
744        if (sendProducerAck) {
745            ProducerAck ack = new ProducerAck(producerInfo.getProducerId(), message.getSize());
746            context.getConnection().dispatchAsync(ack);
747        }
748    }
749
750    private void registerCallbackForNotFullNotification() {
751        // If the usage manager is not full, then the task will not
752        // get called..
753        if (!memoryUsage.notifyCallbackWhenNotFull(sendMessagesWaitingForSpaceTask)) {
754            // so call it directly here.
755            sendMessagesWaitingForSpaceTask.run();
756        }
757    }
758
759    private final LinkedList<MessageContext> indexOrderedCursorUpdates = new LinkedList<>();
760
761    @Override
762    public void onAdd(MessageContext messageContext) {
763        synchronized (indexOrderedCursorUpdates) {
764            indexOrderedCursorUpdates.addLast(messageContext);
765        }
766    }
767
768    private void doPendingCursorAdditions() throws Exception {
769        LinkedList<MessageContext> orderedUpdates = new LinkedList<>();
770        sendLock.lockInterruptibly();
771        try {
772            synchronized (indexOrderedCursorUpdates) {
773                MessageContext candidate = indexOrderedCursorUpdates.peek();
774                while (candidate != null && candidate.message.getMessageId().getFutureOrSequenceLong() != null) {
775                    candidate = indexOrderedCursorUpdates.removeFirst();
776                    // check for duplicate adds suppressed by the store
777                    if (candidate.message.getMessageId().getFutureOrSequenceLong() instanceof Long && ((Long)candidate.message.getMessageId().getFutureOrSequenceLong()).compareTo(-1l) == 0) {
778                        LOG.warn("{} messageStore indicated duplicate add attempt for {}, suppressing duplicate dispatch", this, candidate.message.getMessageId());
779                    } else {
780                        orderedUpdates.add(candidate);
781                    }
782                    candidate = indexOrderedCursorUpdates.peek();
783                }
784            }
785            messagesLock.writeLock().lock();
786            try {
787                for (MessageContext messageContext : orderedUpdates) {
788                    if (!messages.addMessageLast(messageContext.message)) {
789                        // cursor suppressed a duplicate
790                        messageContext.duplicate = true;
791                    }
792                    if (messageContext.onCompletion != null) {
793                        messageContext.onCompletion.run();
794                    }
795                }
796            } finally {
797                messagesLock.writeLock().unlock();
798            }
799        } finally {
800            sendLock.unlock();
801        }
802        for (MessageContext messageContext : orderedUpdates) {
803            if (!messageContext.duplicate) {
804                messageSent(messageContext.context, messageContext.message);
805            }
806        }
807        orderedUpdates.clear();
808    }
809
810    final class CursorAddSync extends Synchronization {
811
812        private final MessageContext messageContext;
813
814        CursorAddSync(MessageContext messageContext) {
815            this.messageContext = messageContext;
816            this.messageContext.message.incrementReferenceCount();
817        }
818
819        @Override
820        public void afterCommit() throws Exception {
821            if (store != null && messageContext.message.isPersistent()) {
822                doPendingCursorAdditions();
823            } else {
824                cursorAdd(messageContext.message);
825                messageSent(messageContext.context, messageContext.message);
826            }
827            messageContext.message.decrementReferenceCount();
828        }
829
830        @Override
831        public void afterRollback() throws Exception {
832            messageContext.message.decrementReferenceCount();
833        }
834    }
835
836    void doMessageSend(final ProducerBrokerExchange producerExchange, final Message message) throws IOException,
837            Exception {
838        final ConnectionContext context = producerExchange.getConnectionContext();
839        ListenableFuture<Object> result = null;
840
841        producerExchange.incrementSend();
842        pendingSends.incrementAndGet();
843        do {
844            checkUsage(context, producerExchange, message);
845            message.getMessageId().setBrokerSequenceId(getDestinationSequenceId());
846            if (store != null && message.isPersistent()) {
847                message.getMessageId().setFutureOrSequenceLong(null);
848                try {
849                    //AMQ-6133 - don't store async if using persistJMSRedelivered
850                    //This flag causes a sync update later on dispatch which can cause a race
851                    //condition if the original add is processed after the update, which can cause
852                    //a duplicate message to be stored
853                    if (messages.isCacheEnabled() && !isPersistJMSRedelivered()) {
854                        result = store.asyncAddQueueMessage(context, message, isOptimizeStorage());
855                        result.addListener(new PendingMarshalUsageTracker(message));
856                    } else {
857                        store.addMessage(context, message);
858                    }
859                } catch (Exception e) {
860                    // we may have a store in inconsistent state, so reset the cursor
861                    // before restarting normal broker operations
862                    resetNeeded = true;
863                    pendingSends.decrementAndGet();
864                    throw e;
865                }
866            }
867
868            //Clear the unmarshalled state if the message is marshalled
869            //Persistent messages will always be marshalled but non-persistent may not be
870            //Specially non-persistent messages over the VM transport won't be
871            if (isReduceMemoryFootprint() && message.isMarshalled()) {
872                message.clearUnMarshalledState();
873            }
874            if(tryOrderedCursorAdd(message, context)) {
875                break;
876            }
877        } while (started.get());
878
879        if (result != null && message.isResponseRequired() && !result.isCancelled()) {
880            try {
881                result.get();
882            } catch (CancellationException e) {
883                // ignore - the task has been cancelled if the message
884                // has already been deleted
885            }
886        }
887    }
888
889    private boolean tryOrderedCursorAdd(Message message, ConnectionContext context) throws Exception {
890        boolean result = true;
891
892        if (context.isInTransaction()) {
893            context.getTransaction().addSynchronization(new CursorAddSync(new MessageContext(context, message, null)));
894        } else if (store != null && message.isPersistent()) {
895            doPendingCursorAdditions();
896        } else {
897            // no ordering issue with non persistent messages
898            result = tryCursorAdd(message);
899            messageSent(context, message);
900        }
901
902        return result;
903    }
904
905    private void checkUsage(ConnectionContext context,ProducerBrokerExchange producerBrokerExchange, Message message) throws ResourceAllocationException, IOException, InterruptedException {
906        if (message.isPersistent()) {
907            if (store != null && systemUsage.getStoreUsage().isFull(getStoreUsageHighWaterMark())) {
908                final String logMessage = "Persistent store is Full, " + getStoreUsageHighWaterMark() + "% of "
909                    + systemUsage.getStoreUsage().getLimit() + ". Stopping producer ("
910                    + message.getProducerId() + ") to prevent flooding "
911                    + getActiveMQDestination().getQualifiedName() + "."
912                    + " See http://activemq.apache.org/producer-flow-control.html for more info";
913
914                waitForSpace(context, producerBrokerExchange, systemUsage.getStoreUsage(), getStoreUsageHighWaterMark(), logMessage);
915            }
916        } else if (messages.getSystemUsage() != null && systemUsage.getTempUsage().isFull()) {
917            final String logMessage = "Temp Store is Full ("
918                    + systemUsage.getTempUsage().getPercentUsage() + "% of " + systemUsage.getTempUsage().getLimit()
919                    +"). Stopping producer (" + message.getProducerId()
920                + ") to prevent flooding " + getActiveMQDestination().getQualifiedName() + "."
921                + " See http://activemq.apache.org/producer-flow-control.html for more info";
922
923            waitForSpace(context, producerBrokerExchange, messages.getSystemUsage().getTempUsage(), logMessage);
924        }
925    }
926
927    private void expireMessages() {
928        LOG.debug("{} expiring messages ..", getActiveMQDestination().getQualifiedName());
929
930        // just track the insertion count
931        List<Message> browsedMessages = new InsertionCountList<Message>();
932        doBrowse(browsedMessages, this.getMaxExpirePageSize());
933        asyncWakeup();
934        LOG.debug("{} expiring messages done.", getActiveMQDestination().getQualifiedName());
935    }
936
937    @Override
938    public void gc() {
939    }
940
941    @Override
942    public void acknowledge(ConnectionContext context, Subscription sub, MessageAck ack, MessageReference node)
943            throws IOException {
944        messageConsumed(context, node);
945        if (store != null && node.isPersistent()) {
946            store.removeAsyncMessage(context, convertToNonRangedAck(ack, node));
947        }
948    }
949
950    Message loadMessage(MessageId messageId) throws IOException {
951        Message msg = null;
952        if (store != null) { // can be null for a temp q
953            msg = store.getMessage(messageId);
954            if (msg != null) {
955                msg.setRegionDestination(this);
956            }
957        }
958        return msg;
959    }
960
961    public long getPendingMessageSize() {
962        messagesLock.readLock().lock();
963        try{
964            return messages.messageSize();
965        } finally {
966            messagesLock.readLock().unlock();
967        }
968    }
969
970    public long getPendingMessageCount() {
971         return this.destinationStatistics.getMessages().getCount();
972    }
973
974    @Override
975    public String toString() {
976        return destination.getQualifiedName() + ", subscriptions=" + consumers.size()
977                + ", memory=" + memoryUsage.getPercentUsage() + "%, size=" + destinationStatistics.getMessages().getCount() + ", pending="
978                + indexOrderedCursorUpdates.size();
979    }
980
981    @Override
982    public void start() throws Exception {
983        if (started.compareAndSet(false, true)) {
984            if (memoryUsage != null) {
985                memoryUsage.start();
986            }
987            if (systemUsage.getStoreUsage() != null) {
988                systemUsage.getStoreUsage().start();
989            }
990            systemUsage.getMemoryUsage().addUsageListener(this);
991            messages.start();
992            if (getExpireMessagesPeriod() > 0) {
993                scheduler.executePeriodically(expireMessagesTask, getExpireMessagesPeriod());
994            }
995            doPageIn(false);
996        }
997    }
998
999    @Override
1000    public void stop() throws Exception {
1001        if (started.compareAndSet(true, false)) {
1002            if (taskRunner != null) {
1003                taskRunner.shutdown();
1004            }
1005            if (this.executor != null) {
1006                ThreadPoolUtils.shutdownNow(executor);
1007                executor = null;
1008            }
1009
1010            scheduler.cancel(expireMessagesTask);
1011
1012            if (flowControlTimeoutTask.isAlive()) {
1013                flowControlTimeoutTask.interrupt();
1014            }
1015
1016            if (messages != null) {
1017                messages.stop();
1018            }
1019
1020            for (MessageReference messageReference : pagedInMessages.values()) {
1021                messageReference.decrementReferenceCount();
1022            }
1023            pagedInMessages.clear();
1024
1025            systemUsage.getMemoryUsage().removeUsageListener(this);
1026            if (memoryUsage != null) {
1027                memoryUsage.stop();
1028            }
1029            if (systemUsage.getStoreUsage() != null) {
1030                systemUsage.getStoreUsage().stop();
1031            }
1032            if (store != null) {
1033                store.stop();
1034            }
1035        }
1036    }
1037
1038    // Properties
1039    // -------------------------------------------------------------------------
1040    @Override
1041    public ActiveMQDestination getActiveMQDestination() {
1042        return destination;
1043    }
1044
1045    public MessageGroupMap getMessageGroupOwners() {
1046        if (messageGroupOwners == null) {
1047            messageGroupOwners = getMessageGroupMapFactory().createMessageGroupMap();
1048            messageGroupOwners.setDestination(this);
1049        }
1050        return messageGroupOwners;
1051    }
1052
1053    public DispatchPolicy getDispatchPolicy() {
1054        return dispatchPolicy;
1055    }
1056
1057    public void setDispatchPolicy(DispatchPolicy dispatchPolicy) {
1058        this.dispatchPolicy = dispatchPolicy;
1059    }
1060
1061    public MessageGroupMapFactory getMessageGroupMapFactory() {
1062        return messageGroupMapFactory;
1063    }
1064
1065    public void setMessageGroupMapFactory(MessageGroupMapFactory messageGroupMapFactory) {
1066        this.messageGroupMapFactory = messageGroupMapFactory;
1067    }
1068
1069    public PendingMessageCursor getMessages() {
1070        return this.messages;
1071    }
1072
1073    public void setMessages(PendingMessageCursor messages) {
1074        this.messages = messages;
1075    }
1076
1077    public boolean isUseConsumerPriority() {
1078        return useConsumerPriority;
1079    }
1080
1081    public void setUseConsumerPriority(boolean useConsumerPriority) {
1082        this.useConsumerPriority = useConsumerPriority;
1083    }
1084
1085    public boolean isStrictOrderDispatch() {
1086        return strictOrderDispatch;
1087    }
1088
1089    public void setStrictOrderDispatch(boolean strictOrderDispatch) {
1090        this.strictOrderDispatch = strictOrderDispatch;
1091    }
1092
1093    public boolean isOptimizedDispatch() {
1094        return optimizedDispatch;
1095    }
1096
1097    public void setOptimizedDispatch(boolean optimizedDispatch) {
1098        this.optimizedDispatch = optimizedDispatch;
1099    }
1100
1101    public int getTimeBeforeDispatchStarts() {
1102        return timeBeforeDispatchStarts;
1103    }
1104
1105    public void setTimeBeforeDispatchStarts(int timeBeforeDispatchStarts) {
1106        this.timeBeforeDispatchStarts = timeBeforeDispatchStarts;
1107    }
1108
1109    public int getConsumersBeforeDispatchStarts() {
1110        return consumersBeforeDispatchStarts;
1111    }
1112
1113    public void setConsumersBeforeDispatchStarts(int consumersBeforeDispatchStarts) {
1114        this.consumersBeforeDispatchStarts = consumersBeforeDispatchStarts;
1115    }
1116
1117    public void setAllConsumersExclusiveByDefault(boolean allConsumersExclusiveByDefault) {
1118        this.allConsumersExclusiveByDefault = allConsumersExclusiveByDefault;
1119    }
1120
1121    public boolean isAllConsumersExclusiveByDefault() {
1122        return allConsumersExclusiveByDefault;
1123    }
1124
1125    public boolean isResetNeeded() {
1126        return resetNeeded;
1127    }
1128
1129    // Implementation methods
1130    // -------------------------------------------------------------------------
1131    private QueueMessageReference createMessageReference(Message message) {
1132        QueueMessageReference result = new IndirectMessageReference(message);
1133        return result;
1134    }
1135
1136    @Override
1137    public Message[] browse() {
1138        List<Message> browseList = new ArrayList<Message>();
1139        doBrowse(browseList, getMaxBrowsePageSize());
1140        return browseList.toArray(new Message[browseList.size()]);
1141    }
1142
1143    public void doBrowse(List<Message> browseList, int max) {
1144        final ConnectionContext connectionContext = createConnectionContext();
1145        try {
1146            int maxPageInAttempts = 1;
1147            if (max > 0) {
1148                messagesLock.readLock().lock();
1149                try {
1150                    maxPageInAttempts += (messages.size() / max);
1151                } finally {
1152                    messagesLock.readLock().unlock();
1153                }
1154                while (shouldPageInMoreForBrowse(max) && maxPageInAttempts-- > 0) {
1155                    pageInMessages(!memoryUsage.isFull(110), max);
1156                }
1157            }
1158            doBrowseList(browseList, max, dispatchPendingList, pagedInPendingDispatchLock, connectionContext, "redeliveredWaitingDispatch+pagedInPendingDispatch");
1159            doBrowseList(browseList, max, pagedInMessages, pagedInMessagesLock, connectionContext, "pagedInMessages");
1160
1161            // we need a store iterator to walk messages on disk, independent of the cursor which is tracking
1162            // the next message batch
1163        } catch (BrokerStoppedException ignored) {
1164        } catch (Exception e) {
1165            LOG.error("Problem retrieving message for browse", e);
1166        }
1167    }
1168
1169    protected void doBrowseList(List<Message> browseList, int max, PendingList list, ReentrantReadWriteLock lock, ConnectionContext connectionContext, String name) throws Exception {
1170        List<MessageReference> toExpire = new ArrayList<MessageReference>();
1171        lock.readLock().lock();
1172        try {
1173            addAll(list.values(), browseList, max, toExpire);
1174        } finally {
1175            lock.readLock().unlock();
1176        }
1177        for (MessageReference ref : toExpire) {
1178            if (broker.isExpired(ref)) {
1179                LOG.debug("expiring from {}: {}", name, ref);
1180                messageExpired(connectionContext, ref);
1181            } else {
1182                lock.writeLock().lock();
1183                try {
1184                    list.remove(ref);
1185                } finally {
1186                    lock.writeLock().unlock();
1187                }
1188                ref.decrementReferenceCount();
1189            }
1190        }
1191    }
1192
1193    private boolean shouldPageInMoreForBrowse(int max) {
1194        int alreadyPagedIn = 0;
1195        pagedInMessagesLock.readLock().lock();
1196        try {
1197            alreadyPagedIn = pagedInMessages.size();
1198        } finally {
1199            pagedInMessagesLock.readLock().unlock();
1200        }
1201        int messagesInQueue = alreadyPagedIn;
1202        messagesLock.readLock().lock();
1203        try {
1204            messagesInQueue += messages.size();
1205        } finally {
1206            messagesLock.readLock().unlock();
1207        }
1208
1209        LOG.trace("max {}, alreadyPagedIn {}, messagesCount {}, memoryUsage {}%", new Object[]{max, alreadyPagedIn, messagesInQueue, memoryUsage.getPercentUsage()});
1210        return (alreadyPagedIn < max)
1211                && (alreadyPagedIn < messagesInQueue)
1212                && messages.hasSpace();
1213    }
1214
1215    private void addAll(Collection<? extends MessageReference> refs, List<Message> l, int max,
1216            List<MessageReference> toExpire) throws Exception {
1217        for (Iterator<? extends MessageReference> i = refs.iterator(); i.hasNext() && l.size() < max;) {
1218            QueueMessageReference ref = (QueueMessageReference) i.next();
1219            if (ref.isExpired() && (ref.getLockOwner() == null)) {
1220                toExpire.add(ref);
1221            } else if (l.contains(ref.getMessage()) == false) {
1222                l.add(ref.getMessage());
1223            }
1224        }
1225    }
1226
1227    public QueueMessageReference getMessage(String id) {
1228        MessageId msgId = new MessageId(id);
1229        pagedInMessagesLock.readLock().lock();
1230        try {
1231            QueueMessageReference ref = (QueueMessageReference)this.pagedInMessages.get(msgId);
1232            if (ref != null) {
1233                return ref;
1234            }
1235        } finally {
1236            pagedInMessagesLock.readLock().unlock();
1237        }
1238        messagesLock.writeLock().lock();
1239        try{
1240            try {
1241                messages.reset();
1242                while (messages.hasNext()) {
1243                    MessageReference mr = messages.next();
1244                    QueueMessageReference qmr = createMessageReference(mr.getMessage());
1245                    qmr.decrementReferenceCount();
1246                    messages.rollback(qmr.getMessageId());
1247                    if (msgId.equals(qmr.getMessageId())) {
1248                        return qmr;
1249                    }
1250                }
1251            } finally {
1252                messages.release();
1253            }
1254        }finally {
1255            messagesLock.writeLock().unlock();
1256        }
1257        return null;
1258    }
1259
1260    public void purge() throws Exception {
1261        ConnectionContext c = createConnectionContext();
1262        List<MessageReference> list = null;
1263        try {
1264            sendLock.lock();
1265            long originalMessageCount = this.destinationStatistics.getMessages().getCount();
1266            do {
1267                doPageIn(true, false, getMaxPageSize());  // signal no expiry processing needed.
1268                pagedInMessagesLock.readLock().lock();
1269                try {
1270                    list = new ArrayList<MessageReference>(pagedInMessages.values());
1271                }finally {
1272                    pagedInMessagesLock.readLock().unlock();
1273                }
1274
1275                for (MessageReference ref : list) {
1276                    try {
1277                        QueueMessageReference r = (QueueMessageReference) ref;
1278                        removeMessage(c, r);
1279                    } catch (IOException e) {
1280                    }
1281                }
1282                // don't spin/hang if stats are out and there is nothing left in the
1283                // store
1284            } while (!list.isEmpty() && this.destinationStatistics.getMessages().getCount() > 0);
1285
1286            if (getMessages().getMessageAudit() != null) {
1287                getMessages().getMessageAudit().clear();
1288            }
1289
1290            if (this.destinationStatistics.getMessages().getCount() > 0) {
1291                LOG.warn("{} after purge of {} messages, message count stats report: {}", getActiveMQDestination().getQualifiedName(), originalMessageCount, this.destinationStatistics.getMessages().getCount());
1292            }
1293        } finally {
1294            sendLock.unlock();
1295        }
1296    }
1297
1298    @Override
1299    public void clearPendingMessages() {
1300        messagesLock.writeLock().lock();
1301        try {
1302            if (resetNeeded) {
1303                messages.gc();
1304                messages.reset();
1305                resetNeeded = false;
1306            } else {
1307                messages.rebase();
1308            }
1309            asyncWakeup();
1310        } finally {
1311            messagesLock.writeLock().unlock();
1312        }
1313    }
1314
1315    /**
1316     * Removes the message matching the given messageId
1317     */
1318    public boolean removeMessage(String messageId) throws Exception {
1319        return removeMatchingMessages(createMessageIdFilter(messageId), 1) > 0;
1320    }
1321
1322    /**
1323     * Removes the messages matching the given selector
1324     *
1325     * @return the number of messages removed
1326     */
1327    public int removeMatchingMessages(String selector) throws Exception {
1328        return removeMatchingMessages(selector, -1);
1329    }
1330
1331    /**
1332     * Removes the messages matching the given selector up to the maximum number
1333     * of matched messages
1334     *
1335     * @return the number of messages removed
1336     */
1337    public int removeMatchingMessages(String selector, int maximumMessages) throws Exception {
1338        return removeMatchingMessages(createSelectorFilter(selector), maximumMessages);
1339    }
1340
1341    /**
1342     * Removes the messages matching the given filter up to the maximum number
1343     * of matched messages
1344     *
1345     * @return the number of messages removed
1346     */
1347    public int removeMatchingMessages(MessageReferenceFilter filter, int maximumMessages) throws Exception {
1348        int movedCounter = 0;
1349        Set<MessageReference> set = new LinkedHashSet<MessageReference>();
1350        ConnectionContext context = createConnectionContext();
1351        do {
1352            doPageIn(true);
1353            pagedInMessagesLock.readLock().lock();
1354            try {
1355                set.addAll(pagedInMessages.values());
1356            } finally {
1357                pagedInMessagesLock.readLock().unlock();
1358            }
1359            List<MessageReference> list = new ArrayList<MessageReference>(set);
1360            for (MessageReference ref : list) {
1361                IndirectMessageReference r = (IndirectMessageReference) ref;
1362                if (filter.evaluate(context, r)) {
1363
1364                    removeMessage(context, r);
1365                    set.remove(r);
1366                    if (++movedCounter >= maximumMessages && maximumMessages > 0) {
1367                        return movedCounter;
1368                    }
1369                }
1370            }
1371        } while (set.size() < this.destinationStatistics.getMessages().getCount());
1372        return movedCounter;
1373    }
1374
1375    /**
1376     * Copies the message matching the given messageId
1377     */
1378    public boolean copyMessageTo(ConnectionContext context, String messageId, ActiveMQDestination dest)
1379            throws Exception {
1380        return copyMatchingMessages(context, createMessageIdFilter(messageId), dest, 1) > 0;
1381    }
1382
1383    /**
1384     * Copies the messages matching the given selector
1385     *
1386     * @return the number of messages copied
1387     */
1388    public int copyMatchingMessagesTo(ConnectionContext context, String selector, ActiveMQDestination dest)
1389            throws Exception {
1390        return copyMatchingMessagesTo(context, selector, dest, -1);
1391    }
1392
1393    /**
1394     * Copies the messages matching the given selector up to the maximum number
1395     * of matched messages
1396     *
1397     * @return the number of messages copied
1398     */
1399    public int copyMatchingMessagesTo(ConnectionContext context, String selector, ActiveMQDestination dest,
1400            int maximumMessages) throws Exception {
1401        return copyMatchingMessages(context, createSelectorFilter(selector), dest, maximumMessages);
1402    }
1403
1404    /**
1405     * Copies the messages matching the given filter up to the maximum number of
1406     * matched messages
1407     *
1408     * @return the number of messages copied
1409     */
1410    public int copyMatchingMessages(ConnectionContext context, MessageReferenceFilter filter, ActiveMQDestination dest,
1411            int maximumMessages) throws Exception {
1412
1413        if (destination.equals(dest)) {
1414            return 0;
1415        }
1416
1417        int movedCounter = 0;
1418        int count = 0;
1419        Set<MessageReference> set = new LinkedHashSet<MessageReference>();
1420        do {
1421            int oldMaxSize = getMaxPageSize();
1422            setMaxPageSize((int) this.destinationStatistics.getMessages().getCount());
1423            doPageIn(true);
1424            setMaxPageSize(oldMaxSize);
1425            pagedInMessagesLock.readLock().lock();
1426            try {
1427                set.addAll(pagedInMessages.values());
1428            } finally {
1429                pagedInMessagesLock.readLock().unlock();
1430            }
1431            List<MessageReference> list = new ArrayList<MessageReference>(set);
1432            for (MessageReference ref : list) {
1433                IndirectMessageReference r = (IndirectMessageReference) ref;
1434                if (filter.evaluate(context, r)) {
1435
1436                    r.incrementReferenceCount();
1437                    try {
1438                        Message m = r.getMessage();
1439                        BrokerSupport.resend(context, m, dest);
1440                        if (++movedCounter >= maximumMessages && maximumMessages > 0) {
1441                            return movedCounter;
1442                        }
1443                    } finally {
1444                        r.decrementReferenceCount();
1445                    }
1446                }
1447                count++;
1448            }
1449        } while (count < this.destinationStatistics.getMessages().getCount());
1450        return movedCounter;
1451    }
1452
1453    /**
1454     * Move a message
1455     *
1456     * @param context
1457     *            connection context
1458     * @param m
1459     *            QueueMessageReference
1460     * @param dest
1461     *            ActiveMQDestination
1462     * @throws Exception
1463     */
1464    public boolean moveMessageTo(ConnectionContext context, QueueMessageReference m, ActiveMQDestination dest) throws Exception {
1465        BrokerSupport.resend(context, m.getMessage(), dest);
1466        removeMessage(context, m);
1467        messagesLock.writeLock().lock();
1468        try {
1469            messages.rollback(m.getMessageId());
1470            if (isDLQ()) {
1471                DeadLetterStrategy stratagy = getDeadLetterStrategy();
1472                stratagy.rollback(m.getMessage());
1473            }
1474        } finally {
1475            messagesLock.writeLock().unlock();
1476        }
1477        return true;
1478    }
1479
1480    /**
1481     * Moves the message matching the given messageId
1482     */
1483    public boolean moveMessageTo(ConnectionContext context, String messageId, ActiveMQDestination dest)
1484            throws Exception {
1485        return moveMatchingMessagesTo(context, createMessageIdFilter(messageId), dest, 1) > 0;
1486    }
1487
1488    /**
1489     * Moves the messages matching the given selector
1490     *
1491     * @return the number of messages removed
1492     */
1493    public int moveMatchingMessagesTo(ConnectionContext context, String selector, ActiveMQDestination dest)
1494            throws Exception {
1495        return moveMatchingMessagesTo(context, selector, dest, Integer.MAX_VALUE);
1496    }
1497
1498    /**
1499     * Moves the messages matching the given selector up to the maximum number
1500     * of matched messages
1501     */
1502    public int moveMatchingMessagesTo(ConnectionContext context, String selector, ActiveMQDestination dest,
1503            int maximumMessages) throws Exception {
1504        return moveMatchingMessagesTo(context, createSelectorFilter(selector), dest, maximumMessages);
1505    }
1506
1507    /**
1508     * Moves the messages matching the given filter up to the maximum number of
1509     * matched messages
1510     */
1511    public int moveMatchingMessagesTo(ConnectionContext context, MessageReferenceFilter filter,
1512            ActiveMQDestination dest, int maximumMessages) throws Exception {
1513
1514        if (destination.equals(dest)) {
1515            return 0;
1516        }
1517
1518        int movedCounter = 0;
1519        Set<MessageReference> set = new LinkedHashSet<MessageReference>();
1520        do {
1521            doPageIn(true);
1522            pagedInMessagesLock.readLock().lock();
1523            try {
1524                set.addAll(pagedInMessages.values());
1525            } finally {
1526                pagedInMessagesLock.readLock().unlock();
1527            }
1528            List<MessageReference> list = new ArrayList<MessageReference>(set);
1529            for (MessageReference ref : list) {
1530                if (filter.evaluate(context, ref)) {
1531                    // We should only move messages that can be locked.
1532                    moveMessageTo(context, (QueueMessageReference)ref, dest);
1533                    set.remove(ref);
1534                    if (++movedCounter >= maximumMessages && maximumMessages > 0) {
1535                        return movedCounter;
1536                    }
1537                }
1538            }
1539        } while (set.size() < this.destinationStatistics.getMessages().getCount() && set.size() < maximumMessages);
1540        return movedCounter;
1541    }
1542
1543    public int retryMessages(ConnectionContext context, int maximumMessages) throws Exception {
1544        if (!isDLQ()) {
1545            throw new Exception("Retry of message is only possible on Dead Letter Queues!");
1546        }
1547        int restoredCounter = 0;
1548        Set<MessageReference> set = new LinkedHashSet<MessageReference>();
1549        do {
1550            doPageIn(true);
1551            pagedInMessagesLock.readLock().lock();
1552            try {
1553                set.addAll(pagedInMessages.values());
1554            } finally {
1555                pagedInMessagesLock.readLock().unlock();
1556            }
1557            List<MessageReference> list = new ArrayList<MessageReference>(set);
1558            for (MessageReference ref : list) {
1559                if (ref.getMessage().getOriginalDestination() != null) {
1560
1561                    moveMessageTo(context, (QueueMessageReference)ref, ref.getMessage().getOriginalDestination());
1562                    set.remove(ref);
1563                    if (++restoredCounter >= maximumMessages && maximumMessages > 0) {
1564                        return restoredCounter;
1565                    }
1566                }
1567            }
1568        } while (set.size() < this.destinationStatistics.getMessages().getCount() && set.size() < maximumMessages);
1569        return restoredCounter;
1570    }
1571
1572    /**
1573     * @return true if we would like to iterate again
1574     * @see org.apache.activemq.thread.Task#iterate()
1575     */
1576    @Override
1577    public boolean iterate() {
1578        MDC.put("activemq.destination", getName());
1579        boolean pageInMoreMessages = false;
1580        synchronized (iteratingMutex) {
1581
1582            // If optimize dispatch is on or this is a slave this method could be called recursively
1583            // we set this state value to short-circuit wakeup in those cases to avoid that as it
1584            // could lead to errors.
1585            iterationRunning = true;
1586
1587            // do early to allow dispatch of these waiting messages
1588            synchronized (messagesWaitingForSpace) {
1589                Iterator<Runnable> it = messagesWaitingForSpace.values().iterator();
1590                while (it.hasNext()) {
1591                    if (!memoryUsage.isFull()) {
1592                        Runnable op = it.next();
1593                        it.remove();
1594                        op.run();
1595                    } else {
1596                        registerCallbackForNotFullNotification();
1597                        break;
1598                    }
1599                }
1600            }
1601
1602            if (firstConsumer) {
1603                firstConsumer = false;
1604                try {
1605                    if (consumersBeforeDispatchStarts > 0) {
1606                        int timeout = 1000; // wait one second by default if
1607                                            // consumer count isn't reached
1608                        if (timeBeforeDispatchStarts > 0) {
1609                            timeout = timeBeforeDispatchStarts;
1610                        }
1611                        if (consumersBeforeStartsLatch.await(timeout, TimeUnit.MILLISECONDS)) {
1612                            LOG.debug("{} consumers subscribed. Starting dispatch.", consumers.size());
1613                        } else {
1614                            LOG.debug("{} ms elapsed and {} consumers subscribed. Starting dispatch.", timeout, consumers.size());
1615                        }
1616                    }
1617                    if (timeBeforeDispatchStarts > 0 && consumersBeforeDispatchStarts <= 0) {
1618                        iteratingMutex.wait(timeBeforeDispatchStarts);
1619                        LOG.debug("{} ms elapsed. Starting dispatch.", timeBeforeDispatchStarts);
1620                    }
1621                } catch (Exception e) {
1622                    LOG.error(e.toString());
1623                }
1624            }
1625
1626            messagesLock.readLock().lock();
1627            try{
1628                pageInMoreMessages |= !messages.isEmpty();
1629            } finally {
1630                messagesLock.readLock().unlock();
1631            }
1632
1633            pagedInPendingDispatchLock.readLock().lock();
1634            try {
1635                pageInMoreMessages |= !dispatchPendingList.isEmpty();
1636            } finally {
1637                pagedInPendingDispatchLock.readLock().unlock();
1638            }
1639
1640            boolean hasBrowsers = !browserDispatches.isEmpty();
1641
1642            if (pageInMoreMessages || hasBrowsers || !dispatchPendingList.hasRedeliveries()) {
1643                try {
1644                    pageInMessages(hasBrowsers && getMaxBrowsePageSize() > 0, getMaxPageSize());
1645                } catch (Throwable e) {
1646                    LOG.error("Failed to page in more queue messages ", e);
1647                }
1648            }
1649
1650            if (hasBrowsers) {
1651                PendingList messagesInMemory = isPrioritizedMessages() ?
1652                        new PrioritizedPendingList() : new OrderedPendingList();
1653                pagedInMessagesLock.readLock().lock();
1654                try {
1655                    messagesInMemory.addAll(pagedInMessages);
1656                } finally {
1657                    pagedInMessagesLock.readLock().unlock();
1658                }
1659
1660                Iterator<BrowserDispatch> browsers = browserDispatches.iterator();
1661                while (browsers.hasNext()) {
1662                    BrowserDispatch browserDispatch = browsers.next();
1663                    try {
1664                        MessageEvaluationContext msgContext = new NonCachedMessageEvaluationContext();
1665                        msgContext.setDestination(destination);
1666
1667                        QueueBrowserSubscription browser = browserDispatch.getBrowser();
1668
1669                        LOG.debug("dispatch to browser: {}, already dispatched/paged count: {}", browser, messagesInMemory.size());
1670                        boolean added = false;
1671                        for (MessageReference node : messagesInMemory) {
1672                            if (!((QueueMessageReference)node).isAcked() && !browser.isDuplicate(node.getMessageId()) && !browser.atMax()) {
1673                                msgContext.setMessageReference(node);
1674                                if (browser.matches(node, msgContext)) {
1675                                    browser.add(node);
1676                                    added = true;
1677                                }
1678                            }
1679                        }
1680                        // are we done browsing? no new messages paged
1681                        if (!added || browser.atMax()) {
1682                            browser.decrementQueueRef();
1683                            browserDispatches.remove(browserDispatch);
1684                        } else {
1685                            wakeup();
1686                        }
1687                    } catch (Exception e) {
1688                        LOG.warn("exception on dispatch to browser: {}", browserDispatch.getBrowser(), e);
1689                    }
1690                }
1691            }
1692
1693            if (pendingWakeups.get() > 0) {
1694                pendingWakeups.decrementAndGet();
1695            }
1696            MDC.remove("activemq.destination");
1697            iterationRunning = false;
1698
1699            return pendingWakeups.get() > 0;
1700        }
1701    }
1702
1703    public void pauseDispatch() {
1704        dispatchSelector.pause();
1705    }
1706
1707    public void resumeDispatch() {
1708        dispatchSelector.resume();
1709        wakeup();
1710    }
1711
1712    public boolean isDispatchPaused() {
1713        return dispatchSelector.isPaused();
1714    }
1715
1716    protected MessageReferenceFilter createMessageIdFilter(final String messageId) {
1717        return new MessageReferenceFilter() {
1718            @Override
1719            public boolean evaluate(ConnectionContext context, MessageReference r) {
1720                return messageId.equals(r.getMessageId().toString());
1721            }
1722
1723            @Override
1724            public String toString() {
1725                return "MessageIdFilter: " + messageId;
1726            }
1727        };
1728    }
1729
1730    protected MessageReferenceFilter createSelectorFilter(String selector) throws InvalidSelectorException {
1731
1732        if (selector == null || selector.isEmpty()) {
1733            return new MessageReferenceFilter() {
1734
1735                @Override
1736                public boolean evaluate(ConnectionContext context, MessageReference messageReference) throws JMSException {
1737                    return true;
1738                }
1739            };
1740        }
1741
1742        final BooleanExpression selectorExpression = SelectorParser.parse(selector);
1743
1744        return new MessageReferenceFilter() {
1745            @Override
1746            public boolean evaluate(ConnectionContext context, MessageReference r) throws JMSException {
1747                MessageEvaluationContext messageEvaluationContext = context.getMessageEvaluationContext();
1748
1749                messageEvaluationContext.setMessageReference(r);
1750                if (messageEvaluationContext.getDestination() == null) {
1751                    messageEvaluationContext.setDestination(getActiveMQDestination());
1752                }
1753
1754                return selectorExpression.matches(messageEvaluationContext);
1755            }
1756        };
1757    }
1758
1759    protected void removeMessage(ConnectionContext c, QueueMessageReference r) throws IOException {
1760        removeMessage(c, null, r);
1761        pagedInPendingDispatchLock.writeLock().lock();
1762        try {
1763            dispatchPendingList.remove(r);
1764        } finally {
1765            pagedInPendingDispatchLock.writeLock().unlock();
1766        }
1767    }
1768
1769    protected void removeMessage(ConnectionContext c, Subscription subs, QueueMessageReference r) throws IOException {
1770        MessageAck ack = new MessageAck();
1771        ack.setAckType(MessageAck.STANDARD_ACK_TYPE);
1772        ack.setDestination(destination);
1773        ack.setMessageID(r.getMessageId());
1774        removeMessage(c, subs, r, ack);
1775    }
1776
1777    protected void removeMessage(ConnectionContext context, Subscription sub, final QueueMessageReference reference,
1778            MessageAck ack) throws IOException {
1779        LOG.trace("ack of {} with {}", reference.getMessageId(), ack);
1780        // This sends the ack the the journal..
1781        if (!ack.isInTransaction()) {
1782            acknowledge(context, sub, ack, reference);
1783            dropMessage(reference);
1784        } else {
1785            try {
1786                acknowledge(context, sub, ack, reference);
1787            } finally {
1788                context.getTransaction().addSynchronization(new Synchronization() {
1789
1790                    @Override
1791                    public void afterCommit() throws Exception {
1792                        dropMessage(reference);
1793                        wakeup();
1794                    }
1795
1796                    @Override
1797                    public void afterRollback() throws Exception {
1798                        reference.setAcked(false);
1799                        wakeup();
1800                    }
1801                });
1802            }
1803        }
1804        if (ack.isPoisonAck() || (sub != null && sub.getConsumerInfo().isNetworkSubscription())) {
1805            // message gone to DLQ, is ok to allow redelivery
1806            messagesLock.writeLock().lock();
1807            try {
1808                messages.rollback(reference.getMessageId());
1809            } finally {
1810                messagesLock.writeLock().unlock();
1811            }
1812            if (sub != null && sub.getConsumerInfo().isNetworkSubscription()) {
1813                getDestinationStatistics().getForwards().increment();
1814            }
1815        }
1816        // after successful store update
1817        reference.setAcked(true);
1818    }
1819
1820    private void dropMessage(QueueMessageReference reference) {
1821        //use dropIfLive so we only process the statistics at most one time
1822        if (reference.dropIfLive()) {
1823            getDestinationStatistics().getDequeues().increment();
1824            getDestinationStatistics().getMessages().decrement();
1825            pagedInMessagesLock.writeLock().lock();
1826            try {
1827                pagedInMessages.remove(reference);
1828            } finally {
1829                pagedInMessagesLock.writeLock().unlock();
1830            }
1831        }
1832    }
1833
1834    public void messageExpired(ConnectionContext context, MessageReference reference) {
1835        messageExpired(context, null, reference);
1836    }
1837
1838    @Override
1839    public void messageExpired(ConnectionContext context, Subscription subs, MessageReference reference) {
1840        LOG.debug("message expired: {}", reference);
1841        broker.messageExpired(context, reference, subs);
1842        destinationStatistics.getExpired().increment();
1843        try {
1844            removeMessage(context, subs, (QueueMessageReference) reference);
1845            messagesLock.writeLock().lock();
1846            try {
1847                messages.rollback(reference.getMessageId());
1848            } finally {
1849                messagesLock.writeLock().unlock();
1850            }
1851        } catch (IOException e) {
1852            LOG.error("Failed to remove expired Message from the store ", e);
1853        }
1854    }
1855
1856    private final boolean cursorAdd(final Message msg) throws Exception {
1857        messagesLock.writeLock().lock();
1858        try {
1859            return messages.addMessageLast(msg);
1860        } finally {
1861            messagesLock.writeLock().unlock();
1862        }
1863    }
1864
1865    private final boolean tryCursorAdd(final Message msg) throws Exception {
1866        messagesLock.writeLock().lock();
1867        try {
1868            return messages.tryAddMessageLast(msg, 50);
1869        } finally {
1870            messagesLock.writeLock().unlock();
1871        }
1872    }
1873
1874    final void messageSent(final ConnectionContext context, final Message msg) throws Exception {
1875        pendingSends.decrementAndGet();
1876        destinationStatistics.getEnqueues().increment();
1877        destinationStatistics.getMessages().increment();
1878        destinationStatistics.getMessageSize().addSize(msg.getSize());
1879        messageDelivered(context, msg);
1880        consumersLock.readLock().lock();
1881        try {
1882            if (consumers.isEmpty()) {
1883                onMessageWithNoConsumers(context, msg);
1884            }
1885        }finally {
1886            consumersLock.readLock().unlock();
1887        }
1888        LOG.debug("{} Message {} sent to {}", new Object[]{ broker.getBrokerName(), msg.getMessageId(), this.destination });
1889        wakeup();
1890    }
1891
1892    @Override
1893    public void wakeup() {
1894        if (optimizedDispatch && !iterationRunning) {
1895            iterate();
1896            pendingWakeups.incrementAndGet();
1897        } else {
1898            asyncWakeup();
1899        }
1900    }
1901
1902    private void asyncWakeup() {
1903        try {
1904            pendingWakeups.incrementAndGet();
1905            this.taskRunner.wakeup();
1906        } catch (InterruptedException e) {
1907            LOG.warn("Async task runner failed to wakeup ", e);
1908        }
1909    }
1910
1911    private void doPageIn(boolean force) throws Exception {
1912        doPageIn(force, true, getMaxPageSize());
1913    }
1914
1915    private void doPageIn(boolean force, boolean processExpired, int maxPageSize) throws Exception {
1916        PendingList newlyPaged = doPageInForDispatch(force, processExpired, maxPageSize);
1917        pagedInPendingDispatchLock.writeLock().lock();
1918        try {
1919            if (dispatchPendingList.isEmpty()) {
1920                dispatchPendingList.addAll(newlyPaged);
1921
1922            } else {
1923                for (MessageReference qmr : newlyPaged) {
1924                    if (!dispatchPendingList.contains(qmr)) {
1925                        dispatchPendingList.addMessageLast(qmr);
1926                    }
1927                }
1928            }
1929        } finally {
1930            pagedInPendingDispatchLock.writeLock().unlock();
1931        }
1932    }
1933
1934    private PendingList doPageInForDispatch(boolean force, boolean processExpired, int maxPageSize) throws Exception {
1935        List<QueueMessageReference> result = null;
1936        PendingList resultList = null;
1937
1938        int toPageIn = maxPageSize;
1939        messagesLock.readLock().lock();
1940        try {
1941            toPageIn = Math.min(toPageIn, messages.size());
1942        } finally {
1943            messagesLock.readLock().unlock();
1944        }
1945        int pagedInPendingSize = 0;
1946        pagedInPendingDispatchLock.readLock().lock();
1947        try {
1948            pagedInPendingSize = dispatchPendingList.size();
1949        } finally {
1950            pagedInPendingDispatchLock.readLock().unlock();
1951        }
1952        if (isLazyDispatch() && !force) {
1953            // Only page in the minimum number of messages which can be
1954            // dispatched immediately.
1955            toPageIn = Math.min(toPageIn, getConsumerMessageCountBeforeFull());
1956        }
1957
1958        if (LOG.isDebugEnabled()) {
1959            LOG.debug("{} toPageIn: {}, force:{}, Inflight: {}, pagedInMessages.size {}, pagedInPendingDispatch.size {}, enqueueCount: {}, dequeueCount: {}, memUsage:{}, maxPageSize:{}",
1960                    new Object[]{
1961                            this,
1962                            toPageIn,
1963                            force,
1964                            destinationStatistics.getInflight().getCount(),
1965                            pagedInMessages.size(),
1966                            pagedInPendingSize,
1967                            destinationStatistics.getEnqueues().getCount(),
1968                            destinationStatistics.getDequeues().getCount(),
1969                            getMemoryUsage().getUsage(),
1970                            maxPageSize
1971                    });
1972        }
1973
1974        if (toPageIn > 0 && (force || (haveRealConsumer() && pagedInPendingSize < maxPageSize))) {
1975            int count = 0;
1976            result = new ArrayList<QueueMessageReference>(toPageIn);
1977            messagesLock.writeLock().lock();
1978            try {
1979                try {
1980                    messages.setMaxBatchSize(toPageIn);
1981                    messages.reset();
1982                    while (count < toPageIn && messages.hasNext()) {
1983                        MessageReference node = messages.next();
1984                        messages.remove();
1985
1986                        QueueMessageReference ref = createMessageReference(node.getMessage());
1987                        if (processExpired && ref.isExpired()) {
1988                            if (broker.isExpired(ref)) {
1989                                messageExpired(createConnectionContext(), ref);
1990                            } else {
1991                                ref.decrementReferenceCount();
1992                            }
1993                        } else {
1994                            result.add(ref);
1995                            count++;
1996                        }
1997                    }
1998                } finally {
1999                    messages.release();
2000                }
2001            } finally {
2002                messagesLock.writeLock().unlock();
2003            }
2004            // Only add new messages, not already pagedIn to avoid multiple
2005            // dispatch attempts
2006            pagedInMessagesLock.writeLock().lock();
2007            try {
2008                if(isPrioritizedMessages()) {
2009                    resultList = new PrioritizedPendingList();
2010                } else {
2011                    resultList = new OrderedPendingList();
2012                }
2013                for (QueueMessageReference ref : result) {
2014                    if (!pagedInMessages.contains(ref)) {
2015                        pagedInMessages.addMessageLast(ref);
2016                        resultList.addMessageLast(ref);
2017                    } else {
2018                        ref.decrementReferenceCount();
2019                        // store should have trapped duplicate in it's index, or cursor audit trapped insert
2020                        // or producerBrokerExchange suppressed send.
2021                        // note: jdbc store will not trap unacked messages as a duplicate b/c it gives each message a unique sequence id
2022                        LOG.warn("{}, duplicate message {} - {} from cursor, is cursor audit disabled or too constrained? Redirecting to dlq", this, ref.getMessageId(), ref.getMessage().getMessageId().getFutureOrSequenceLong());
2023                        if (store != null) {
2024                            ConnectionContext connectionContext = createConnectionContext();
2025                            dropMessage(ref);
2026                            if (gotToTheStore(ref.getMessage())) {
2027                                LOG.debug("Duplicate message {} from cursor, removing from store", this, ref.getMessage());
2028                                store.removeMessage(connectionContext, new MessageAck(ref.getMessage(), MessageAck.POSION_ACK_TYPE, 1));
2029                            }
2030                            broker.getRoot().sendToDeadLetterQueue(connectionContext, ref.getMessage(), null, new Throwable("duplicate paged in from cursor for " + destination));
2031                        }
2032                    }
2033                }
2034            } finally {
2035                pagedInMessagesLock.writeLock().unlock();
2036            }
2037        } else {
2038            // Avoid return null list, if condition is not validated
2039            resultList = new OrderedPendingList();
2040        }
2041
2042        return resultList;
2043    }
2044
2045    private final boolean haveRealConsumer() {
2046        return consumers.size() - browserDispatches.size() > 0;
2047    }
2048
2049    private void doDispatch(PendingList list) throws Exception {
2050        boolean doWakeUp = false;
2051
2052        pagedInPendingDispatchLock.writeLock().lock();
2053        try {
2054            if (isPrioritizedMessages() && !dispatchPendingList.isEmpty() && list != null && !list.isEmpty()) {
2055                // merge all to select priority order
2056                for (MessageReference qmr : list) {
2057                    if (!dispatchPendingList.contains(qmr)) {
2058                        dispatchPendingList.addMessageLast(qmr);
2059                    }
2060                }
2061                list = null;
2062            }
2063
2064            doActualDispatch(dispatchPendingList);
2065            // and now see if we can dispatch the new stuff.. and append to the pending
2066            // list anything that does not actually get dispatched.
2067            if (list != null && !list.isEmpty()) {
2068                if (dispatchPendingList.isEmpty()) {
2069                    dispatchPendingList.addAll(doActualDispatch(list));
2070                } else {
2071                    for (MessageReference qmr : list) {
2072                        if (!dispatchPendingList.contains(qmr)) {
2073                            dispatchPendingList.addMessageLast(qmr);
2074                        }
2075                    }
2076                    doWakeUp = true;
2077                }
2078            }
2079        } finally {
2080            pagedInPendingDispatchLock.writeLock().unlock();
2081        }
2082
2083        if (doWakeUp) {
2084            // avoid lock order contention
2085            asyncWakeup();
2086        }
2087    }
2088
2089    /**
2090     * @return list of messages that could get dispatched to consumers if they
2091     *         were not full.
2092     */
2093    private PendingList doActualDispatch(PendingList list) throws Exception {
2094        List<Subscription> consumers;
2095        consumersLock.readLock().lock();
2096
2097        try {
2098            if (this.consumers.isEmpty()) {
2099                // slave dispatch happens in processDispatchNotification
2100                return list;
2101            }
2102            consumers = new ArrayList<Subscription>(this.consumers);
2103        } finally {
2104            consumersLock.readLock().unlock();
2105        }
2106
2107        Set<Subscription> fullConsumers = new HashSet<Subscription>(this.consumers.size());
2108
2109        for (Iterator<MessageReference> iterator = list.iterator(); iterator.hasNext();) {
2110
2111            MessageReference node = iterator.next();
2112            Subscription target = null;
2113            for (Subscription s : consumers) {
2114                if (s instanceof QueueBrowserSubscription) {
2115                    continue;
2116                }
2117                if (!fullConsumers.contains(s)) {
2118                    if (!s.isFull()) {
2119                        if (dispatchSelector.canSelect(s, node) && assignMessageGroup(s, (QueueMessageReference)node) && !((QueueMessageReference) node).isAcked() ) {
2120                            // Dispatch it.
2121                            s.add(node);
2122                            LOG.trace("assigned {} to consumer {}", node.getMessageId(), s.getConsumerInfo().getConsumerId());
2123                            iterator.remove();
2124                            target = s;
2125                            break;
2126                        }
2127                    } else {
2128                        // no further dispatch of list to a full consumer to
2129                        // avoid out of order message receipt
2130                        fullConsumers.add(s);
2131                        LOG.trace("Subscription full {}", s);
2132                    }
2133                }
2134            }
2135
2136            if (target == null && node.isDropped()) {
2137                iterator.remove();
2138            }
2139
2140            // return if there are no consumers or all consumers are full
2141            if (target == null && consumers.size() == fullConsumers.size()) {
2142                return list;
2143            }
2144
2145            // If it got dispatched, rotate the consumer list to get round robin
2146            // distribution.
2147            if (target != null && !strictOrderDispatch && consumers.size() > 1
2148                    && !dispatchSelector.isExclusiveConsumer(target)) {
2149                consumersLock.writeLock().lock();
2150                try {
2151                    if (removeFromConsumerList(target)) {
2152                        addToConsumerList(target);
2153                        consumers = new ArrayList<Subscription>(this.consumers);
2154                    }
2155                } finally {
2156                    consumersLock.writeLock().unlock();
2157                }
2158            }
2159        }
2160
2161        return list;
2162    }
2163
2164    protected boolean assignMessageGroup(Subscription subscription, QueueMessageReference node) throws Exception {
2165        boolean result = true;
2166        // Keep message groups together.
2167        String groupId = node.getGroupID();
2168        int sequence = node.getGroupSequence();
2169        if (groupId != null) {
2170
2171            MessageGroupMap messageGroupOwners = getMessageGroupOwners();
2172            // If we can own the first, then no-one else should own the
2173            // rest.
2174            if (sequence == 1) {
2175                assignGroup(subscription, messageGroupOwners, node, groupId);
2176            } else {
2177
2178                // Make sure that the previous owner is still valid, we may
2179                // need to become the new owner.
2180                ConsumerId groupOwner;
2181
2182                groupOwner = messageGroupOwners.get(groupId);
2183                if (groupOwner == null) {
2184                    assignGroup(subscription, messageGroupOwners, node, groupId);
2185                } else {
2186                    if (groupOwner.equals(subscription.getConsumerInfo().getConsumerId())) {
2187                        // A group sequence < 1 is an end of group signal.
2188                        if (sequence < 0) {
2189                            messageGroupOwners.removeGroup(groupId);
2190                            subscription.getConsumerInfo().decrementAssignedGroupCount(destination);
2191                        }
2192                    } else {
2193                        result = false;
2194                    }
2195                }
2196            }
2197        }
2198
2199        return result;
2200    }
2201
2202    protected void assignGroup(Subscription subs, MessageGroupMap messageGroupOwners, MessageReference n, String groupId) throws IOException {
2203        messageGroupOwners.put(groupId, subs.getConsumerInfo().getConsumerId());
2204        Message message = n.getMessage();
2205        message.setJMSXGroupFirstForConsumer(true);
2206        subs.getConsumerInfo().incrementAssignedGroupCount(destination);
2207    }
2208
2209    protected void pageInMessages(boolean force, int maxPageSize) throws Exception {
2210        doDispatch(doPageInForDispatch(force, true, maxPageSize));
2211    }
2212
2213    private void addToConsumerList(Subscription sub) {
2214        if (useConsumerPriority) {
2215            consumers.add(sub);
2216            Collections.sort(consumers, orderedCompare);
2217        } else {
2218            consumers.add(sub);
2219        }
2220    }
2221
2222    private boolean removeFromConsumerList(Subscription sub) {
2223        return consumers.remove(sub);
2224    }
2225
2226    private int getConsumerMessageCountBeforeFull() throws Exception {
2227        int total = 0;
2228        consumersLock.readLock().lock();
2229        try {
2230            for (Subscription s : consumers) {
2231                if (s.isBrowser()) {
2232                    continue;
2233                }
2234                int countBeforeFull = s.countBeforeFull();
2235                total += countBeforeFull;
2236            }
2237        } finally {
2238            consumersLock.readLock().unlock();
2239        }
2240        return total;
2241    }
2242
2243    /*
2244     * In slave mode, dispatch is ignored till we get this notification as the
2245     * dispatch process is non deterministic between master and slave. On a
2246     * notification, the actual dispatch to the subscription (as chosen by the
2247     * master) is completed. (non-Javadoc)
2248     * @see
2249     * org.apache.activemq.broker.region.BaseDestination#processDispatchNotification
2250     * (org.apache.activemq.command.MessageDispatchNotification)
2251     */
2252    @Override
2253    public void processDispatchNotification(MessageDispatchNotification messageDispatchNotification) throws Exception {
2254        // do dispatch
2255        Subscription sub = getMatchingSubscription(messageDispatchNotification);
2256        if (sub != null) {
2257            MessageReference message = getMatchingMessage(messageDispatchNotification);
2258            sub.add(message);
2259            sub.processMessageDispatchNotification(messageDispatchNotification);
2260        }
2261    }
2262
2263    private QueueMessageReference getMatchingMessage(MessageDispatchNotification messageDispatchNotification)
2264            throws Exception {
2265        QueueMessageReference message = null;
2266        MessageId messageId = messageDispatchNotification.getMessageId();
2267
2268        pagedInPendingDispatchLock.writeLock().lock();
2269        try {
2270            for (MessageReference ref : dispatchPendingList) {
2271                if (messageId.equals(ref.getMessageId())) {
2272                    message = (QueueMessageReference)ref;
2273                    dispatchPendingList.remove(ref);
2274                    break;
2275                }
2276            }
2277        } finally {
2278            pagedInPendingDispatchLock.writeLock().unlock();
2279        }
2280
2281        if (message == null) {
2282            pagedInMessagesLock.readLock().lock();
2283            try {
2284                message = (QueueMessageReference)pagedInMessages.get(messageId);
2285            } finally {
2286                pagedInMessagesLock.readLock().unlock();
2287            }
2288        }
2289
2290        if (message == null) {
2291            messagesLock.writeLock().lock();
2292            try {
2293                try {
2294                    messages.setMaxBatchSize(getMaxPageSize());
2295                    messages.reset();
2296                    while (messages.hasNext()) {
2297                        MessageReference node = messages.next();
2298                        messages.remove();
2299                        if (messageId.equals(node.getMessageId())) {
2300                            message = this.createMessageReference(node.getMessage());
2301                            break;
2302                        }
2303                    }
2304                } finally {
2305                    messages.release();
2306                }
2307            } finally {
2308                messagesLock.writeLock().unlock();
2309            }
2310        }
2311
2312        if (message == null) {
2313            Message msg = loadMessage(messageId);
2314            if (msg != null) {
2315                message = this.createMessageReference(msg);
2316            }
2317        }
2318
2319        if (message == null) {
2320            throw new JMSException("Slave broker out of sync with master - Message: "
2321                    + messageDispatchNotification.getMessageId() + " on "
2322                    + messageDispatchNotification.getDestination() + " does not exist among pending("
2323                    + dispatchPendingList.size() + ") for subscription: "
2324                    + messageDispatchNotification.getConsumerId());
2325        }
2326        return message;
2327    }
2328
2329    /**
2330     * Find a consumer that matches the id in the message dispatch notification
2331     *
2332     * @param messageDispatchNotification
2333     * @return sub or null if the subscription has been removed before dispatch
2334     * @throws JMSException
2335     */
2336    private Subscription getMatchingSubscription(MessageDispatchNotification messageDispatchNotification)
2337            throws JMSException {
2338        Subscription sub = null;
2339        consumersLock.readLock().lock();
2340        try {
2341            for (Subscription s : consumers) {
2342                if (messageDispatchNotification.getConsumerId().equals(s.getConsumerInfo().getConsumerId())) {
2343                    sub = s;
2344                    break;
2345                }
2346            }
2347        } finally {
2348            consumersLock.readLock().unlock();
2349        }
2350        return sub;
2351    }
2352
2353    @Override
2354    public void onUsageChanged(@SuppressWarnings("rawtypes") Usage usage, int oldPercentUsage, int newPercentUsage) {
2355        if (oldPercentUsage > newPercentUsage) {
2356            asyncWakeup();
2357        }
2358    }
2359
2360    @Override
2361    protected Logger getLog() {
2362        return LOG;
2363    }
2364
2365    protected boolean isOptimizeStorage(){
2366        boolean result = false;
2367        if (isDoOptimzeMessageStorage()){
2368            consumersLock.readLock().lock();
2369            try{
2370                if (consumers.isEmpty()==false){
2371                    result = true;
2372                    for (Subscription s : consumers) {
2373                        if (s.getPrefetchSize()==0){
2374                            result = false;
2375                            break;
2376                        }
2377                        if (s.isSlowConsumer()){
2378                            result = false;
2379                            break;
2380                        }
2381                        if (s.getInFlightUsage() > getOptimizeMessageStoreInFlightLimit()){
2382                            result = false;
2383                            break;
2384                        }
2385                    }
2386                }
2387            } finally {
2388                consumersLock.readLock().unlock();
2389            }
2390        }
2391        return result;
2392    }
2393}