Entering content frame

This graphic is explained in the accompanying text Creating Message Producer to a Topic Locate the document in its SAP Library structure

Use

The topic message producer is represented by the TopicPublisher class. Use the publisher to send messages to a specified topic.

The topic here is the destination of the publisher – we say that the messages are sent to this topic. You can also create publisher without supplying a destination, but in this case you must specify the destination of the messages each time you want to send it.

Procedure

Create the TopicPublisher using the following source code in the class:

This graphic is explained in the accompanying text

TopicPublisher topicPublsiher = topicSession.createTopicPublisher(topic);

Example

import javax.jms.*;

import java.util.Properties;

import javax.naming.*;

 

/**

 * An example class that represents a producer to a Topic.

 */

public class Producer {

   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;

 

   /**

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

   }

 

   /**

    * Initialize 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 {

 

         //close the jms topic session

         topicSession.close();

 

         //close the jms topic connection

         topicConnection.close();

 

      } catch (JMSException jmse) {

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

      }

   }

 

   /**

    * Create a topic connection and a session.

    * Then use the session to create a topic publisher.

    */

   public void aMethod() {

      try {

 

         // initializes all important JMS data

         initJMS();

 

         // create topic session

         topicSession = topicConnection.createTopicSession(false, 1);

 

         // create topic destination

         topic = topicSession.createTopic("ExampleTopic");

 

         // create topic publisher

         topicPublisher = topicSession.createPublisher(topic);

 

         /**

          * Publish some messages.

          */

 

         // Closes all resources used in this test.

         closeJMS();

 

      } catch (JMSException jmse) {

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

      }

   }

 

}

 

 

Leaving content frame