Java JMS Connection create

Description

Java JMS Connection create

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   w ww .j  a v  a2  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.createQueue(args[0]);
         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