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 CommonInterfaceQueueSender { 28 29 private static final Logger log = Logger.getLogger(CommonInterfaceQueueSender.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 queue = (Destination )initialContext.lookup("Queue1"); 43 44 Connection connection = connectionFactory.createConnection(); 45 46 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 47 48 MessageProducer sender = session.createProducer(queue); 49 50 int numberOfMessages = getNumberOfMessages(args); 51 log.info("Sending "+numberOfMessages+" text messages ..."); 52 53 for(int i = 0; i < numberOfMessages; i++) { 54 TextMessage message = session.createTextMessage(); 55 message.setText("QUEUE MESSAGE ["+i+"]"); 56 sender.send(message); 57 log.info("Sent message "+i); 58 } 59 60 log.info("Finished sending messages"); 61 62 67 } 70 71 private static int getNumberOfMessages(String [] args) { 72 73 int result = DEFAULT_NUMBER_OF_MESSAGES; 74 75 if (args.length > 0) { 76 try { 77 result = Integer.parseInt(args[0]); 78 } 79 catch(Exception e) { 80 log.warn("Invalid number of messages: "+args[0]); 81 } 82 } 83 84 return result; 85 } 86 87 88 } 89 90 91 92 | Popular Tags |