receivingThread = null;
   public Message receive(long timeout) throws JMSException, InterruptedException   {            long startTimestamp = System.currentTimeMillis();            // make sure the current state allows receiving            synchronized(this)      {         if (listener != null)         {            throw new JBossJMSException("A message listener is already registered");         }         if (receiving)         {            throw new JBossJMSException("Another thread is already receiving");         }         receiving = true;      }            try      {         receivingThread = Thread.currentThread();                  JBossMessage m = null;         while(true)         {            try            {               if (timeout == 0)               {                  if (log.isTraceEnabled()) log.trace("receive with no timeout");                                      m = ((JBossMessage)messages.take());                  if (log.isTraceEnabled()) { log.trace("Got message: " + m); }               }               else if (timeout == -1)               {                  //ReceiveNoWait                  if (log.isTraceEnabled()) { log.trace("receive noWait"); }                  m = ((JBossMessage)messages.poll(0));                  return m;               }               else               {                  if (log.isTraceEnabled()) { log.trace("receive timeout " + timeout + " ms"); }                  m = ((JBossMessage)messages.poll(timeout));                                                      if (m == null)                  {                     // timeout expired                     if (log.isTraceEnabled()) { log.trace("Timeout expired"); }                     return null;                  }               }            }            catch(InterruptedException e)            {               if (log.isTraceEnabled()) { log.trace("Thread was interrupted"); }               if (closed)               {                  if (log.isTraceEnabled()) { log.trace("It's closed"); }                  return null;               }               else               {                  throw e;               }            }                        if (log.isTraceEnabled()) { log.trace("Calling delivered()"); }                        // notify that the message has been delivered (not necessarily acknowledged though)            preDeliver(m);            postDeliver(m);                        if (!m.isExpired())            {               if (log.isTraceEnabled()) { log.trace("Message is not expired, returning it to the caller"); }               return m;            }            log.debug("Message expired");            // discard the message, adjust timeout and reenter the buffer            if (timeout != 0)            {               timeout -= System.currentTimeMillis() - startTimestamp;            }         }      }      finally      {         receiving = false;         receivingThread = null;      }   }