Entering content frame

This graphic is explained in the accompanying text Asynchronously Receiving Messages Sent to a Topic Locate the document in its SAP Library structure

This example uses the TopicSubscriber to register the class as an javax.jms.MessageListener instance:

import java.util.Properties;

import javax.naming.*;

import javax.jms.*;

/**

 * This class creates a topic connection, and then

 * sends to and receives messages from the Topic.

 */

public class AsynchTopic implements javax.jms.MessageListener {

  

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

 

   TopicConnectionFactory topicConnectionFactory = null;

   TopicConnection topicConnection = null;

   Topic topic = null;

   TopicSession topicSession = null;

   TopicPublisher topicPublisher = null;

   TopicSubscriber topicSubscriber = null;

   TextMessage textMessage1 = null;

  

   boolean flag = true;

   String text = "test string";

 

   /**

    * Get 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;

   }

 

   /**

    * Initializes JMS.

    */

   private void initJMS() {

      try {

         InitialContext context = getInitialContext();

 

         // look up the TopicConnectionFactory

         topicConnectionFactory =

            (TopicConnectionFactory) context.lookup(

               "java:comp/env/TopicConnectionFactory");

 

         // create topic connection

         topicConnection = topicConnectionFactory.createTopicConnection();

 

         // start the connection

         topicConnection.start();

 

      } catch (NamingException ne) {

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

      } catch (JMSException jmse) {

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

      }

   }

 

   /**

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

    * when you want to finish using JMS.

    */

   private void closeJMS() {

      try {

 

         //closes the jms topic session

         topicSession.close();

 

         //closes the jms topic connection

         topicConnection.close();

 

      } catch (JMSException jmse) {

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

      }

   }

 

   /**

    * Create a topic, send and receive messages

    * and then close the connection.

    */

   public void aMethod() {

      try {

 

         // initializes all important data that will be used.

         initJMS();

 

         // create topic session

         topicSession =

            topicConnection.createTopicSession(

               false,

               Session.AUTO_ACKNOWLEDGE);

 

         // topic session's topic

         topic = topicSession.createTopic("ExampleTopic");

 

         // create sender

         topicPublisher = topicSession.createPublisher(topic);

 

         //create subscriber

         topicSubscriber = topicSession.createSubscriber(topic);

         topicSubscriber.setMessageListener(this);

 

         // create text message

         textMessage1 = topicSession.createTextMessage();

         textMessage1.setText(text);

 

         // send the message

         topicPublisher.publish(textMessage1);

 

         //topic subscriber is waiting for messages.

         while (flag) {

            try {

               Thread.sleep(1000);

            } catch (Exception e) {

               break;

            }

         }

 

         // cleares all resources used by JMS

         closeJMS();

 

      } catch (JMSException e) {

         e.printStackTrace();

      }

   }

 

   /**

    * Implemented from the MessageListener interface.

    * Allows for asynchronous message receipt. Here you can

    * unpack the message.

    * @param message the newly received message

    */

   public void onMessage(Message message) {

      // do something with the message received.

      try {

         System.out.println(

            "Message reveived: " + ((TextMessage) message).getText());

         flag = false;

      } catch (JMSException jmse) {

         jmse.printStackTrace();

      }

   }

 

}

 

 

Leaving content frame