1 7 package org.jboss.jms.serverless.client; 8 9 import javax.naming.Context ; 10 import javax.naming.InitialContext ; 11 import javax.naming.NamingException ; 12 import org.jboss.logging.Logger; 13 import javax.jms.ConnectionFactory ; 14 import javax.jms.Connection ; 15 import javax.jms.Session ; 16 import javax.jms.MessageProducer ; 17 import javax.jms.MessageConsumer ; 18 import javax.jms.Destination ; 19 import javax.jms.TextMessage ; 20 import javax.jms.Topic ; 21 import javax.jms.MessageListener ; 22 import javax.jms.Message ; 23 import javax.jms.JMSException ; 24 25 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 41 public static void main(String [] args) throws Exception { 42 43 Context initialContext = new InitialContext (); 44 45 ConnectionFactory connectionFactory = 46 (ConnectionFactory )initialContext.lookup("ConnectionFactory"); 47 48 Destination topic = (Destination )initialContext.lookup("Topic1"); 49 50 Connection connection = connectionFactory.createConnection(); 51 52 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 53 54 MessageConsumer consumer = session.createConsumer(topic); 55 consumer.setMessageListener(new MessageListener () { 56 57 public void onMessage(Message message) { 58 59 if (startTimestamp == 0) { 60 startTimestamp = System.currentTimeMillis(); 61 } 62 63 try { 64 TextMessage tm = (TextMessage )message; 65 String 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 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 |