1 18 27 package org.apache.activemq.simple; 28 29 import javax.jms.Connection ; 30 import javax.jms.ConnectionFactory ; 31 import javax.jms.Destination ; 32 import javax.jms.JMSException ; 33 import javax.jms.MessageProducer ; 34 import javax.jms.Session ; 35 import javax.jms.TextMessage ; 36 37 import org.apache.activemq.ActiveMQConnectionFactory; 38 import org.apache.activemq.command.ActiveMQQueue; 39 40 public class Producer { 41 42 private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory 43 .getLog(Producer.class); 44 45 public static void main(String [] args) throws JMSException , InterruptedException { 46 47 String url = "peer://localhost1/groupA?persistent=false"; 48 if( args.length>0 ) { 49 url = args[0]; 50 } 51 52 ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url); 53 Destination destination = new ActiveMQQueue("TEST.QUEUE"); 54 55 Connection connection = connectionFactory.createConnection(); 56 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 57 MessageProducer producer = session.createProducer(destination); 58 TextMessage message = session.createTextMessage(); 59 for (int i = 0; i < 1000; i++) { 60 message.setText("This is message " + (i + 1)); 61 log.info("Sending message: " + message.getText()); 62 producer.send(message); 63 Thread.sleep(1000); 64 } 65 connection.close(); 66 67 } 68 } 69 70 | Popular Tags |