Show TOC Start of Content Area

Background documentation Queue Sender and Receiver  Locate the document in its SAP Library structure

import java.util.Properties;

import javax.naming.*;

import javax.jms.*;

 

/**

 * This class creates a queue then sends and receives (synchronously) messages sent to that queue.

 */

public class SynchQueue {

   private static final String USER = "Administrator";

   private static final String PASSWORD = "admin_pass";

   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";

 

   QueueConnectionFactory queueConnectionFactory = null;

   QueueConnection queueConnection = null;

   Queue queue = null;

   QueueSession queueSession = null;

   QueueReceiver queueReceiver = null;

   QueueSender queueSender = null;

   TextMessage textMessage = null;

 

   /**

    * Gets InitialContext with default values.

    */

   private InitialContext getInitialContext() {

      try {

         // set the properties for the InitalContext

         Properties properties = new Properties();

         properties.put(

            Context.INITIAL_CONTEXT_FACTORY,

            SAP_INITIAL_CONTEXT_FACTORY_IMPL);

         properties.put(Context.PROVIDER_URL, SAP_NAMING_PROVIDER_URL);

         properties.put(Context.SECURITY_PRINCIPAL, USER);

         properties.put(Context.SECURITY_CREDENTIALS, PASSWORD);

 

         // initalize and return the InitalContext with the specified properties

         return new InitialContext(properties);

 

      } catch (NamingException ne) {

         System.out.println("NamingException: " + ne);

      }

 

      return null;

   }

 

   private void initJMS() {

      try {

         InitialContext context = getInitialContext();

 

         // look up the QueueConnectionFactory

         queueConnectionFactory =

            (QueueConnectionFactory) context.lookup(

               " jmsfactory/default/QueueConnectionFactory");

 

         // create queue connection

         queueConnection = queueConnectionFactory.createQueueConnection();

 

         // start the connection

         queueConnection.start();

 

      } catch (NamingException ne) {

         System.out.println("NamingException: " + ne);

      } catch (JMSException jmse) {

         System.out.println("JMSException: " + jmse);

      }

   }

 

   /**

    * Closes all resources used in this test. This should be called

    * when finished using the JMS.

    */

   private void closeJMS() {

      try {

 

         //closes the jms queue session

         queueSession.close();

 

         //closes the jms queue connection

         queueConnection.close();

 

      } catch (JMSException jmse) {

         System.out.println("JMSException: " + jmse);

      }

   }

 

   /**

    * Creates queue connection and session. Receives messages sent

    * to the queue.

    */

   public synchronized void aMethod() {

      try {

         // initializes all important data that will be used.

         initJMS();

 

         // create queue session

         queueSession =

            queueConnection.createQueueSession(

               false,

               Session.AUTO_ACKNOWLEDGE);

 

         // create queue

         queue = queueSession.createQueue("ExampleQueue");

 

         // create sender

         queueSender = queueSession.createSender(queue);

 

         // create receiver

         queueReceiver = queueSession.createReceiver(queue);

 

         // create appropriate message

         textMessage = queueSession.createTextMessage("Just a test");

 

         // send message

         queueSender.send(textMessage);

 

         // receive message

         textMessage = (TextMessage) queueReceiver.receive();

 

         // get message contents

         String messageText = textMessage.getText();

 

         System.out.println("Message text: " + messageText);

 

         // Closes all resorces used in this test.

         closeJMS();

 

      } catch (JMSException e) {

         System.out.println("" + e);

      }

   }

}

 

 

 

End of Content Area