/*------------------------------------------------------------------------------ * * Boeing-Autometric, Inc. * * Copyright (c) 2002 Boeing-Autometric, Inc. All rights reserved. This * program or documentation contains proprietary confidential information and * trade secrets of Autometric. Reverse engineering of object code is * prohibited. Use of copyright notice is precautionary and does not imply * publication. Any unauthorized use, reproduction or transfer of this * program is strictly prohibited. * * RESTRICTED RIGHTS NOTICE * * Use, duplication, or disclosure by the Government is subject to * restrictions as set forth in subdivision (b)(3)(ii) of the Rights in * Technical Data and Computer Software clause at 252.227-7013. * * Autometric, Inc. * 1330 Inverness Drive, Suite 350 * Colorado Springs, CO 80910 * (719) 637-8332 * *----------------------------------------------------------------------------*/ import javax.jms.*; import javax.naming.*; import java.io.*; /** * Class SimpleTopicSubscriber * * * @author Chad Beaudin * @version 1.0, $date$ */ public class MyTopicSubscriber implements MessageListener { private float previousTime = 0; private float currentTime = 0; /** * Constructor MyTopicSubscriber * * * @param topicName * */ public MyTopicSubscriber(String topicName) { Context jndiContext = null; TopicConnectionFactory topicConnectionFactory = null; TopicConnection topicConnection = null; TopicSession topicSession = null; Topic topic = null; TopicSubscriber topicSubscriber = null; previousTime = System.currentTimeMillis(); /* * Create a JNDI API InitialContext object if none exists yet. * */ try { jndiContext = new InitialContext(); } catch (NamingException e) { System.out.println("Could not create JNDI API " + "context: " + e.toString()); e.printStackTrace(); System.exit(1); } /* * Look up connection factory and topic. If either doe snot exist, exit. * */ try { topicConnectionFactory = (TopicConnectionFactory) jndiContext.lookup("TopicConnectionFactory"); topic = (Topic) jndiContext.lookup(topicName); System.out.println("JNDI API lookup done"); } catch (NamingException e) { System.out.println("JNDI API lookup failed: " + e.toString()); e.printStackTrace(); System.exit(1); } /* * Create connection. * Create session from connection; false means session is * not transacted. * Create subscriber. * Register message listener (TextListener). * Receive text messages from topic. * When all messages have been received, enter Q to quit. * Close connection. */ try { topicConnection = topicConnectionFactory.createTopicConnection(); topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); topicSubscriber = topicSession.createSubscriber(topic); System.out.println("Subscribed to " + topicName); topicSubscriber.setMessageListener(this); topicConnection.start(); System.out.println("Subscriber Started"); } catch (JMSException e) { System.out.println("Exception occurred: " + e.toString()); } } /** * This method is required when your class implements MessageListener. This is the method * that will receive the updates from the topic. * * * @param message * */ public void onMessage(Message message) { TextMessage msg = null; currentTime = System.currentTimeMillis(); try { if (message instanceof TextMessage) { msg = (TextMessage) message; System.out.println("Receiving message: " + msg.getText()); } else { System.out.println("Message of wrong type: " + message.getClass().getName()); } } catch (JMSException e) { System.out.println("JMSException in onMessage(): " + e.toString()); } catch (Throwable t) { System.out.println("Exception in onMessage():" + t.getMessage()); } } /** * Method main * * * @param args * */ public static void main(String[] args) { String topicName = null; for (int i = 0; i < args.length; i++) { if (args[i].startsWith("-topic")) { topicName = args[i].substring(args[i].indexOf('=') + 1); } else { System.out.println("VALID USAGE\n-----------------------------"); System.out.println("-topic=\"your topic\""); System.exit(1); } } //Create the topic subscriber MyTopicSubscriber subscriber = new MyTopicSubscriber(topicName); } }