    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.10 $
    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      }
    69
    70      protected void subscribe() throws JMSException {
    71          for (int i = 0; i < subjects.length; i++) {
    72              subscribe(subjects[i]);
    73          }
    74      }
    75
    76      protected void subscribe(String subject) throws JMSException {
    77          Session session = createSession();
    78
    79          Destination destination = createDestination(session, subject);
    80
    81          System.out.println("Consuming on : " + destination + " of type: " + destination.getClass().getName());
    82
    83          MessageConsumer consumer = null;
    84          if (isDurable() && isTopic()) {
    85              consumer = session.createDurableSubscriber((Topic) destination, getClass().getName());
    86          }
    87          else {
    88              consumer = session.createConsumer(destination);
    89          }
    90          consumer.setMessageListener(this);
    91          addResource(consumer);
    92      }
    93
    94      public void onMessage(Message message) {
    95          try {
    96              TextMessage textMessage = (TextMessage) message;
    97
    98              // lets force the content to be deserialized
    99              String text = textMessage.getText();
    100             count(1);
    101
    102             // lets count the messages
    103
    104             //message.acknowledge();
    105         }
    106         catch (JMSException e) {
    107             // TODO Auto-generated catch block
    108             e.printStackTrace();
    109         }
    110     }
    111
    112 }