001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.jms.serverless.client;
023:
024: import javax.naming.Context;
025: import javax.naming.InitialContext;
026: import javax.naming.NamingException;
027: import org.jboss.logging.Logger;
028: import javax.jms.ConnectionFactory;
029: import javax.jms.Connection;
030: import javax.jms.Session;
031: import javax.jms.MessageProducer;
032: import javax.jms.MessageConsumer;
033: import javax.jms.Destination;
034: import javax.jms.TextMessage;
035: import javax.jms.Topic;
036: import javax.jms.MessageListener;
037: import javax.jms.Message;
038: import javax.jms.JMSException;
039:
040: /**
041: * A simple JMS client that consumes messages from a topic. It uses the common JMS 1.1 interfaces.
042: *
043: * @author Ovidiu Feodorov <ovidiu@jboss.org>
044: * @version $Revision: 57195 $ $Date: 2006-09-26 08:08:17 -0400 (Tue, 26 Sep 2006) $
045: **/
046: public class CommonInterfaceSubscriber {
047:
048: private static final Logger log = Logger
049: .getLogger(CommonInterfaceSubscriber.class);
050:
051: private static long counter = 0;
052: private static long startTimestamp = 0;
053: private static long stopTimestamp = 0;
054:
055: /**
056: **/
057: public static void main(String[] args) throws Exception {
058:
059: Context initialContext = new InitialContext();
060:
061: ConnectionFactory connectionFactory = (ConnectionFactory) initialContext
062: .lookup("ConnectionFactory");
063:
064: Destination topic = (Destination) initialContext
065: .lookup("Topic1");
066:
067: Connection connection = connectionFactory.createConnection();
068:
069: Session session = connection.createSession(false,
070: Session.AUTO_ACKNOWLEDGE);
071:
072: MessageConsumer consumer = session.createConsumer(topic);
073: consumer.setMessageListener(new MessageListener() {
074:
075: public void onMessage(Message message) {
076:
077: if (startTimestamp == 0) {
078: startTimestamp = System.currentTimeMillis();
079: }
080:
081: try {
082: TextMessage tm = (TextMessage) message;
083: String text = tm.getText();
084: if (log.isDebugEnabled()) {
085: log.debug("Got message: " + text);
086: }
087:
088: if (!"".equals(text)) {
089: counter++;
090: if (counter % 1000 == 0) {
091: System.out.println(counter);
092: }
093: } else {
094: stopTimestamp = System.currentTimeMillis();
095: long elapsed = stopTimestamp - startTimestamp;
096: int msgPerSec = (int) (((float) counter)
097: / elapsed * 1000);
098: log.info("Received " + counter
099: + " messages in " + elapsed + " ms, "
100: + msgPerSec + " messages per second");
101: System.exit(0);
102: }
103: } catch (JMSException e) {
104: log.error("Error handling the message", e);
105: }
106: }
107: });
108:
109: connection.start();
110: log.info("Connection started, waiting for messages ...");
111: }
112:
113: }
|