Show TOC Start of Content Area

This graphic is explained in the accompanying text JMS Client Example Locate the document in its SAP Library structure

A JMS client can be a remote client or a client residing on the server. This is the code relevant for the client, assuming that the client’s name is JMSSender.java:

import java.util.Properties;

 

import javax.jms.*;

import javax.naming.*;

 

/**

 * The SimpleClient class sends several messages to a queue.

 */

public class SimpleClient {

   private static final String USER = "Administrator";

 

   private static final String PASSWORD = "abc123";

 

   private static final String SAP_NAMING_PROVIDER_URL = "localhost:50004";

 

   private static final String SAP_INITIAL_CONTEXT_FACTORY_IMPL =

      "com.sap.engine.services.jndi.InitialContextFactoryImpl";

 

   /**

    * Main method.

    */

   public static void main(String[] args) {

      Context jndiContext = null;

      QueueConnectionFactory queueConnectionFactory = null;

      QueueConnection queueConnection = null;

      QueueSession queueSession = null;

      Queue queue = null;

      QueueSender queueSender = null;

      TextMessage message = null;

      final int NUM_MSGS = 3;

 

      /* JNDI service naming environment properties initialization */

      Properties ctxProp = new Properties();

 

      ctxProp.put(

         Context.INITIAL_CONTEXT_FACTORY,

         SAP_INITIAL_CONTEXT_FACTORY_IMPL);

      ctxProp.put(Context.PROVIDER_URL, SAP_NAMING_PROVIDER_URL);

      ctxProp.put(Context.SECURITY_PRINCIPAL, USER);

      ctxProp.put(Context.SECURITY_CREDENTIALS, PASSWORD);

 

      /*

       * Create a JNDI API InitialContext object.

       */

      try {

         jndiContext = new InitialContext(ctxProp);

      } catch (NamingException e) {

         System.out.println(

            "Could not create JNDI API" + "context: " + e.toString());

         e.printStackTrace();

         System.exit(1);

      }

 

      /*

       * Look up connection factory and queue. If either does not exist, exit.

       * If you want to lookup to JMS Connector Factory you should specify

       * argument with value "mdb". Otherwise, for using JMS Provider Factory

       * directly the argument should be with value "java".

       */

 

      if (args[0].equals("mdb")) {

         try {

            queueConnectionFactory =

               (QueueConnectionFactory) jndiContext.lookup(

                  "java:comp/env/jms/MyQueueConnectionFactory");

            queue =

               (Queue) jndiContext.lookup("java:comp/env/jms/QueueName");

         } catch (NamingException e) {

            System.out.println("JNDI API lookup failed: " + e.toString());

            System.exit(1);

         }

      } else if (args[0].equals("java")) {

         try {

            queueConnectionFactory =

               (QueueConnectionFactory) jndiContext.lookup(

                  "jmsfactory/default/QueueConnectionFactory");

            queue =

               (Queue) jndiContext.lookup(

                  "jmsqueues/default/JMSTestDestination");

         } catch (NamingException e) {

            System.out.println("JNDI API lookup failed: " + e.toString());

            e.printStackTrace();

            System.exit(1);

         }

      }

 

      /*

       * Create connection. Create session from connection; false means

       * session is not transacted. Create sender and text message. Send

       * messages, verifying text slightly. Finally, close connection.

       */

      try {

         queueConnection = queueConnectionFactory.createQueueConnection();

         queueSession =

            queueConnection.createQueueSession(

               false,

               Session.AUTO_ACKNOWLEDGE);

         queueSender = queueSession.createSender(queue);

         message = queueSession.createTextMessage();

 

         for (int i = 0; i < NUM_MSGS; i++) {

            message.setText("This is message: " + (i + 1));

            System.out.println("Sending message: " + message.getText());

            queueSender.send(message);

         }

      } catch (JMSException e) {

         System.out.println(" JMS Exception occured: " + e.toString());

         e.printStackTrace();

      } finally {

         if (queueConnection != null) {

            try {

               queueConnection.close();

            } catch (JMSException e) {

               e.printStackTrace();

            }

         }

         System.exit(0);

      }

   }

}

Next Step

The Message Driven Bean

End of Content Area