Java JMS MessageProducer send message

Description

Java JMS MessageProducer send message

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;

public class Main {
   public static void main(String[] args) {
      try {/*from ww w  .  ja  v a  2  s  .  c o  m*/
         Context ctx = new InitialContext();
         ConnectionFactory cf = (ConnectionFactory) ctx.lookup(args[0]);
         Connection con = cf.createConnection();
         Session sn = con.createSession(false, Session.AUTO_ACKNOWLEDGE);

         Destination dest = sn.createTopic("ATopic");  
         MessageProducer mp = sn.createProducer(dest);  
         TextMessage tm = sn.createTextMessage();  
         tm.setText("A test message");  
         mp.send(tm);  
         System.out.println("Message sent:");  
         sn.close();  
         con.close();  
       } catch (Exception e) { e.printStackTrace();}  
     }  
   } 



PreviousNext

Related