1 18 19 28 package org.apache.activemq.demo; 29 30 32 import org.apache.activemq.ActiveMQConnectionFactory; 33 34 import javax.jms.*; 35 import javax.naming.Context ; 36 import javax.naming.InitialContext ; 37 import javax.naming.NamingException ; 38 39 44 public class DefaultQueueSender { 45 46 private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory 47 .getLog(DefaultQueueSender.class); 48 49 public static void main(String [] args) { 50 51 String uri = "tcp://localhost:61616"; 52 String text = "Hello World!"; 53 54 Connection connection = null; 55 QueueSession queueSession = null; 56 57 if ((args.length < 1)) { 58 printUsage(); 59 System.exit(1); 60 } 61 62 int idx = 0; 63 String arg = args[0]; 64 if (arg.equals("-uri")) { 65 if (args.length == 1) { 66 printUsage(); 67 System.exit(1); 68 } 69 uri = args[1]; 70 idx += 2; 71 } 72 String queueName = args[idx]; 73 log.info("Connecting to: " + uri); 74 log.info("Queue name is " + queueName); 75 76 if (++idx < args.length) { 77 text = args[idx]; 78 } 79 80 try { 81 ConnectionFactory factory = new ActiveMQConnectionFactory(uri); 82 connection = factory.createConnection(); 83 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 84 Destination destination = session.createQueue(queueName); 85 MessageProducer producer = session.createProducer(destination); 86 87 Message message = session.createTextMessage(text); 88 producer.send(message); 89 } 90 catch (JMSException e) { 91 log.info("Exception occurred: " + e.toString()); 92 } 93 finally { 94 if (connection != null) { 95 try { 96 connection.close(); 97 } 98 catch (JMSException e) { 99 } 100 } 101 } 102 } 103 104 protected static void printUsage() { 105 System.out.println("Usage: java DefaultQueueSender [-uri <connection-uri>] " + "<queue-name> [<message-body>]"); 106 } 107 } 108 109 | Popular Tags |