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 java.io.IOException;
020import java.util.ArrayList;
021import java.util.Iterator;
022import java.util.List;
023import java.util.concurrent.CountDownLatch;
024import java.util.concurrent.TimeUnit;
025import java.util.concurrent.atomic.AtomicInteger;
026
027import javax.jms.JMSException;
028
029import org.apache.activemq.broker.Broker;
030import org.apache.activemq.broker.ConnectionContext;
031import org.apache.activemq.broker.region.cursors.PendingMessageCursor;
032import org.apache.activemq.broker.region.cursors.VMPendingMessageCursor;
033import org.apache.activemq.command.ConsumerControl;
034import org.apache.activemq.command.ConsumerInfo;
035import org.apache.activemq.command.Message;
036import org.apache.activemq.command.MessageAck;
037import org.apache.activemq.command.MessageDispatch;
038import org.apache.activemq.command.MessageDispatchNotification;
039import org.apache.activemq.command.MessageId;
040import org.apache.activemq.command.MessagePull;
041import org.apache.activemq.command.Response;
042import org.apache.activemq.thread.Scheduler;
043import org.apache.activemq.transaction.Synchronization;
044import org.apache.activemq.transport.TransmitCallback;
045import org.apache.activemq.usage.SystemUsage;
046import org.slf4j.Logger;
047import org.slf4j.LoggerFactory;
048
049/**
050 * A subscription that honors the pre-fetch option of the ConsumerInfo.
051 */
052public abstract class PrefetchSubscription extends AbstractSubscription {
053
054    private static final Logger LOG = LoggerFactory.getLogger(PrefetchSubscription.class);
055    protected final Scheduler scheduler;
056
057    protected PendingMessageCursor pending;
058    protected final List<MessageReference> dispatched = new ArrayList<MessageReference>();
059    protected final AtomicInteger prefetchExtension = new AtomicInteger();
060    protected boolean usePrefetchExtension = true;
061    private int maxProducersToAudit=32;
062    private int maxAuditDepth=2048;
063    protected final SystemUsage usageManager;
064    protected final Object pendingLock = new Object();
065    protected final Object dispatchLock = new Object();
066    private final CountDownLatch okForAckAsDispatchDone = new CountDownLatch(1);
067
068    public PrefetchSubscription(Broker broker, SystemUsage usageManager, ConnectionContext context, ConsumerInfo info, PendingMessageCursor cursor) throws JMSException {
069        super(broker,context, info);
070        this.usageManager=usageManager;
071        pending = cursor;
072        try {
073            pending.start();
074        } catch (Exception e) {
075            throw new JMSException(e.getMessage());
076        }
077        this.scheduler = broker.getScheduler();
078    }
079
080    public PrefetchSubscription(Broker broker,SystemUsage usageManager, ConnectionContext context, ConsumerInfo info) throws JMSException {
081        this(broker,usageManager,context, info, new VMPendingMessageCursor(false));
082    }
083
084    /**
085     * Allows a message to be pulled on demand by a client
086     */
087    @Override
088    public Response pullMessage(ConnectionContext context, final MessagePull pull) throws Exception {
089        // The slave should not deliver pull messages.
090        // TODO: when the slave becomes a master, He should send a NULL message to all the
091        // consumers to 'wake them up' in case they were waiting for a message.
092        if (getPrefetchSize() == 0) {
093            prefetchExtension.set(pull.getQuantity());
094            final long dispatchCounterBeforePull = getSubscriptionStatistics().getDispatched().getCount();
095
096            // Have the destination push us some messages.
097            for (Destination dest : destinations) {
098                dest.iterate();
099            }
100            dispatchPending();
101
102            synchronized(this) {
103                // If there was nothing dispatched.. we may need to setup a timeout.
104                if (dispatchCounterBeforePull == getSubscriptionStatistics().getDispatched().getCount() || pull.isAlwaysSignalDone()) {
105                    // immediate timeout used by receiveNoWait()
106                    if (pull.getTimeout() == -1) {
107                        // Null message indicates the pull is done or did not have pending.
108                        prefetchExtension.set(1);
109                        add(QueueMessageReference.NULL_MESSAGE);
110                        dispatchPending();
111                    }
112                    if (pull.getTimeout() > 0) {
113                        scheduler.executeAfterDelay(new Runnable() {
114                            @Override
115                            public void run() {
116                                pullTimeout(dispatchCounterBeforePull, pull.isAlwaysSignalDone());
117                            }
118                        }, pull.getTimeout());
119                    }
120                }
121            }
122        }
123        return null;
124    }
125
126    /**
127     * Occurs when a pull times out. If nothing has been dispatched since the
128     * timeout was setup, then send the NULL message.
129     */
130    final void pullTimeout(long dispatchCounterBeforePull, boolean alwaysSignalDone) {
131        synchronized (pendingLock) {
132            if (dispatchCounterBeforePull == getSubscriptionStatistics().getDispatched().getCount() || alwaysSignalDone) {
133                try {
134                    prefetchExtension.set(1);
135                    add(QueueMessageReference.NULL_MESSAGE);
136                    dispatchPending();
137                } catch (Exception e) {
138                    context.getConnection().serviceException(e);
139                } finally {
140                    prefetchExtension.set(0);
141                }
142            }
143        }
144    }
145
146    @Override
147    public void add(MessageReference node) throws Exception {
148        synchronized (pendingLock) {
149            // The destination may have just been removed...
150            if (!destinations.contains(node.getRegionDestination()) && node != QueueMessageReference.NULL_MESSAGE) {
151                // perhaps we should inform the caller that we are no longer valid to dispatch to?
152                return;
153            }
154
155            // Don't increment for the pullTimeout control message.
156            if (!node.equals(QueueMessageReference.NULL_MESSAGE)) {
157                getSubscriptionStatistics().getEnqueues().increment();
158            }
159            pending.addMessageLast(node);
160        }
161        dispatchPending();
162    }
163
164    @Override
165    public void processMessageDispatchNotification(MessageDispatchNotification mdn) throws Exception {
166        synchronized(pendingLock) {
167            try {
168                pending.reset();
169                while (pending.hasNext()) {
170                    MessageReference node = pending.next();
171                    node.decrementReferenceCount();
172                    if (node.getMessageId().equals(mdn.getMessageId())) {
173                        // Synchronize between dispatched list and removal of messages from pending list
174                        // related to remove subscription action
175                        synchronized(dispatchLock) {
176                            pending.remove();
177                            createMessageDispatch(node, node.getMessage());
178                            dispatched.add(node);
179                            getSubscriptionStatistics().getInflightMessageSize().addSize(node.getSize());
180                            onDispatch(node, node.getMessage());
181                        }
182                        return;
183                    }
184                }
185            } finally {
186                pending.release();
187            }
188        }
189        throw new JMSException(
190                "Slave broker out of sync with master: Dispatched message ("
191                        + mdn.getMessageId() + ") was not in the pending list for "
192                        + mdn.getConsumerId() + " on " + mdn.getDestination().getPhysicalName());
193    }
194
195    @Override
196    public final void acknowledge(final ConnectionContext context,final MessageAck ack) throws Exception {
197        // Handle the standard acknowledgment case.
198        boolean callDispatchMatched = false;
199        Destination destination = null;
200
201        if (!okForAckAsDispatchDone.await(0l, TimeUnit.MILLISECONDS)) {
202            // suppress unexpected ack exception in this expected case
203            LOG.warn("Ignoring ack received before dispatch; result of failover with an outstanding ack. Acked messages will be replayed if present on this broker. Ignored ack: {}", ack);
204            return;
205        }
206
207        LOG.trace("ack: {}", ack);
208
209        synchronized(dispatchLock) {
210            if (ack.isStandardAck()) {
211                // First check if the ack matches the dispatched. When using failover this might
212                // not be the case. We don't ever want to ack the wrong messages.
213                assertAckMatchesDispatched(ack);
214
215                // Acknowledge all dispatched messages up till the message id of
216                // the acknowledgment.
217                boolean inAckRange = false;
218                List<MessageReference> removeList = new ArrayList<MessageReference>();
219                for (final MessageReference node : dispatched) {
220                    MessageId messageId = node.getMessageId();
221                    if (ack.getFirstMessageId() == null
222                            || ack.getFirstMessageId().equals(messageId)) {
223                        inAckRange = true;
224                    }
225                    if (inAckRange) {
226                        // Don't remove the nodes until we are committed.
227                        if (!context.isInTransaction()) {
228                            getSubscriptionStatistics().getDequeues().increment();
229                            ((Destination)node.getRegionDestination()).getDestinationStatistics().getInflight().decrement();
230                            removeList.add(node);
231                        } else {
232                            registerRemoveSync(context, node);
233                        }
234                        acknowledge(context, ack, node);
235                        if (ack.getLastMessageId().equals(messageId)) {
236                            destination = (Destination) node.getRegionDestination();
237                            callDispatchMatched = true;
238                            break;
239                        }
240                    }
241                }
242                for (final MessageReference node : removeList) {
243                    dispatched.remove(node);
244                    getSubscriptionStatistics().getInflightMessageSize().addSize(-node.getSize());
245                }
246                // this only happens after a reconnect - get an ack which is not
247                // valid
248                if (!callDispatchMatched) {
249                    LOG.warn("Could not correlate acknowledgment with dispatched message: {}", ack);
250                }
251            } else if (ack.isIndividualAck()) {
252                // Message was delivered and acknowledge - but only delete the
253                // individual message
254                for (final MessageReference node : dispatched) {
255                    MessageId messageId = node.getMessageId();
256                    if (ack.getLastMessageId().equals(messageId)) {
257                        // Don't remove the nodes until we are committed - immediateAck option
258                        if (!context.isInTransaction()) {
259                            getSubscriptionStatistics().getDequeues().increment();
260                            ((Destination)node.getRegionDestination()).getDestinationStatistics().getInflight().decrement();
261                            dispatched.remove(node);
262                            getSubscriptionStatistics().getInflightMessageSize().addSize(-node.getSize());
263                        } else {
264                            registerRemoveSync(context, node);
265                        }
266
267                        if (usePrefetchExtension && getPrefetchSize() != 0 && ack.isInTransaction()) {
268                            // allow transaction batch to exceed prefetch
269                            while (true) {
270                                int currentExtension = prefetchExtension.get();
271                                int newExtension = Math.max(currentExtension, currentExtension + 1);
272                                if (prefetchExtension.compareAndSet(currentExtension, newExtension)) {
273                                    break;
274                                }
275                            }
276                        }
277
278                        acknowledge(context, ack, node);
279                        destination = (Destination) node.getRegionDestination();
280                        callDispatchMatched = true;
281                        break;
282                    }
283                }
284            }else if (ack.isDeliveredAck()) {
285                // Message was delivered but not acknowledged: update pre-fetch
286                // counters.
287                int index = 0;
288                for (Iterator<MessageReference> iter = dispatched.iterator(); iter.hasNext(); index++) {
289                    final MessageReference node = iter.next();
290                    Destination nodeDest = (Destination) node.getRegionDestination();
291                    if (ack.getLastMessageId().equals(node.getMessageId())) {
292                        if (usePrefetchExtension && getPrefetchSize() != 0) {
293                            // allow  batch to exceed prefetch
294                            while (true) {
295                                int currentExtension = prefetchExtension.get();
296                                int newExtension = Math.max(currentExtension, index + 1);
297                                if (prefetchExtension.compareAndSet(currentExtension, newExtension)) {
298                                    break;
299                                }
300                            }
301                        }
302                        destination = nodeDest;
303                        callDispatchMatched = true;
304                        break;
305                    }
306                }
307                if (!callDispatchMatched) {
308                    throw new JMSException(
309                            "Could not correlate acknowledgment with dispatched message: "
310                                    + ack);
311                }
312            } else if (ack.isExpiredAck()) {
313                // Message was expired
314                int index = 0;
315                boolean inAckRange = false;
316                for (Iterator<MessageReference> iter = dispatched.iterator(); iter.hasNext(); index++) {
317                    final MessageReference node = iter.next();
318                    Destination nodeDest = (Destination) node.getRegionDestination();
319                    MessageId messageId = node.getMessageId();
320                    if (ack.getFirstMessageId() == null
321                            || ack.getFirstMessageId().equals(messageId)) {
322                        inAckRange = true;
323                    }
324                    if (inAckRange) {
325                        if (node.isExpired()) {
326                            if (broker.isExpired(node)) {
327                                Destination regionDestination = nodeDest;
328                                regionDestination.messageExpired(context, this, node);
329                            }
330                            iter.remove();
331                            nodeDest.getDestinationStatistics().getInflight().decrement();
332                        }
333                        if (ack.getLastMessageId().equals(messageId)) {
334                            if (usePrefetchExtension && getPrefetchSize() != 0) {
335                                // allow  batch to exceed prefetch
336                                while (true) {
337                                    int currentExtension = prefetchExtension.get();
338                                    int newExtension = Math.max(currentExtension, index + 1);
339                                    if (prefetchExtension.compareAndSet(currentExtension, newExtension)) {
340                                        break;
341                                    }
342                                }
343                            }
344
345                            destination = (Destination) node.getRegionDestination();
346                            callDispatchMatched = true;
347                            break;
348                        }
349                    }
350                }
351                if (!callDispatchMatched) {
352                    throw new JMSException(
353                            "Could not correlate expiration acknowledgment with dispatched message: "
354                                    + ack);
355                }
356            } else if (ack.isRedeliveredAck()) {
357                // Message was re-delivered but it was not yet considered to be
358                // a DLQ message.
359                boolean inAckRange = false;
360                for (final MessageReference node : dispatched) {
361                    MessageId messageId = node.getMessageId();
362                    if (ack.getFirstMessageId() == null
363                            || ack.getFirstMessageId().equals(messageId)) {
364                        inAckRange = true;
365                    }
366                    if (inAckRange) {
367                        if (ack.getLastMessageId().equals(messageId)) {
368                            destination = (Destination) node.getRegionDestination();
369                            callDispatchMatched = true;
370                            break;
371                        }
372                    }
373                }
374                if (!callDispatchMatched) {
375                    throw new JMSException(
376                            "Could not correlate acknowledgment with dispatched message: "
377                                    + ack);
378                }
379            } else if (ack.isPoisonAck()) {
380                // TODO: what if the message is already in a DLQ???
381                // Handle the poison ACK case: we need to send the message to a
382                // DLQ
383                if (ack.isInTransaction()) {
384                    throw new JMSException("Poison ack cannot be transacted: "
385                            + ack);
386                }
387                int index = 0;
388                boolean inAckRange = false;
389                List<MessageReference> removeList = new ArrayList<MessageReference>();
390                for (final MessageReference node : dispatched) {
391                    MessageId messageId = node.getMessageId();
392                    if (ack.getFirstMessageId() == null
393                            || ack.getFirstMessageId().equals(messageId)) {
394                        inAckRange = true;
395                    }
396                    if (inAckRange) {
397                        sendToDLQ(context, node, ack.getPoisonCause());
398                        Destination nodeDest = (Destination) node.getRegionDestination();
399                        nodeDest.getDestinationStatistics()
400                        .getInflight().decrement();
401                        removeList.add(node);
402                        getSubscriptionStatistics().getDequeues().increment();
403                        index++;
404                        acknowledge(context, ack, node);
405                        if (ack.getLastMessageId().equals(messageId)) {
406                            while (true) {
407                                int currentExtension = prefetchExtension.get();
408                                int newExtension = Math.max(0, currentExtension - (index + 1));
409                                if (prefetchExtension.compareAndSet(currentExtension, newExtension)) {
410                                    break;
411                                }
412                            }
413                            destination = nodeDest;
414                            callDispatchMatched = true;
415                            break;
416                        }
417                    }
418                }
419                for (final MessageReference node : removeList) {
420                    dispatched.remove(node);
421                    getSubscriptionStatistics().getInflightMessageSize().addSize(-node.getSize());
422                }
423                if (!callDispatchMatched) {
424                    throw new JMSException(
425                            "Could not correlate acknowledgment with dispatched message: "
426                                    + ack);
427                }
428            }
429        }
430        if (callDispatchMatched && destination != null) {
431            destination.wakeup();
432            dispatchPending();
433
434            if (pending.isEmpty()) {
435                for (Destination dest : destinations) {
436                    dest.wakeup();
437                }
438            }
439        } else {
440            LOG.debug("Acknowledgment out of sync (Normally occurs when failover connection reconnects): {}", ack);
441        }
442    }
443
444    private void registerRemoveSync(ConnectionContext context, final MessageReference node) {
445        // setup a Synchronization to remove nodes from the
446        // dispatched list.
447        context.getTransaction().addSynchronization(
448                new Synchronization() {
449
450                    @Override
451                    public void beforeEnd() {
452                        if (usePrefetchExtension && getPrefetchSize() != 0) {
453                            while (true) {
454                                int currentExtension = prefetchExtension.get();
455                                int newExtension = Math.max(0, currentExtension - 1);
456                                if (prefetchExtension.compareAndSet(currentExtension, newExtension)) {
457                                    break;
458                                }
459                            }
460                        }
461                    }
462
463                    @Override
464                    public void afterCommit()
465                            throws Exception {
466                        Destination nodeDest = (Destination) node.getRegionDestination();
467                        synchronized(dispatchLock) {
468                            getSubscriptionStatistics().getDequeues().increment();
469                            dispatched.remove(node);
470                            getSubscriptionStatistics().getInflightMessageSize().addSize(-node.getSize());
471                            nodeDest.getDestinationStatistics().getInflight().decrement();
472                        }
473                        nodeDest.wakeup();
474                        dispatchPending();
475                    }
476
477                    @Override
478                    public void afterRollback() throws Exception {
479                        synchronized(dispatchLock) {
480                            // poisionAck will decrement - otherwise still inflight on client
481                        }
482                    }
483                });
484    }
485
486    /**
487     * Checks an ack versus the contents of the dispatched list.
488     *  called with dispatchLock held
489     * @param ack
490     * @throws JMSException if it does not match
491     */
492    protected void assertAckMatchesDispatched(MessageAck ack) throws JMSException {
493        MessageId firstAckedMsg = ack.getFirstMessageId();
494        MessageId lastAckedMsg = ack.getLastMessageId();
495        int checkCount = 0;
496        boolean checkFoundStart = false;
497        boolean checkFoundEnd = false;
498        for (MessageReference node : dispatched) {
499
500            if (firstAckedMsg == null) {
501                checkFoundStart = true;
502            } else if (!checkFoundStart && firstAckedMsg.equals(node.getMessageId())) {
503                checkFoundStart = true;
504            }
505
506            if (checkFoundStart) {
507                checkCount++;
508            }
509
510            if (lastAckedMsg != null && lastAckedMsg.equals(node.getMessageId())) {
511                checkFoundEnd = true;
512                break;
513            }
514        }
515        if (!checkFoundStart && firstAckedMsg != null)
516            throw new JMSException("Unmatched acknowledge: " + ack
517                    + "; Could not find Message-ID " + firstAckedMsg
518                    + " in dispatched-list (start of ack)");
519        if (!checkFoundEnd && lastAckedMsg != null)
520            throw new JMSException("Unmatched acknowledge: " + ack
521                    + "; Could not find Message-ID " + lastAckedMsg
522                    + " in dispatched-list (end of ack)");
523        if (ack.getMessageCount() != checkCount && !ack.isInTransaction()) {
524            throw new JMSException("Unmatched acknowledge: " + ack
525                    + "; Expected message count (" + ack.getMessageCount()
526                    + ") differs from count in dispatched-list (" + checkCount
527                    + ")");
528        }
529    }
530
531    /**
532     *
533     * @param context
534     * @param node
535     * @param poisonCause
536     * @throws IOException
537     * @throws Exception
538     */
539    protected void sendToDLQ(final ConnectionContext context, final MessageReference node, Throwable poisonCause) throws IOException, Exception {
540        broker.getRoot().sendToDeadLetterQueue(context, node, this, poisonCause);
541    }
542
543    @Override
544    public int getInFlightSize() {
545        return dispatched.size();
546    }
547
548    /**
549     * Used to determine if the broker can dispatch to the consumer.
550     *
551     * @return true if the subscription is full
552     */
553    @Override
554    public boolean isFull() {
555        return getPrefetchSize() == 0 ? prefetchExtension.get() == 0 : dispatched.size() - prefetchExtension.get() >= info.getPrefetchSize();
556    }
557
558    /**
559     * @return true when 60% or more room is left for dispatching messages
560     */
561    @Override
562    public boolean isLowWaterMark() {
563        return (dispatched.size() - prefetchExtension.get()) <= (info.getPrefetchSize() * .4);
564    }
565
566    /**
567     * @return true when 10% or less room is left for dispatching messages
568     */
569    @Override
570    public boolean isHighWaterMark() {
571        return (dispatched.size() - prefetchExtension.get()) >= (info.getPrefetchSize() * .9);
572    }
573
574    @Override
575    public int countBeforeFull() {
576        return getPrefetchSize() == 0 ? prefetchExtension.get() : info.getPrefetchSize() + prefetchExtension.get() - dispatched.size();
577    }
578
579    @Override
580    public int getPendingQueueSize() {
581        return pending.size();
582    }
583
584    @Override
585    public int getDispatchedQueueSize() {
586        return dispatched.size();
587    }
588
589    @Override
590    public long getDequeueCounter() {
591        return getSubscriptionStatistics().getDequeues().getCount();
592    }
593
594    @Override
595    public long getDispatchedCounter() {
596        return getSubscriptionStatistics().getDispatched().getCount();
597    }
598
599    @Override
600    public long getEnqueueCounter() {
601        return getSubscriptionStatistics().getEnqueues().getCount();
602    }
603
604    @Override
605    public boolean isRecoveryRequired() {
606        return pending.isRecoveryRequired();
607    }
608
609    public PendingMessageCursor getPending() {
610        return this.pending;
611    }
612
613    public void setPending(PendingMessageCursor pending) {
614        this.pending = pending;
615        if (this.pending!=null) {
616            this.pending.setSystemUsage(usageManager);
617            this.pending.setMemoryUsageHighWaterMark(getCursorMemoryHighWaterMark());
618        }
619    }
620
621    @Override
622    public void add(ConnectionContext context, Destination destination) throws Exception {
623        synchronized(pendingLock) {
624            super.add(context, destination);
625            pending.add(context, destination);
626        }
627    }
628
629    @Override
630    public List<MessageReference> remove(ConnectionContext context, Destination destination) throws Exception {
631        return remove(context, destination, dispatched);
632    }
633
634    public List<MessageReference> remove(ConnectionContext context, Destination destination, List<MessageReference> dispatched) throws Exception {
635        List<MessageReference> rc = new ArrayList<MessageReference>();
636        synchronized(pendingLock) {
637            super.remove(context, destination);
638            // Here is a potential problem concerning Inflight stat:
639            // Messages not already committed or rolled back may not be removed from dispatched list at the moment
640            // Except if each commit or rollback callback action comes before remove of subscriber.
641            rc.addAll(pending.remove(context, destination));
642
643            if (dispatched == null) {
644                return rc;
645            }
646
647            // Synchronized to DispatchLock if necessary
648            if (dispatched == this.dispatched) {
649                synchronized(dispatchLock) {
650                    updateDestinationStats(rc, destination, dispatched);
651                }
652            } else {
653                updateDestinationStats(rc, destination, dispatched);
654            }
655        }
656        return rc;
657    }
658
659    private void updateDestinationStats(List<MessageReference> rc, Destination destination, List<MessageReference> dispatched) {
660        ArrayList<MessageReference> references = new ArrayList<MessageReference>();
661        for (MessageReference r : dispatched) {
662            if (r.getRegionDestination() == destination) {
663                references.add(r);
664                getSubscriptionStatistics().getInflightMessageSize().addSize(-r.getSize());
665            }
666        }
667        rc.addAll(references);
668        destination.getDestinationStatistics().getInflight().subtract(references.size());
669        dispatched.removeAll(references);
670    }
671
672    // made public so it can be used in MQTTProtocolConverter
673    public void dispatchPending() throws IOException {
674        synchronized(pendingLock) {
675            try {
676                int numberToDispatch = countBeforeFull();
677                if (numberToDispatch > 0) {
678                    setSlowConsumer(false);
679                    setPendingBatchSize(pending, numberToDispatch);
680                    int count = 0;
681                    pending.reset();
682                    while (pending.hasNext() && !isFull() && count < numberToDispatch) {
683                        MessageReference node = pending.next();
684                        if (node == null) {
685                            break;
686                        }
687
688                        // Synchronize between dispatched list and remove of message from pending list
689                        // related to remove subscription action
690                        synchronized(dispatchLock) {
691                            pending.remove();
692                            if (!isDropped(node) && canDispatch(node)) {
693
694                                // Message may have been sitting in the pending
695                                // list a while waiting for the consumer to ak the message.
696                                if (node != QueueMessageReference.NULL_MESSAGE && node.isExpired()) {
697                                    //increment number to dispatch
698                                    numberToDispatch++;
699                                    if (broker.isExpired(node)) {
700                                        ((Destination)node.getRegionDestination()).messageExpired(context, this, node);
701                                    }
702
703                                    if (!isBrowser()) {
704                                        node.decrementReferenceCount();
705                                        continue;
706                                    }
707                                }
708                                dispatch(node);
709                                count++;
710                            }
711                        }
712                        // decrement after dispatch has taken ownership to avoid usage jitter
713                        node.decrementReferenceCount();
714                    }
715                } else if (!isSlowConsumer()) {
716                    setSlowConsumer(true);
717                    for (Destination dest :destinations) {
718                        dest.slowConsumer(context, this);
719                    }
720                }
721            } finally {
722                pending.release();
723            }
724        }
725    }
726
727    protected void setPendingBatchSize(PendingMessageCursor pending, int numberToDispatch) {
728        pending.setMaxBatchSize(numberToDispatch);
729    }
730
731    // called with dispatchLock held
732    protected boolean dispatch(final MessageReference node) throws IOException {
733        final Message message = node.getMessage();
734        if (message == null) {
735            return false;
736        }
737
738        okForAckAsDispatchDone.countDown();
739
740        MessageDispatch md = createMessageDispatch(node, message);
741        if (node != QueueMessageReference.NULL_MESSAGE) {
742            getSubscriptionStatistics().getDispatched().increment();
743            dispatched.add(node);
744            getSubscriptionStatistics().getInflightMessageSize().addSize(node.getSize());
745        }
746        if (getPrefetchSize() == 0) {
747            while (true) {
748                int currentExtension = prefetchExtension.get();
749                int newExtension = Math.max(0, currentExtension - 1);
750                if (prefetchExtension.compareAndSet(currentExtension, newExtension)) {
751                    break;
752                }
753            }
754        }
755        if (info.isDispatchAsync()) {
756            md.setTransmitCallback(new TransmitCallback() {
757
758                @Override
759                public void onSuccess() {
760                    // Since the message gets queued up in async dispatch, we don't want to
761                    // decrease the reference count until it gets put on the wire.
762                    onDispatch(node, message);
763                }
764
765                @Override
766                public void onFailure() {
767                    Destination nodeDest = (Destination) node.getRegionDestination();
768                    if (nodeDest != null) {
769                        if (node != QueueMessageReference.NULL_MESSAGE) {
770                            nodeDest.getDestinationStatistics().getDispatched().increment();
771                            nodeDest.getDestinationStatistics().getInflight().increment();
772                            LOG.trace("{} failed to dispatch: {} - {}, dispatched: {}, inflight: {}", new Object[]{ info.getConsumerId(), message.getMessageId(), message.getDestination(), getSubscriptionStatistics().getDispatched().getCount(), dispatched.size() });
773                        }
774                    }
775                    if (node instanceof QueueMessageReference) {
776                        ((QueueMessageReference) node).unlock();
777                    }
778                }
779            });
780            context.getConnection().dispatchAsync(md);
781        } else {
782            context.getConnection().dispatchSync(md);
783            onDispatch(node, message);
784        }
785        return true;
786    }
787
788    protected void onDispatch(final MessageReference node, final Message message) {
789        Destination nodeDest = (Destination) node.getRegionDestination();
790        if (nodeDest != null) {
791            if (node != QueueMessageReference.NULL_MESSAGE) {
792                nodeDest.getDestinationStatistics().getDispatched().increment();
793                nodeDest.getDestinationStatistics().getInflight().increment();
794                LOG.trace("{} dispatched: {} - {}, dispatched: {}, inflight: {}", new Object[]{ info.getConsumerId(), message.getMessageId(), message.getDestination(), getSubscriptionStatistics().getDispatched().getCount(), dispatched.size() });
795            }
796        }
797
798        if (info.isDispatchAsync()) {
799            try {
800                dispatchPending();
801            } catch (IOException e) {
802                context.getConnection().serviceExceptionAsync(e);
803            }
804        }
805    }
806
807    /**
808     * inform the MessageConsumer on the client to change it's prefetch
809     *
810     * @param newPrefetch
811     */
812    @Override
813    public void updateConsumerPrefetch(int newPrefetch) {
814        if (context != null && context.getConnection() != null && context.getConnection().isManageable()) {
815            ConsumerControl cc = new ConsumerControl();
816            cc.setConsumerId(info.getConsumerId());
817            cc.setPrefetch(newPrefetch);
818            context.getConnection().dispatchAsync(cc);
819        }
820    }
821
822    /**
823     * @param node
824     * @param message
825     * @return MessageDispatch
826     */
827    protected MessageDispatch createMessageDispatch(MessageReference node, Message message) {
828        MessageDispatch md = new MessageDispatch();
829        md.setConsumerId(info.getConsumerId());
830
831        if (node == QueueMessageReference.NULL_MESSAGE) {
832            md.setMessage(null);
833            md.setDestination(null);
834        } else {
835            Destination regionDestination = (Destination) node.getRegionDestination();
836            md.setDestination(regionDestination.getActiveMQDestination());
837            md.setMessage(message);
838            md.setRedeliveryCounter(node.getRedeliveryCounter());
839        }
840
841        return md;
842    }
843
844    /**
845     * Use when a matched message is about to be dispatched to the client.
846     *
847     * @param node
848     * @return false if the message should not be dispatched to the client
849     *         (another sub may have already dispatched it for example).
850     * @throws IOException
851     */
852    protected abstract boolean canDispatch(MessageReference node) throws IOException;
853
854    protected abstract boolean isDropped(MessageReference node);
855
856    /**
857     * Used during acknowledgment to remove the message.
858     *
859     * @throws IOException
860     */
861    protected abstract void acknowledge(ConnectionContext context, final MessageAck ack, final MessageReference node) throws IOException;
862
863
864    public int getMaxProducersToAudit() {
865        return maxProducersToAudit;
866    }
867
868    public void setMaxProducersToAudit(int maxProducersToAudit) {
869        this.maxProducersToAudit = maxProducersToAudit;
870        if (this.pending != null) {
871            this.pending.setMaxProducersToAudit(maxProducersToAudit);
872        }
873    }
874
875    public int getMaxAuditDepth() {
876        return maxAuditDepth;
877    }
878
879    public void setMaxAuditDepth(int maxAuditDepth) {
880        this.maxAuditDepth = maxAuditDepth;
881        if (this.pending != null) {
882            this.pending.setMaxAuditDepth(maxAuditDepth);
883        }
884    }
885
886    public boolean isUsePrefetchExtension() {
887        return usePrefetchExtension;
888    }
889
890    public void setUsePrefetchExtension(boolean usePrefetchExtension) {
891        this.usePrefetchExtension = usePrefetchExtension;
892    }
893
894    protected int getPrefetchExtension() {
895        return this.prefetchExtension.get();
896    }
897
898    @Override
899    public void setPrefetchSize(int prefetchSize) {
900        this.info.setPrefetchSize(prefetchSize);
901        try {
902            this.dispatchPending();
903        } catch (Exception e) {
904            LOG.trace("Caught exception during dispatch after prefetch change.", e);
905        }
906    }
907}