if (trace) { log.trace(this + " incrementing delivery count for " + del); }
   protected void deliverInternal() throws Throwable   {      try      {         // The iterator is used to iterate through the refs in the channel in the case that they         // don't match the selectors of any receivers.         ListIterator iter = null;                  MessageReference ref = null;         while (true)         {                       synchronized (refLock)            {               ref = nextReference(iter);                           }            if (ref != null)            {               // Check if message is expired (we also do this on the clientside) If so ack it from the channel               if (ref.isExpired())               {                  expireRef(ref, iter);               }               else               {                  // Reference is not expired                  // Attempt to push the ref to a receiver                                    if (trace) { log.trace(this + " pushing " + ref); }                                                    Delivery del = router.handle(this, ref, null);                  receiversReady = del != null;                                    if (del == null)                  {                     // No receiver, broken receiver or full receiver so we stop delivering                     if (trace) { log.trace(this + ": no delivery returned for message" + ref + " so no receiver got the message. Delivery is now complete"); }                     break;                  }                  else if (!del.isSelectorAccepted())                  {                     // No receiver accepted the message because no selectors matched, so we create                     // an iterator (if we haven't already created it) to iterate through the refs                     // in the channel. No delivery was really performed                                          if (iter == null)                     {                        iter = messageRefs.iterator();                     }                                       }                  else                  {                     if (trace) { log.trace(this + ": " + del + " returned for message:" + ref); }                                          // Receiver accepted the reference                                          // We must synchronize here to cope with a race condition where message                     // is cancelled/acked in flight while the following few actions are being                     // performed. e.g. delivery could be cancelled acked after being removed from                     // state but before delivery being added (observed).                     synchronized (del)                     {                        if (trace) { log.trace(this + " incrementing delivery count for " + del); }                        // FIXME - It's actually possible the delivery could be cancelled before it reaches                        // here, in which case we wouldn't get a delivery but we still need to increment the                        // delivery count. TODO http://jira.jboss.com/jira/browse/JBMESSAGING-355                        if (!del.isCancelled())                        {                           removeReference(iter);                           // delivered                           if (!del.isDone())                           {                              // Add the delivery to state                              synchronized (deliveryLock)                              {                                 deliveries.add(del);                                                                  if (trace) { log.trace(this + " starting to track  " + del); }                              }                           }                        }                     }                  }               }            }            else            {               // No more refs in channel or only ones that don't match any selectors               if (trace) { log.trace(this + " no more refs to deliver "); }                              break;            }         }      }      catch (Throwable t)      {         log.error(this + " Failed to deliver", t);      }   }