!--a11y-->
Creating a JMS Connection 
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.
A connection factory must be created and registered in the JNDI context.
The JMS Connection is obtained in a few steps.

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.
Specify the connection properties. Specify the security properties and all the other properties required to obtain the connection.

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

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

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:

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:

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

We recommend that you use the default connection factories provided by the SAP J2EE Engine, located in jmsfactory/default/TopicConnectionFactory or jmsfactory/default/QueueConnectionFactory.
Having obtained the connection factory object, use it to create the Connection object of the required type – Topic, Queue, XATopic or XAQueue:

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

// create connection of type Topic TopicConnection topicConnection; topicConnection = topicConnectionFactory.createTopicConnection(); |
For more information about the JMS messaging models you can use, see Point-to-Point Model and Publish-Subscribe Model.
Start the connection:

// start the connection of _CT_ type _CT_Connection.start(); |

// start the connection of Topic type topicConnection.start(); |
Use the connection to create a session object:

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

// create session of Topic type TopicSession topicSession; topicSession = topcicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); |
For XA sessions:

// create session of XATopic type XATopicSession xatopicSession; xatopicSession = xatopicConnection.createXATopicSession(true, Session.AUTO_ACKNOWLEDGE); |
The XA sessions implement the XAResource that is transacted.
Now create or look up a destination of the required type. Specify a name of the destination you are creating:

// create destination of _CT_ type _CT_= _CT_Session.create_CT_("Example_destination_name"); |

// create destination of Topic type Topic = TopicSession.createTopic("Example_destination_name"); |
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.
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
