O'Reilly - Java Message Service
O'Reilly - Java Message Service
O'Reilly - Java Message Service
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
<strong>Java</strong> <strong>Message</strong> <strong>Service</strong><br />
Although session, entity, and web components can all act as JMS producers, these<br />
components can only consume JMS messages synchronously using the<br />
<strong>Message</strong>Consumer.receive( ) methods. Calling one of the receive( ) methods causes the<br />
JMS client to pole the queue or topic and wait for a message. [1] These methods are used to<br />
consume messages synchronously, while <strong>Message</strong>Listener objects are used to consume<br />
messages asynchronously.<br />
[1] It's recommended that the component developer use the nonblocking method, receiveNoWait(<br />
), to conserve resources and avoid blocking. Unrestricted blocking is not limited to any length of<br />
time, and is therefore risky.<br />
Only the message-driven bean and application client components can both produce and<br />
consume asynchronous messages. The web, session, and entity components cannot act as<br />
asynchronous JMS consumers because they are driven by synchronous request-reply<br />
protocols, not asynchronous messages. Web components respond to HTTP requests while<br />
entity and session beans respond to <strong>Java</strong> RMI-IIOP requests.<br />
The fact that neither web components nor session and entity beans can asynchronously<br />
consume JMS messages was one of the things that led to development of the messagedriven<br />
bean. The message-driven bean provides J2EE developers with a server-side JMS<br />
consumer that can consume asynchronous messages, something they didn't have before<br />
EJB 2.0.<br />
8.4 The New <strong>Message</strong>-Driven Bean in EJB 2.0<br />
While most JMS vendors provide the message-brokering facilities for routing messages<br />
from producers to consumers, the responsibility for implementing JMS clients is left to the<br />
application developer. In many cases the JMS clients that consume and process messages<br />
need a lot of infrastructure in order to be robust, secure, fault-tolerant, and scalable. JMS<br />
clients may access databases and other resources, use local and distributed transactions,<br />
require authentication and authorization security, or need to process a large load of<br />
concurrent messages. Fulfilling these needs is a tall order, requiring that a significant<br />
amount of work be done by the application developer. In the end, the kind of infrastructure<br />
needed to support powerful JMS consumers is not unlike the infrastructure needed for<br />
session and entity beans, which can produce but not consume messages asynchronously. [2]<br />
[2] Entity and session beans can technically consume JMS messages synchronously by polling the<br />
destination using the <strong>Message</strong>Consumer.receive( ) methods.<br />
In recognition of this need, EJB 2.0 now includes the <strong>Message</strong>DrivenBean type, which can<br />
consume JMS messages, and process them in the same robust component-based<br />
infrastructure that session and entity beans enjoy. The <strong>Message</strong>DrivenBean type (messagedriven<br />
bean) is a first-class enterprise bean component that is designed to consume<br />
asynchronous JMS messages. Like stateless session beans, message-driven beans don't<br />
maintain state between requests; they may also have instance variables that are maintained<br />
throughout the bean instance's life, but that may not store conversational state. Unlike all<br />
other bean types, a message-driven bean does not have a remote or home interface,<br />
because the message-driven bean is not an RPC component. It does not have business<br />
methods that are invoked by EJB clients. A message-driven bean consumes messages<br />
delivered by other JMS clients through a message server.<br />
128