Entering content frame

This graphic is explained in the accompanying text Creating a JMS Connection Locate the document in its SAP Library structure

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.

This graphic is explained in the accompanying text

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.

This graphic is explained in the accompanying text

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", "sap");

// start initial context with the properties specified

InitialContext context = new InitialContext(properties);

This graphic is explained in the accompanying text

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 documentation

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 documentation

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:

Example

TopicConnectionFactory topicConnectionFactory;

// lookup the TopicConnectionFactory object

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

Recommendation

We recommend that you use the default connection factories provided by the SAP 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:

This graphic is explained in the accompanying text

_CT_ConnectionFactory _CT_ConnectionFactory;

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

 

Example

// create connection of type Topic

TopicConnection 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:

This graphic is explained in the accompanying text

// start the connection of _CT_ type

_CT_Connection.start();

 

Example

// start the connection of Topic type

topicConnection.start();

Step 5: Create session

Use the connection to create a session object:

This graphic is explained in the accompanying text

// create session of _CT_  type

_CT_Session _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.

Example

// create session of Topic type

TopicSession topicSession;

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

For XA sessions:

This graphic is explained in the accompanying text

// create session of XATopic type

XATopicSession xatopicSession;

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

 

The XA sessions implement the XAResource 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:

This graphic is explained in the accompanying text

// create destination of _CT_ type

_CT_= _CT_Session.create_CT_("Example_destination_name");

 

Example

// 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.

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

 

 

Leaving content frame