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() || ack.isExpiredAck()) {
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 (node.isExpired()) {
292                        if (broker.isExpired(node)) {
293                            Destination regionDestination = nodeDest;
294                            regionDestination.messageExpired(context, this, node);
295                        }
296                        iter.remove();
297                        nodeDest.getDestinationStatistics().getInflight().decrement();
298                    }
299                    if (ack.getLastMessageId().equals(node.getMessageId())) {
300                        if (usePrefetchExtension && getPrefetchSize() != 0) {
301                            // allow  batch to exceed prefetch
302                            while (true) {
303                                int currentExtension = prefetchExtension.get();
304                                int newExtension = Math.max(currentExtension, index + 1);
305                                if (prefetchExtension.compareAndSet(currentExtension, newExtension)) {
306                                    break;
307                                }
308                            }
309                        }
310                        destination = nodeDest;
311                        callDispatchMatched = true;
312                        break;
313                    }
314                }
315                if (!callDispatchMatched) {
316                    throw new JMSException(
317                            "Could not correlate acknowledgment with dispatched message: "
318                                    + ack);
319                }
320            } else if (ack.isRedeliveredAck()) {
321                // Message was re-delivered but it was not yet considered to be
322                // a DLQ message.
323                boolean inAckRange = false;
324                for (final MessageReference node : dispatched) {
325                    MessageId messageId = node.getMessageId();
326                    if (ack.getFirstMessageId() == null
327                            || ack.getFirstMessageId().equals(messageId)) {
328                        inAckRange = true;
329                    }
330                    if (inAckRange) {
331                        if (ack.getLastMessageId().equals(messageId)) {
332                            destination = (Destination) node.getRegionDestination();
333                            callDispatchMatched = true;
334                            break;
335                        }
336                    }
337                }
338                if (!callDispatchMatched) {
339                    throw new JMSException(
340                            "Could not correlate acknowledgment with dispatched message: "
341                                    + ack);
342                }
343            } else if (ack.isPoisonAck()) {
344                // TODO: what if the message is already in a DLQ???
345                // Handle the poison ACK case: we need to send the message to a
346                // DLQ
347                if (ack.isInTransaction()) {
348                    throw new JMSException("Poison ack cannot be transacted: "
349                            + ack);
350                }
351                int index = 0;
352                boolean inAckRange = false;
353                List<MessageReference> removeList = new ArrayList<MessageReference>();
354                for (final MessageReference node : dispatched) {
355                    MessageId messageId = node.getMessageId();
356                    if (ack.getFirstMessageId() == null
357                            || ack.getFirstMessageId().equals(messageId)) {
358                        inAckRange = true;
359                    }
360                    if (inAckRange) {
361                        sendToDLQ(context, node, ack.getPoisonCause());
362                        Destination nodeDest = (Destination) node.getRegionDestination();
363                        nodeDest.getDestinationStatistics()
364                        .getInflight().decrement();
365                        removeList.add(node);
366                        getSubscriptionStatistics().getDequeues().increment();
367                        index++;
368                        acknowledge(context, ack, node);
369                        if (ack.getLastMessageId().equals(messageId)) {
370                            while (true) {
371                                int currentExtension = prefetchExtension.get();
372                                int newExtension = Math.max(0, currentExtension - (index + 1));
373                                if (prefetchExtension.compareAndSet(currentExtension, newExtension)) {
374                                    break;
375                                }
376                            }
377                            destination = nodeDest;
378                            callDispatchMatched = true;
379                            break;
380                        }
381                    }
382                }
383                for (final MessageReference node : removeList) {
384                    dispatched.remove(node);
385                    getSubscriptionStatistics().getInflightMessageSize().addSize(-node.getSize());
386                }
387                if (!callDispatchMatched) {
388                    throw new JMSException(
389                            "Could not correlate acknowledgment with dispatched message: "
390                                    + ack);
391                }
392            }
393        }
394        if (callDispatchMatched && destination != null) {
395            destination.wakeup();
396            dispatchPending();
397
398            if (pending.isEmpty()) {
399                for (Destination dest : destinations) {
400                    dest.wakeup();
401                }
402            }
403        } else {
404            LOG.debug("Acknowledgment out of sync (Normally occurs when failover connection reconnects): {}", ack);
405        }
406    }
407
408    private void registerRemoveSync(ConnectionContext context, final MessageReference node) {
409        // setup a Synchronization to remove nodes from the
410        // dispatched list.
411        context.getTransaction().addSynchronization(
412                new Synchronization() {
413
414                    @Override
415                    public void beforeEnd() {
416                        if (usePrefetchExtension && getPrefetchSize() != 0) {
417                            while (true) {
418                                int currentExtension = prefetchExtension.get();
419                                int newExtension = Math.max(0, currentExtension - 1);
420                                if (prefetchExtension.compareAndSet(currentExtension, newExtension)) {
421                                    break;
422                                }
423                            }
424                        }
425                    }
426
427                    @Override
428                    public void afterCommit()
429                            throws Exception {
430                        Destination nodeDest = (Destination) node.getRegionDestination();
431                        synchronized(dispatchLock) {
432                            getSubscriptionStatistics().getDequeues().increment();
433                            dispatched.remove(node);
434                            getSubscriptionStatistics().getInflightMessageSize().addSize(-node.getSize());
435                            nodeDest.getDestinationStatistics().getInflight().decrement();
436                        }
437                        nodeDest.wakeup();
438                        dispatchPending();
439                    }
440
441                    @Override
442                    public void afterRollback() throws Exception {
443                        synchronized(dispatchLock) {
444                            // poisionAck will decrement - otherwise still inflight on client
445                        }
446                    }
447                });
448    }
449
450    /**
451     * Checks an ack versus the contents of the dispatched list.
452     *  called with dispatchLock held
453     * @param ack
454     * @throws JMSException if it does not match
455     */
456    protected void assertAckMatchesDispatched(MessageAck ack) throws JMSException {
457        MessageId firstAckedMsg = ack.getFirstMessageId();
458        MessageId lastAckedMsg = ack.getLastMessageId();
459        int checkCount = 0;
460        boolean checkFoundStart = false;
461        boolean checkFoundEnd = false;
462        for (MessageReference node : dispatched) {
463
464            if (firstAckedMsg == null) {
465                checkFoundStart = true;
466            } else if (!checkFoundStart && firstAckedMsg.equals(node.getMessageId())) {
467                checkFoundStart = true;
468            }
469
470            if (checkFoundStart) {
471                checkCount++;
472            }
473
474            if (lastAckedMsg != null && lastAckedMsg.equals(node.getMessageId())) {
475                checkFoundEnd = true;
476                break;
477            }
478        }
479        if (!checkFoundStart && firstAckedMsg != null)
480            throw new JMSException("Unmatched acknowledge: " + ack
481                    + "; Could not find Message-ID " + firstAckedMsg
482                    + " in dispatched-list (start of ack)");
483        if (!checkFoundEnd && lastAckedMsg != null)
484            throw new JMSException("Unmatched acknowledge: " + ack
485                    + "; Could not find Message-ID " + lastAckedMsg
486                    + " in dispatched-list (end of ack)");
487        if (ack.getMessageCount() != checkCount && !ack.isInTransaction()) {
488            throw new JMSException("Unmatched acknowledge: " + ack
489                    + "; Expected message count (" + ack.getMessageCount()
490                    + ") differs from count in dispatched-list (" + checkCount
491                    + ")");
492        }
493    }
494
495    /**
496     *
497     * @param context
498     * @param node
499     * @param poisonCause
500     * @throws IOException
501     * @throws Exception
502     */
503    protected void sendToDLQ(final ConnectionContext context, final MessageReference node, Throwable poisonCause) throws IOException, Exception {
504        broker.getRoot().sendToDeadLetterQueue(context, node, this, poisonCause);
505    }
506
507    @Override
508    public int getInFlightSize() {
509        return dispatched.size();
510    }
511
512    /**
513     * Used to determine if the broker can dispatch to the consumer.
514     *
515     * @return
516     */
517    @Override
518    public boolean isFull() {
519        return getPrefetchSize() == 0 ? prefetchExtension.get() == 0 : dispatched.size() - prefetchExtension.get() >= info.getPrefetchSize();
520    }
521
522    /**
523     * @return true when 60% or more room is left for dispatching messages
524     */
525    @Override
526    public boolean isLowWaterMark() {
527        return (dispatched.size() - prefetchExtension.get()) <= (info.getPrefetchSize() * .4);
528    }
529
530    /**
531     * @return true when 10% or less room is left for dispatching messages
532     */
533    @Override
534    public boolean isHighWaterMark() {
535        return (dispatched.size() - prefetchExtension.get()) >= (info.getPrefetchSize() * .9);
536    }
537
538    @Override
539    public int countBeforeFull() {
540        return getPrefetchSize() == 0 ? prefetchExtension.get() : info.getPrefetchSize() + prefetchExtension.get() - dispatched.size();
541    }
542
543    @Override
544    public int getPendingQueueSize() {
545        return pending.size();
546    }
547
548    @Override
549    public int getDispatchedQueueSize() {
550        return dispatched.size();
551    }
552
553    @Override
554    public long getDequeueCounter() {
555        return getSubscriptionStatistics().getDequeues().getCount();
556    }
557
558    @Override
559    public long getDispatchedCounter() {
560        return getSubscriptionStatistics().getDispatched().getCount();
561    }
562
563    @Override
564    public long getEnqueueCounter() {
565        return getSubscriptionStatistics().getEnqueues().getCount();
566    }
567
568    @Override
569    public boolean isRecoveryRequired() {
570        return pending.isRecoveryRequired();
571    }
572
573    public PendingMessageCursor getPending() {
574        return this.pending;
575    }
576
577    public void setPending(PendingMessageCursor pending) {
578        this.pending = pending;
579        if (this.pending!=null) {
580            this.pending.setSystemUsage(usageManager);
581            this.pending.setMemoryUsageHighWaterMark(getCursorMemoryHighWaterMark());
582        }
583    }
584
585    @Override
586    public void add(ConnectionContext context, Destination destination) throws Exception {
587        synchronized(pendingLock) {
588            super.add(context, destination);
589            pending.add(context, destination);
590        }
591    }
592
593    @Override
594    public List<MessageReference> remove(ConnectionContext context, Destination destination) throws Exception {
595        return remove(context, destination, dispatched);
596    }
597
598    public List<MessageReference> remove(ConnectionContext context, Destination destination, List<MessageReference> dispatched) throws Exception {
599        List<MessageReference> rc = new ArrayList<MessageReference>();
600        synchronized(pendingLock) {
601            super.remove(context, destination);
602            // Here is a potential problem concerning Inflight stat:
603            // Messages not already committed or rolled back may not be removed from dispatched list at the moment
604            // Except if each commit or rollback callback action comes before remove of subscriber.
605            rc.addAll(pending.remove(context, destination));
606
607            if (dispatched == null) {
608                return rc;
609            }
610
611            // Synchronized to DispatchLock if necessary
612            if (dispatched == this.dispatched) {
613                synchronized(dispatchLock) {
614                    updateDestinationStats(rc, destination, dispatched);
615                }
616            } else {
617                updateDestinationStats(rc, destination, dispatched);
618            }
619        }
620        return rc;
621    }
622
623    private void updateDestinationStats(List<MessageReference> rc, Destination destination, List<MessageReference> dispatched) {
624        ArrayList<MessageReference> references = new ArrayList<MessageReference>();
625        for (MessageReference r : dispatched) {
626            if (r.getRegionDestination() == destination) {
627                references.add(r);
628                getSubscriptionStatistics().getInflightMessageSize().addSize(-r.getSize());
629            }
630        }
631        rc.addAll(references);
632        destination.getDestinationStatistics().getInflight().subtract(references.size());
633        dispatched.removeAll(references);
634    }
635
636    // made public so it can be used in MQTTProtocolConverter
637    public void dispatchPending() throws IOException {
638        synchronized(pendingLock) {
639            try {
640                int numberToDispatch = countBeforeFull();
641                if (numberToDispatch > 0) {
642                    setSlowConsumer(false);
643                    setPendingBatchSize(pending, numberToDispatch);
644                    int count = 0;
645                    pending.reset();
646                    while (pending.hasNext() && !isFull() && count < numberToDispatch) {
647                        MessageReference node = pending.next();
648                        if (node == null) {
649                            break;
650                        }
651
652                        // Synchronize between dispatched list and remove of message from pending list
653                        // related to remove subscription action
654                        synchronized(dispatchLock) {
655                            pending.remove();
656                            if (!isDropped(node) && canDispatch(node)) {
657
658                                // Message may have been sitting in the pending
659                                // list a while waiting for the consumer to ak the message.
660                                if (node!=QueueMessageReference.NULL_MESSAGE && node.isExpired()) {
661                                    //increment number to dispatch
662                                    numberToDispatch++;
663                                    if (broker.isExpired(node)) {
664                                        ((Destination)node.getRegionDestination()).messageExpired(context, this, node);
665                                    }
666                                    continue;
667                                }
668                                dispatch(node);
669                                count++;
670                            }
671                        }
672                        // decrement after dispatch has taken ownership to avoid usage jitter
673                        node.decrementReferenceCount();
674                    }
675                } else if (!isSlowConsumer()) {
676                    setSlowConsumer(true);
677                    for (Destination dest :destinations) {
678                        dest.slowConsumer(context, this);
679                    }
680                }
681            } finally {
682                pending.release();
683            }
684        }
685    }
686
687    protected void setPendingBatchSize(PendingMessageCursor pending, int numberToDispatch) {
688        pending.setMaxBatchSize(numberToDispatch);
689    }
690
691    // called with dispatchLock held
692    protected boolean dispatch(final MessageReference node) throws IOException {
693        final Message message = node.getMessage();
694        if (message == null) {
695            return false;
696        }
697
698        okForAckAsDispatchDone.countDown();
699
700        MessageDispatch md = createMessageDispatch(node, message);
701        if (node != QueueMessageReference.NULL_MESSAGE) {
702            getSubscriptionStatistics().getDispatched().increment();
703            dispatched.add(node);
704            getSubscriptionStatistics().getInflightMessageSize().addSize(node.getSize());
705        }
706        if (getPrefetchSize() == 0) {
707            while (true) {
708                int currentExtension = prefetchExtension.get();
709                int newExtension = Math.max(0, currentExtension - 1);
710                if (prefetchExtension.compareAndSet(currentExtension, newExtension)) {
711                    break;
712                }
713            }
714        }
715        if (info.isDispatchAsync()) {
716            md.setTransmitCallback(new TransmitCallback() {
717
718                @Override
719                public void onSuccess() {
720                    // Since the message gets queued up in async dispatch, we don't want to
721                    // decrease the reference count until it gets put on the wire.
722                    onDispatch(node, message);
723                }
724
725                @Override
726                public void onFailure() {
727                    Destination nodeDest = (Destination) node.getRegionDestination();
728                    if (nodeDest != null) {
729                        if (node != QueueMessageReference.NULL_MESSAGE) {
730                            nodeDest.getDestinationStatistics().getDispatched().increment();
731                            nodeDest.getDestinationStatistics().getInflight().increment();
732                            LOG.trace("{} failed to dispatch: {} - {}, dispatched: {}, inflight: {}", new Object[]{ info.getConsumerId(), message.getMessageId(), message.getDestination(), getSubscriptionStatistics().getDispatched().getCount(), dispatched.size() });
733                        }
734                    }
735                    if (node instanceof QueueMessageReference) {
736                        ((QueueMessageReference) node).unlock();
737                    }
738                }
739            });
740            context.getConnection().dispatchAsync(md);
741        } else {
742            context.getConnection().dispatchSync(md);
743            onDispatch(node, message);
744        }
745        return true;
746    }
747
748    protected void onDispatch(final MessageReference node, final Message message) {
749        Destination nodeDest = (Destination) node.getRegionDestination();
750        if (nodeDest != null) {
751            if (node != QueueMessageReference.NULL_MESSAGE) {
752                nodeDest.getDestinationStatistics().getDispatched().increment();
753                nodeDest.getDestinationStatistics().getInflight().increment();
754                LOG.trace("{} dispatched: {} - {}, dispatched: {}, inflight: {}", new Object[]{ info.getConsumerId(), message.getMessageId(), message.getDestination(), getSubscriptionStatistics().getDispatched().getCount(), dispatched.size() });
755            }
756        }
757
758        if (info.isDispatchAsync()) {
759            try {
760                dispatchPending();
761            } catch (IOException e) {
762                context.getConnection().serviceExceptionAsync(e);
763            }
764        }
765    }
766
767    /**
768     * inform the MessageConsumer on the client to change it's prefetch
769     *
770     * @param newPrefetch
771     */
772    @Override
773    public void updateConsumerPrefetch(int newPrefetch) {
774        if (context != null && context.getConnection() != null && context.getConnection().isManageable()) {
775            ConsumerControl cc = new ConsumerControl();
776            cc.setConsumerId(info.getConsumerId());
777            cc.setPrefetch(newPrefetch);
778            context.getConnection().dispatchAsync(cc);
779        }
780    }
781
782    /**
783     * @param node
784     * @param message
785     * @return MessageDispatch
786     */
787    protected MessageDispatch createMessageDispatch(MessageReference node, Message message) {
788        MessageDispatch md = new MessageDispatch();
789        md.setConsumerId(info.getConsumerId());
790
791        if (node == QueueMessageReference.NULL_MESSAGE) {
792            md.setMessage(null);
793            md.setDestination(null);
794        } else {
795            Destination regionDestination = (Destination) node.getRegionDestination();
796            md.setDestination(regionDestination.getActiveMQDestination());
797            md.setMessage(message);
798            md.setRedeliveryCounter(node.getRedeliveryCounter());
799        }
800
801        return md;
802    }
803
804    /**
805     * Use when a matched message is about to be dispatched to the client.
806     *
807     * @param node
808     * @return false if the message should not be dispatched to the client
809     *         (another sub may have already dispatched it for example).
810     * @throws IOException
811     */
812    protected abstract boolean canDispatch(MessageReference node) throws IOException;
813
814    protected abstract boolean isDropped(MessageReference node);
815
816    /**
817     * Used during acknowledgment to remove the message.
818     *
819     * @throws IOException
820     */
821    protected abstract void acknowledge(ConnectionContext context, final MessageAck ack, final MessageReference node) throws IOException;
822
823
824    public int getMaxProducersToAudit() {
825        return maxProducersToAudit;
826    }
827
828    public void setMaxProducersToAudit(int maxProducersToAudit) {
829        this.maxProducersToAudit = maxProducersToAudit;
830        if (this.pending != null) {
831            this.pending.setMaxProducersToAudit(maxProducersToAudit);
832        }
833    }
834
835    public int getMaxAuditDepth() {
836        return maxAuditDepth;
837    }
838
839    public void setMaxAuditDepth(int maxAuditDepth) {
840        this.maxAuditDepth = maxAuditDepth;
841        if (this.pending != null) {
842            this.pending.setMaxAuditDepth(maxAuditDepth);
843        }
844    }
845
846    public boolean isUsePrefetchExtension() {
847        return usePrefetchExtension;
848    }
849
850    public void setUsePrefetchExtension(boolean usePrefetchExtension) {
851        this.usePrefetchExtension = usePrefetchExtension;
852    }
853
854    protected int getPrefetchExtension() {
855        return this.prefetchExtension.get();
856    }
857
858    @Override
859    public void setPrefetchSize(int prefetchSize) {
860        this.info.setPrefetchSize(prefetchSize);
861        try {
862            this.dispatchPending();
863        } catch (Exception e) {
864            LOG.trace("Caught exception during dispatch after prefetch change.", e);
865        }
866    }
867}