Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

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


advertisement

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:


<resource-ref>
   <description> This is a textual description of the resource.</description>
   <res-ref-name>jms/PointToPoint</res-ref-name>
   <res-type>javax.jms.QueueConnectionFactory</res-type>
   <res-auth>Container</res-auth>
</resource-ref>
<resource-env-ref>
   <resource-env-ref-name>jms/MyQueue</resource-env-ref-name>
   <resource-env-ref-type>javax.jms.Queue</resource-env-ref-type>
</resource-env-ref>


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) 
            context.lookup("java:comp/env/jms/PointToPoint");

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



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

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