JMS Resources in Java EE

The two JMS resources that must be obtained from the JNDI context are the JMS destination (Queue and Topic) and the JMS connection factory (TopicConnectionFactory and QueueConnectionFactory). In previous examples in this book, these resources were obtained by first getting an InitialContext to the JMS provider and then performing a lookup using the published JNDI name. Keeping with our borrower and lender example from the previous chapters, let’s assume that we have a topic connection factory defined in the Java EE application server named “TopicCF,” and a topic used to publish prices also defined in the Java EE application server named “jms/Rates.” In previous examples, the constructor of the Lender class was where we established an InitialContext and obtained the TopicConnectionFactory and Topic via a JNDI lookup:

public class Lender {

   TopicConnection conn = null;
   TopicSession session = null;
   Topic ratesTopic = null;
   TopicPublisher publisher = null;
     
   public Lender() {
     try {

        Context ctx = new InitialContext();

        TopicConnectionFactory factory = (TopicConnectionFactory)
           ctx.lookup("TopicCF");
        conn = factory.createTopicConnection();
        conn.start();

        session = 
           conn.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);

        ratesTopic = (Topic)ctx.lookup("jms/Rates");
        publisher = session.createPublisher(ratesTopic);

        ...
            
     } catch (JMSException jmse) {
        jmse.printStackTrace(); 
     } catch (NamingException jne) {
        jne.printStackTrace();
     }      
   }
   ...
}         

Within the Java EE environment, objects ...

Get Java Message Service, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.