View Javadoc

    1   /***
    2    *
    3    * Copyright 2004 Protique Ltd
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18
    19  package org.codehaus.activemq;
    20
    21  import org.codehaus.activemq.message.ActiveMQDestination;
    22
    23  import javax.jms.JMSException;
    24  import javax.jms.Queue;
    25  import javax.jms.QueueBrowser;
    26  import java.util.Enumeration;
    27
    28  /***
    29   * A client uses a <CODE>QueueBrowser</CODE> object to look at messages on a
    30   * queue without removing them.
    31   * <p/>
    32   * <P>
    33   * The <CODE>getEnumeration</CODE> method returns a <CODE>
    34   * java.util.Enumeration</CODE> that is used to scan the queue's messages. It
    35   * may be an enumeration of the entire content of a queue, or it may contain
    36   * only the messages matching a message selector.
    37   * <p/>
    38   * <P>
    39   * Messages may be arriving and expiring while the scan is done. The JMS API
    40   * does not require the content of an enumeration to be a static snapshot of
    41   * queue content. Whether these changes are visible or not depends on the JMS
    42   * provider.
    43   * <p/>
    44   * <P>
    45   * A <CODE>QueueBrowser</CODE> can be created from either a <CODE>Session
    46   * </CODE> or a <CODE>QueueSession</CODE>.
    47   *
    48   * @see javax.jms.Session#createBrowser
    49   * @see javax.jms.QueueSession#createBrowser
    50   * @see javax.jms.QueueBrowser
    51   * @see javax.jms.QueueReceiver
    52   */
    53
    54  public class ActiveMQQueueBrowser extends ActiveMQMessageConsumer implements
    55          QueueBrowser, Enumeration {
    56
    57      /***
    58       * Constructor for an ActiveMQQueueBrowser - used internally
    59       *
    60       * @param theSession
    61       * @param dest
    62       * @param selector
    63       * @param cnum
    64       * @throws JMSException
    65       */
    66      protected ActiveMQQueueBrowser(ActiveMQSession theSession,
    67                                     ActiveMQDestination dest, String selector, int cnum)
    68              throws JMSException {
    69          super(theSession, dest, "", selector, cnum, theSession.connection.getPrefetchPolicy().getQueueBrowserPrefetch(), false, true);
    70      }
    71
    72      /***
    73       * Gets the queue associated with this queue browser.
    74       *
    75       * @return the queue
    76       * @throws JMSException if the JMS provider fails to get the queue associated
    77       *                      with this browser due to some internal error.
    78       */
    79
    80      public Queue getQueue() throws JMSException {
    81          return (Queue) getDestination();
    82      }
    83
    84      /***
    85       * Gets an enumeration for browsing the current queue messages in the order
    86       * they would be received.
    87       *
    88       * @return an enumeration for browsing the messages
    89       * @throws JMSException if the JMS provider fails to get the enumeration for this
    90       *                      browser due to some internal error.
    91       */
    92
    93      public Enumeration getEnumeration() throws JMSException {
    94          checkClosed();
    95          //ok - started browsing - wait for inbound messages
    96          if (super.messageQueue.size() == 0) {
    97              try {
    98                  Thread.sleep(1000);
    99              }
    100             catch (InterruptedException e) {
    101             }
    102         }
    103         return this;
    104     }
    105
    106     /***
    107      * @return true if more messages to process
    108      */
    109     public boolean hasMoreElements() {
    110         // TODO Auto-generated method stub
    111         return super.messageQueue.size() > 0;
    112     }
    113
    114     /***
    115      * @return the next message
    116      */
    117     public Object nextElement() {
    118         Object answer = null;
    119         try {
    120             answer = super.receiveNoWait();
    121         }
    122         catch (JMSException e) {
    123             e.printStackTrace();
    124         }
    125         return answer;
    126     }
    127
    128 }