Show TOC Anfang des Inhaltsbereichs

Diese Grafik wird im zugehörigen Text erklärt Creating a JMS Connection Dokument im Navigationsbaum lokalisieren

Use

No matter whether you want to use the point-to-point or the publish/subscribe model, you must initialize the connection object first. For both messaging styles, this is done in the same way.

Prerequisites

A connection factory must be created and registered in the JNDI context.

Procedure

The JMS Connection is obtained in a few steps.

Diese Grafik wird im zugehörigen Text erklärt

In the example source that follows _CT_ shows the type of the connection:

·        Topic – use this for the publish-subscribe messaging model.

·        Queue – use this for the point-to-point messaging model.

·        XATopic – use this for the transacted publish-subscribe messaging model.

·        XAQueue – use this for the transacted point-to-point messaging model.

Step 1: Set the properties of the JNDI connection

Specify the connection properties. Specify the security properties and all the other properties required to obtain the connection.

Syntax

java.util.Properties properties = new Properties();

// set the properties of the connection

properties.put("Context.INITIAL_CONTEXT_FACTORY", "com.sap.engine.services.jndi.InitialContextFactoryImpl");

properties.put("Context.SECURITY_PRINCIPAL", "Administrator");

properties.put("Context SECURITY_CREDENTIALS", "admin_password");

// start initial context with the properties specified

InitialContext context = new InitialContext(properties);

Hinweis

The properties that have to be set may differ depending on the client type. For example, internal users of the server may not need to specify properties until external clients have to specify additional properties.

For more information about the security provided with the JMS, see Security on JMS Service.

Step 2: Register the connection factory

Use the default connection factories provided by the J2EE Engine – these are:

·        jmsfactory/default/TopicConnectionFactory

·        jmsfactory/default/QueueConnectionFactory.

First you have to register it using the JMS Connector service. Then to:

·        Look up a connection factory, use the following code:

Syntax

QueueConnectonFactory queueConnectionFactory = (QueueConnectonFactory) context.lookup("java:comp/env/<res-ref-name>");

TopicConnectonFactory topicConnectionFactory = (TopicConnectionFactory) context.lookup("java:comp/env/<res-ref-name>");

·        Look up a destination, use the following code:

Syntax

Queue queue = (Queue) context.lookup("java:comp/env/<res-env-ref -name>");

Topic topic = (Topic) context.lookup("java:comp/env/<res-env-ref-name>");

To perform a lookup operation, you also need a resource reference entry for the relevant administered object in the deployment descriptor of the component that looks up the object:

·        Declare a JMS ConnectionFactory resource in the <resource-ref> element of the standard deployment descriptor for the relevant application component. The name that you specify in the <res-ref-name> tag is the name used for the lookup operation.

·        Declare a JMS Destination resource in the <res-env-ref> element of the standard deployment descriptor of the application component. The name that you specify in the <res-env-ref-name> tag is the name used for the lookup operation.

 

For more information about declaring resource references for enterprise beans, see Declaring Resource References. For Web components, see Configuring Resource References.

 

The J2EE Engine JMS Provider gives you the following types of connection factories:

·        TopicConnectionFactory – enables you to create and configure a topic connection.

·        QueueConnectionFactory – enables you to create and configure a queue connection.

·        XATopicConnectionFactory – enables you to create and configure a transacted topic connection.

·        XAQueueConnectionFactory – enables you to create and configure a transacted queue connection.

Use the following code to lookup the connection factory:

Beispiel

TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) context.lookup("java:comp/env/TopicConnectionFactory");

Empfehlung

We recommend that you use the default connection factories provided by the J2EE Engine, located in jmsfactory/default/TopicConnectionFactory or jmsfactory/default/QueueConnectionFactory.

Step 3: Specify the connection type

Having obtained the connection factory object, use it to create the Connection object of the required type – Topic, Queue, XATopic or XAQueue:

Syntax

_CT_ConnectionFactory _ct_ConnectionFactory = (_CT_ConnectionFactory)context.lookup("jmsfactory/default/_CT_ConnectionFactory");

 

Beispiel

// create connection of type Topic

TopicConnection topicConnection = topicConnectionFactory.createTopicConnection();

Step 4: Start the connection

For more information about the JMS messaging models you can use, see Point-to-Point Model and Publish-Subscribe Model.

Start the connection:

Syntax

// start the connection of _CT_ type

_CT_Connection.start();

 

Beispiel

// start the connection of Topic type

topicConnection.start();

Step 5: Create session

Use the connection to create a session object:

Syntax

// create session of _CT_ type

_CT_Session _ct_Session = CT_Connection.create_CT_Session(false/true, Session.ACKNOWLEDGE_TYPE);

You have to specify the Session parameters. The first one shows whether or not the session is transacted. The second parameter specifies the acknowledgement mode. For more information, see Message Acknowledgement.

Beispiel

// create session of Topic type

TopicSession topicSession;

topicSession = topcicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);

For XA sessions:

Syntax

// create session of XATopic type

XATopicSession xatopicSession = xatopicConnection.createXATopicSession(true, Session.AUTO_ACKNOWLEDGE);

The XA sessions implement theXAResource that is transacted.

Step 6: Create or look up a destination

Now create or look up a destination of the required type. Specify a name of the destination you are creating:

Syntax

// create destination of _CT_ type

_CT_= _CT_Session.create_CT_("Example_destination_name");

 

Beispiel

// create destination of Topic type

Topic = TopicSession.createTopic("Example_destination_name");

Final step: close the connection

When you have finished using the JMS connection, close the producers, consumers, sessions and the connection. This enables you to release the resources that are no longer used. For more information, see Closing the JMS Connection.

Hinweis

You can handle the connection exceptions using your own implementation of the class javax.jms.ExceptionListener. You have to implement the onException() method, and this will allow you in case an exception occurs to perform a scenario of your own and to avoid losing data. For example:

   /**

    * Handle asynchronous errors with the connection.

    */

   public void onException(javax.jms.JMSException jsme) {

      // Tries to reconnect

   }

To register the implementation use:

// Register this class as the exception listener.

connection.setExceptionListener(

   (javax.jms.ExceptionListener) ExceptionListenerImpl);

Achtung

When you implement the onException() method, do not close the client connections that were used up to that moment. The JMS Provider takes care of that. The client has to get the objects he or she requires from the factory that was already obtained.

Result

Having obtained JMS connection, use it to send, receive and manage the JMS messages.

The message sending and receiving depends on the messaging model you selected for the JMS connection. For more information about the messaging styles, see

·        Point-to-Point Model

·        Publish-Subscribe Model

 

 

Ende des Inhaltsbereichs