Show TOC

 Synchronously Receiving Messages Sent to a TopicLocate this document in the navigation structure

This example uses the TopicSubscriber to receive a specified TextMessage:

import java.util.Properties;
import javax.naming.*;
import javax.jms.*;
 
/**
 * This class creates a topic and receives messages sent to that topic.
 */
public class SynchTopic {
   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;
   TopicSubscriber topicSubscriber = 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 TopicConnectionFactory
         topicConnectionFactory =
            (TopicConnectionFactory) context.lookup(
               "jmsfactory/default/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 {
 
         //close the jms topic session
         topicSession.close();
 
         //close the jms topic connection
         topicConnection.close();
 
      } catch (JMSException jmse) {
         System.out.println("JMSException: " + jmse);
      }
   }
 
   /**
    * Creates topic connection and session. Receives messages sent
    * to the topic.
    */
   public synchronized void aMethod() {
      try {
         // initializes all important data that will be used.
         initJMS();
 
         // create topic session
         topicSession =
            topicConnection.createTopicSession(
               false,
               Session.AUTO_ACKNOWLEDGE);
 
         // create topic
         topic = topicSession.createTopic("ExampleTopic");
 
         // create subscriber
         topicSubscriber = topicSession.createSubscriber(topic);
 
         textMessage = (TextMessage) topicSubscriber.receive();
 
         // get message contents
         textMessage.getText();
 
         // Closes all resources used in this test.
         closeJMS();
      } catch (JMSException e) {
         System.out.println("" + e);
      }
   }
}