O'Reilly - Java Message Service
O'Reilly - Java Message Service
O'Reilly - Java Message Service
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
8.3 The JMS Resource in J2EE<br />
<strong>Java</strong> <strong>Message</strong> <strong>Service</strong><br />
The JNDI Environment Naming Context (ENC) is central to the J2EE platform. The JNDI<br />
ENC specifies that JMS connection factories (Topic-ConnectionFactory and<br />
QueueConnectionFactory) can be bound within a JNDI namespace and made available to<br />
any J2EE component at runtime. This allows any J2EE component to become a JMS<br />
client.<br />
For example, the Wholesaler JMS client developed in Chapter 4, could be modeled as a<br />
J2EE application client, which would allow it to access a JMS connection factory through<br />
the JNDI ENC:<br />
public class Wholesaler implements javax.jms.<strong>Message</strong>Listener{<br />
public Wholesaler(String username, String password){<br />
try{<br />
}<br />
...<br />
InitialContext jndiEnc = new InitialContext( );<br />
TopicConnectionFactory factory = (TopicConnectionFactory)<br />
jndiEnc.lookup("java:comp/env/jms/broker");<br />
connect = factory.createTopicConnection (username, password);<br />
session =<br />
connect.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);<br />
hotDealsTopic=(Topic)<br />
jndiEnc.lookup("java:comp/env/jms/HotDeals");<br />
publisher = session.createPublisher(hotDealsTopic);<br />
...<br />
} catch (javax.jms.JMSException jmse){<br />
jmse.printStackTrace( ); System.exit(1);<br />
} catch (javax.naming.NamingException jne){<br />
jne.printStackTrace( ); System.exit(1);<br />
}<br />
Notice that the InitialContext did not need a set of vendor specific properties and that the<br />
lookup( ) operations referenced a special namespace, "java:comp/env/jms/", to access the<br />
connection factories. The JNDI ENC allows the J2EE component to remain ignorant of the<br />
actual network location of the administered objects, and even of the vendor that<br />
implements them. This allows the J2EE components to be portable across JMS providers<br />
as well as J2EE platforms. In addition, the JNDI names used to locate objects are logical<br />
bindings, so the topics or queues bound to these names can change independently of the<br />
actual bindings used by the JMS provider.<br />
In the XML deployment descriptor for the Wholesaler application client, the component<br />
developer declares that a JMS connection factory and destination need to be bound within<br />
the JNDI ENC:<br />
<br />
Wholesaler Applicaton<br />
<br />
Hot Deals Broker<br />
jms/broker<br />
javax.jms.TopicConnectionFactory<br />
Container<br />
126