+

Search Tips   |   Advanced Search

Sample JMS 1.1 application client

If we are new to JMS 1.1 application client programming, we can use this example code as a starting-point for developing your client application.


Example

Here is a typical example of JMS 1.1 application client code:
import java.util.Hashtable;
import javax.jms.JMSException;
import javax.naming.Context;
import javax.naming.*;
import javax.jms.*;
 
public class JMSppSampleClient
{
  public static void main(String[] args)
  throws JMSException, Exception
   {
    String  messageID                 = null;
    String  outString                 = null;
    String  cfName                    = "jms/blueconfactory";
    String  qnameIn                   = "java:comp/env/jms/Q1";
    String  qnameOut                  = "jms/bluequename";
    boolean verbose                   = false;
     Session                session    = null;
    Connection             connection = null;
    ConnectionFactory      cf         = null;
    MessageProducer        mp         = null;
    Destination            destination = null;
         try {           Hashtable env = new Hashtable();
     env.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");
     env.put(Context.PROVIDER_URL, "iiop://localhost:2809");
     Context initialContext = new InitialContext(env);
  System.out.println("Getting Connection Factory");
     cf= (ConnectionFactory)initialContext.lookup( cfName );
     System.out.println("Getting Queue");
  destination =(Destination)initialContext.lookup(qnameOut);
         System.out.println("Getting Connection for Queue");
  connection = cf.createConnection();
         System.out.println("staring the connection");
  connection.start();
         System.out.println("creating session");
  session = connection.createSession(false, 1);
         System.out.println("creating messageProducer");
  mp = session.createProducer(destination);
         System.out.println("creating TextMessage");
  TextMessage outMessage = session.createTextMessage("this is test application");
         System.out.println("sending Message");
  mp.send(outMessage);
   mp.close();
  session.close();
  connection.close();
    }
    catch (Exception je)     {}