O'Reilly logo

Java Message Service by Richard Monson-Haefel, David A Chappell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The JMS Resource in J2EE

The JNDI Environment Naming Context (ENC) is central to the J2EE platform. The JNDI ENC specifies that JMS connection factories (Topic-ConnectionFactory and QueueConnectionFactory) can be bound within a JNDI namespace and made available to any J2EE component at runtime. This allows any J2EE component to become a JMS client.

For example, the Wholesaler JMS client developed in Chapter 4, could be modeled as a J2EE application client, which would allow it to access a JMS connection factory through the JNDI ENC:

public class Wholesaler implements javax.jms.MessageListener{   
   public Wholesaler(String username, String password){
      try{

         InitialContext jndiEnc = new InitialContext( );
                  
         TopicConnectionFactory factory = (TopicConnectionFactory)
            jndiEnc.lookup("java:comp/env/jms/broker");
         connect = factory.createTopicConnection (username, password);

         session = 
          connect.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
         
         hotDealsTopic=(Topic)
             jndiEnc.lookup("java:comp/env/jms/HotDeals");
         publisher = session.createPublisher(hotDealsTopic);

         ...
         
      } catch (javax.jms.JMSException jmse){
         jmse.printStackTrace( ); System.exit(1);
      } catch (javax.naming.NamingException jne){
         jne.printStackTrace( ); System.exit(1);
      }
   }
...

Notice that the InitialContext did not need a set of vendor specific properties and that the lookup( ) operations referenced a special namespace, "java:comp/env/jms/", to access the connection factories. The JNDI ENC allows the J2EE component to remain ignorant of ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required