active = false; grabbing = false;
if (!selectorRejected) { active = false; grabbing = false; }
   public Delivery handle(DeliveryObserver observer, Routable reference, Transaction tx)   {      if (trace) { log.trace(this + " receives reference " + reference.getMessageID() + " for delivery"); }      if (!isReady())      {         if (trace) { log.trace(this + " rejects reference with ID " + reference.getMessageID()); }         return null;      }            try      {         Delivery delivery = null;                  JBossMessage message = (JBossMessage)reference.getMessage();                  boolean accept = this.accept(message);         if (!accept)         {            if (trace) { log.trace(this + " DOES NOT accept the message"); }            return null;         }                  //TODO - We need to put the message in a DLQ         //For now we just ack it otherwise the message will keep being retried         //and we'll never get anywhere         if (reference.getDeliveryCount() > MAX_DELIVERY_ATTEMPTS)         {            log.warn("Message has exceed maximum delivery attempts and will be removed " + message);            delivery = new SimpleDelivery(observer, (MessageReference)reference, true);            return delivery;         }                                           delivery = new SimpleDelivery(observer, (MessageReference)reference);                           deliveries.put(new Long(reference.getMessageID()), delivery);                        //We don't send the message as-is, instead we create a MessageProxy instance         //This allows local fields such as deliveryCount to be handled by the proxy         //but global data to be fielded by the same underlying Message instance.         //This allows us to avoid expensive copying of messages         MessageProxy md = JBossMessage.createThinDelegate(message, reference.getDeliveryCount());                       if (!grabbing)         {            //We want to asynchronously deliver the message to the consumer            //deliver the message on a different thread than the core thread that brought it here                        try            {               if (trace) { log.trace("queueing message " + message + " for delivery to client"); }                              threadPoolDelegate.execute(                  new DeliveryRunnable(md, id, sessionEndpoint.getConnectionEndpoint(), trace));            }            catch (InterruptedException e)            {               log.warn("Thread interrupted", e);            }         }         else         {            //The message is being "grabbed" and returned for receiveNoWait semantics                        toGrab = md;         }                  lastMessageIDDelivered = md.getMessage().getMessageID();                  return delivery;           }      finally      {         active = false;         grabbing = false;      }   }