/*------------------------------------------------------------------------------ * * 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.*; /** * Class SimpleTopicPublisher * * * @author Chad Beaudin * @version 1.0, $date$ */ public class MyTopicPublisher implements Runnable { private Thread t = null; private String topicName = null; private int count = 0; private long delay = 0; /** * Constructor MyTopicPublisher * * * @param topicName * @param count * @param delay * */ public MyTopicPublisher(String topicName, int count, long delay) { this.topicName = topicName; this.count = count; this.delay = delay; init(); } /** * Method init * * */ public void init() { t = new Thread(this); t.start(); } /** * Publishes a small document until killed. * * @see java.lang.Thread#run() */ public void run() { Context jndiContext = null; TopicConnectionFactory topicConnectionFactory = null; TopicConnection topicConnection = null; TopicSession topicSession = null; Topic topic = null; TopicPublisher topicPublisher = null; TextMessage message = null; /* * 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 does not exist, exit. */ try { topicConnectionFactory = (TopicConnectionFactory) jndiContext.lookup("TopicConnectionFactory"); topic = (Topic) jndiContext.lookup(topicName); } 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 publisher and text message. * Send messages, varying text slightly. * Finally, close connection. */ try { topicConnection = topicConnectionFactory.createTopicConnection(); topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); topicPublisher = topicSession.createPublisher(topic); message = topicSession.createTextMessage(); System.out.println("\nPublishing " + count + " messages with a delay time of " + delay + " milliseconds\n"); for (int i = count; i != 0; i--) { //if -1, continues for long time try { message.setText("This is a text message " + (i + 1) + " of " + count); System.out.println("Publishing message: " + message.getText()); topicPublisher.publish(message); t.sleep(delay); } catch (Exception e) { System.out.println(e); System.exit(0); } } } catch (JMSException e) { System.out.println("Exception occurred: " + e.toString()); } finally { if (topicConnection != null) { try { topicConnection.close(); } catch (JMSException e) {} } } System.exit(0); } /** * Main method. * * @param args the topic used by the example and, * optionally, the number of messages to send */ public static void main(String[] args) { String topicName = null; int count = 0; long delay = 0; for (int i = 0; i < args.length; i++) { if (args[i].startsWith("-topic")) { topicName = args[i].substring(args[i].indexOf('=') + 1); } else if (args[i].startsWith("-count")) { //set the number of times to publish before exiting try { count = Integer.parseInt(args[i].substring(args[i].indexOf('=') + 1)); } catch (Exception e) { System.err.print("Count not set properly, default to -1"); count = -1; } } else if (args[i].startsWith("-delay")) { //set the length of the delay between messages try { delay = Long.parseLong(args[i].substring(args[i].indexOf('=') + 1)); } catch (Exception e) { System.err.print("Unable to set Delay, default to 0 ms"); delay = 0; } } else { System.out.println("VALID USAGE\n-----------------------------"); System.out.println("-topic=\"your topic\""); System.out.println("-count=\"number of times to publish\"\n"); System.out.println("-delay=\"delay between publishes\"\n"); System.exit(1); } } MyTopicPublisher publisher = new MyTopicPublisher(topicName, count, delay); } }