RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


EJB Messaging, Part I: JMS and JavaMail-3 : Page 3


EJBs and JMS Messaging
In order for your enterprise bean to obtain a connection to a JMS session, you must do one of the following:

  • Perform a JNDI lookup to obtain a connection factory reference
  • Obtain a connection via the factory reference
  • Use the topic or queue connection object in the normal fashion for JMS
The container typically maintains a single connection with the message broker, with multiple sessions (each with its own transaction context and acknowledgement mode) being allocated via the connection factory references. Two classes create these resource manager connection factory objects in JMS: javax.jms.QueueConnectionFactory and javax.jms.TopicConnectionFactory.

In addition to the standard deployment descriptor tags, JMS requires an additional declaration. The <resource-env-ref> element defines administered objects, which are essentially container-managed singleton components. In the case of JMS, it declares a topic or queue for the J2EE container to manage:

   <description> This is a textual description of the resource.</description>

The following code sample demonstrates how to obtain a reference to a JMS QueueConnection object based on the deployment descriptor settings above:

// Obtain the initial JNDI context
Context context = new InitialContext();

// JNDI lookup to obtain resource manager connection factory reference
javax.jms.QueueConnectionFactory factory = (javax.jms.QueueConnectionFactory) 

// JNDI lookup to obtain a reference to the queue declared in the 
deployment descriptor javax.jms.Queue queue = (javax.jms.Queue) context.lookup("java:comp/env/jms/MyQueue"); // Invoke factory object to create connection object javax.jms.QueueConnection connection = factory.createQueueConnection(); // Obtain a JMS session from the connection object javax.jms.QueueSession session = connection.createTopicSession( true, 0 );

At this point, you have a reference to a javax.jms.Session object and you are free to build QueueSender and QueueReceiver objects to be used in a point-to-point message exchange with other JMS clients.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date