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.Destination ; 18 import javax.jms.TextMessage ; 19 import javax.jms.Topic ; 20 21 27 public class CommonInterfacePublisher { 28 29 private static final Logger log = Logger.getLogger(CommonInterfacePublisher.class); 30 31 private static final int DEFAULT_NUMBER_OF_MESSAGES = 10; 32 33 35 public static void main(String [] args) throws Exception { 36 37 Context initialContext = new InitialContext (); 38 39 ConnectionFactory connectionFactory = 40 (ConnectionFactory )initialContext.lookup("ConnectionFactory"); 41 42 Destination topic = (Destination )initialContext.lookup("Topic1"); 43 44 Connection connection = connectionFactory.createConnection(); 45 46 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 47 48 MessageProducer producer = session.createProducer(topic); 49 connection.start(); 50 Thread.sleep(1000); 51 52 int numberOfMessages = getNumberOfMessages(args); 53 log.info("Sending "+numberOfMessages+" text messages ..."); 54 55 for(int i = 0; i < numberOfMessages; i++) { 56 TextMessage message = session.createTextMessage(); 57 message.setText("This is message "+i); 58 producer.send(message); 59 log.debug("sent message "+i); 60 } 61 62 TextMessage message = session.createTextMessage(); 63 message.setText(""); 64 producer.send(message); 65 log.debug("sent end-of-communication message"); 66 67 log.info("Finished sending messages"); 68 69 } 76 77 78 private static int getNumberOfMessages(String [] args) { 79 80 int result = DEFAULT_NUMBER_OF_MESSAGES; 81 82 if (args.length > 0) { 83 try { 84 result = Integer.parseInt(args[0]); 85 } 86 catch(Exception e) { 87 log.warn("Invalid number of messages: "+args[0]); 88 } 89 } 90 91 return result; 92 } 93 94 } 95 96 97 98 | Popular Tags |