KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > benchmark > Consumer


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.benchmark;
19
20 import javax.jms.Destination JavaDoc;
21 import javax.jms.JMSException JavaDoc;
22 import javax.jms.Message JavaDoc;
23 import javax.jms.MessageConsumer JavaDoc;
24 import javax.jms.MessageListener JavaDoc;
25 import javax.jms.Session JavaDoc;
26 import javax.jms.TextMessage JavaDoc;
27 import javax.jms.Topic JavaDoc;
28
29 /**
30  * @author James Strachan
31  * @version $Revision$
32  */

33 public class Consumer extends BenchmarkSupport implements MessageListener JavaDoc {
34
35     public static void main(String JavaDoc[] 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 JavaDoc e) {
57             System.out.println("Caught: " + e);
58             e.printStackTrace();
59         }
60     }
61
62     public Consumer() {
63     }
64
65     public void run() throws JMSException JavaDoc {
66         start();
67         subscribe();
68     }
69
70     protected void subscribe() throws JMSException JavaDoc {
71         for (int i = 0; i < subjects.length; i++) {
72             subscribe(subjects[i]);
73         }
74     }
75
76     protected void subscribe(String JavaDoc subject) throws JMSException JavaDoc {
77         Session JavaDoc session = createSession();
78
79         Destination JavaDoc destination = createDestination(session, subject);
80
81         System.out.println("Consuming on : " + destination + " of type: " + destination.getClass().getName());
82
83         MessageConsumer JavaDoc consumer = null;
84         if (isDurable() && isTopic()) {
85             consumer = session.createDurableSubscriber((Topic JavaDoc) 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 JavaDoc message) {
95         try {
96             TextMessage JavaDoc textMessage = (TextMessage JavaDoc) message;
97
98             // lets force the content to be deserialized
99
String JavaDoc text = textMessage.getText();
100             count(1);
101             
102             // lets count the messages
103

104             //message.acknowledge();
105
}
106         catch (JMSException JavaDoc e) {
107             // TODO Auto-generated catch block
108
e.printStackTrace();
109         }
110     }
111
112 }
113
Popular Tags