KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jms > serverless > client > CommonInterfaceSubscriber


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.jms.serverless.client;
8
9 import javax.naming.Context JavaDoc;
10 import javax.naming.InitialContext JavaDoc;
11 import javax.naming.NamingException JavaDoc;
12 import org.jboss.logging.Logger;
13 import javax.jms.ConnectionFactory JavaDoc;
14 import javax.jms.Connection JavaDoc;
15 import javax.jms.Session JavaDoc;
16 import javax.jms.MessageProducer JavaDoc;
17 import javax.jms.MessageConsumer JavaDoc;
18 import javax.jms.Destination JavaDoc;
19 import javax.jms.TextMessage JavaDoc;
20 import javax.jms.Topic JavaDoc;
21 import javax.jms.MessageListener JavaDoc;
22 import javax.jms.Message JavaDoc;
23 import javax.jms.JMSException JavaDoc;
24
25 /**
26  * A simple JMS client that consumes messages from a topic. It uses the common JMS 1.1 interfaces.
27  *
28  * @author Ovidiu Feodorov <ovidiu@jboss.org>
29  * @version $Revision: 1.1 $ $Date: 2004/04/15 22:54:21 $
30  **/

31 public class CommonInterfaceSubscriber {
32
33     private static final Logger log = Logger.getLogger(CommonInterfaceSubscriber.class);
34
35     private static long counter = 0;
36     private static long startTimestamp = 0;
37     private static long stopTimestamp = 0;
38
39     /**
40      **/

41     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
42
43         Context JavaDoc initialContext = new InitialContext JavaDoc();
44
45         ConnectionFactory JavaDoc connectionFactory =
46             (ConnectionFactory JavaDoc)initialContext.lookup("ConnectionFactory");
47
48         Destination JavaDoc topic = (Destination JavaDoc)initialContext.lookup("Topic1");
49
50         Connection JavaDoc connection = connectionFactory.createConnection();
51
52         Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
53         
54         MessageConsumer JavaDoc consumer = session.createConsumer(topic);
55         consumer.setMessageListener(new MessageListener JavaDoc() {
56                 
57                 public void onMessage(Message JavaDoc message) {
58
59                     if (startTimestamp == 0) {
60                         startTimestamp = System.currentTimeMillis();
61                     }
62                     
63                     try {
64                         TextMessage JavaDoc tm = (TextMessage JavaDoc)message;
65                         String JavaDoc text = tm.getText();
66                         if (log.isDebugEnabled()) {
67                             log.debug("Got message: "+text);
68                         }
69
70                         if (!"".equals(text)) {
71                             counter++;
72                             if(counter % 1000 == 0) {
73                                 System.out.println(counter);
74                             }
75                         }
76                         else {
77                             stopTimestamp = System.currentTimeMillis();
78                             long elapsed = stopTimestamp - startTimestamp;
79                             int msgPerSec = (int)(((float)counter) / elapsed * 1000);
80                             log.info("Received "+counter+" messages in " +
81                                      elapsed + " ms, "+msgPerSec+" messages per second");
82                             System.exit(0);
83                         }
84                     }
85                     catch(JMSException JavaDoc e) {
86                         log.error("Error handling the message", e);
87                     }
88                 }
89             });
90
91
92         connection.start();
93         log.info("Connection started, waiting for messages ...");
94     }
95
96 }
97
98
99
100
Popular Tags