    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  package org.codehaus.activemq.benchmark;
    19
    20  import javax.jms.Destination;
    21  import javax.jms.JMSException;
    22  import javax.jms.Message;
    23  import javax.jms.MessageConsumer;
    24  import javax.jms.MessageListener;
    25  import javax.jms.Session;
    26  import javax.jms.TextMessage;
    27  import javax.jms.Topic;
    28
    29  /***
    30   * @author James Strachan
    31   * @version $Revision: 1.9 $
    32   */
    33  public class Consumer extends BenchmarkSupport implements MessageListener {
    34
    35      public static void main(String[] args) {
    36          Consumer tool = new Consumer();
    37          if (args.length > 0) {
    38              tool.setUrl(args[0]);
    39          }
    40          if (args.length > 1) {
    41              tool.setTopic(parseBoolean(args[1]));
    42          }
    43          if (args.length > 2) {
    44              tool.setSubject(args[2]);
    45          }
    46          if (args.length > 3) {
    47              tool.setDurable(parseBoolean(args[3]));
    48          }
    49          if (args.length > 4) {
    50              tool.setConnectionCount(Integer.parseInt(args[4]));
    51          }
    52
    53          try {
    54              tool.run();
    55          }
    56          catch (Exception e) {
    57              System.out.println("Caught: " + e);
    58              e.printStackTrace();
    59          }
    60      }
    61
    62      public Consumer() {
    63      }
    64
    65      public void run() throws JMSException {
    66          start();
    67          subscribe();
    68          waitLoop();
    69      }
    70
    71      protected void subscribe() throws JMSException {
    72          for (int i = 0; i < subjects.length; i++) {
    73              subscribe(subjects[i]);
    74          }
    75      }
    76
    77      protected void waitLoop() {
    78          // now lets sleep until we're done
    79          Object lock = new Object();
    80          synchronized (lock) {
    81              try {
    82                  lock.wait();
    83              }
    84              catch (InterruptedException e) {
    85                  // TODO Auto-generated catch block
    86                  e.printStackTrace();
    87              }
    88          }
    89      }
    90
    91      protected void subscribe(String subject) throws JMSException {
    92          Session session = createSession();
    93
    94
    95          Destination destination = createDestination(session, subject);
    96
    97          System.out.println("Consuming on : " + destination + " of type: " + destination.getClass().getName());
    98
    99          MessageConsumer consumer = null;
    100         if (isDurable() && isTopic()) {
    101             consumer = session.createDurableSubscriber((Topic) destination, getClass().getName());
    102         }
    103         else {
    104             consumer = session.createConsumer(destination);
    105         }
    106         consumer.setMessageListener(this);
    107         addResource(consumer);
    108     }
    109
    110     public void onMessage(Message message) {
    111         try {
    112             TextMessage textMessage = (TextMessage) message;
    113
    114             // lets force the content to be deserialized
    115             String text = textMessage.getText();
    116             count(1);
    117
    118             // lets count the messages
    119
    120             //message.acknowledge();
    121         }
    122         catch (JMSException e) {
    123             // TODO Auto-generated catch block
    124             e.printStackTrace();
    125         }
    126     }
    127
    128 }