23.07.2013 Views

O'Reilly - Java Message Service

O'Reilly - Java Message Service

O'Reilly - Java Message Service

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Figure 8.3. Lifecycle of a message-driven bean<br />

<strong>Java</strong> <strong>Message</strong> <strong>Service</strong><br />

The Retailer JMS client developed in Chapter 4 can easily be converted to a messagedriven<br />

bean. When messages are received from the wholesalers, Retailer<strong>Message</strong>Bean can<br />

process them quickly and efficiently, providing a more scalable option then the JMS<br />

clients we developed in Chapter 4:<br />

public class Retailer<strong>Message</strong>Bean<br />

implements javax.ejb.<strong>Message</strong>DrivenBean, javax.jms.<strong>Message</strong>Listener {<br />

private javax.jms.QueueConnection connect = null;<br />

private javax.jms.QueueSession session = null;<br />

private javax.jms.QueueSender sender = null;<br />

private javax.jms.Queue buyQueue = null;<br />

public void set<strong>Message</strong>DrivenContext(<strong>Message</strong>DrivenContext mdc){<br />

}<br />

public void ejbCreate( ){<br />

try {<br />

InitialContext jndiEnc = new InitialContext( );<br />

QueueConnectionFactory factory = (QueueConnectionFactory)<br />

jndiEnc.lookup("java:comp/env/jms/broker");<br />

connect = factory.createQueueConnection( );<br />

session = connect.createQueueSession<br />

(false,Session.AUTO_ACKNOWLEDGE);<br />

buyQueue = (Queue)<br />

jndiEnc.lookup("java:comp/env/jms/BuyQueue");<br />

sender = session.createSender(buyQueue);<br />

connect.start( );<br />

} catch (javax.jms.JMSException jmse){<br />

throw new javax.ejb.EJBException( );<br />

} catch (javax.naming.NamingException jne){<br />

throw new javax.ejb.EJBException( );<br />

}<br />

}<br />

public void ejbRemove( ){<br />

try {<br />

connect.close( );<br />

130

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!