Chapter 6: Message-Driven Beans


Overview

Message-driven beans were introduced with version 2.0 of the Enterprise JavaBeans specification and slightly enhanced with version 2.1 (Figure 6-1). With this component type, the Java Message Service (JMS) has become inseparably linked with Enterprise JavaBeans.

click to expand
Figure 6-1: Overview of message-driven beans.

The significant difference between this and other bean types is that a client cannot directly access a message-driven bean. It can address it only indirectly, by sending a message over a particular channel of the message service. A message-driven bean of a particular type receives messages from a particular channel of the message service. It is also significant that the bean does not know which client or user has sent the message. The exchange of messages over a message service transpires anonymously. The application must itself take care of whatever identification may be required. To this end it could send an identifier with the message that enables the sender to be identified.

The component model of Enterprise JavaBeans is given two new aspects through message-driven beans:

  • parallel processing;

  • asynchronicity.

For example, if a client calls a method on the remote interface of an entity bean, then client execution will be blocked until the processing of the entity bean method on the server is complete. Thus we are dealing with a synchronous call. Sending a message via a message service is asynchronous. This means that the client can continue processing as soon as it has sent the message. The processing of the client will not be blocked until the message has been delivered or processed by the receiver. Message-driven beans thus work asynchronously, which distinguishes them in an important way from session and entity beans. Asynchronous processing can greatly reduce server response time. The client does not have to wait for the conclusion of processing-intensive actions, which can run in the background. On the other hand, the client does not automatically know when the processing of the asynchronous process has been completed or whether it was able to be completed successfully at all. The JMS specification takes a number of precautions to make the delivery of a message as secure as possible (with respect to data loss). However, there is no mechanism that provides for informing the client of the successful processing of a message, though such mechanisms can be implemented by means of JMS itself.

To process asynchronous events or to speed up processes in a program, one seeks, as a rule, to parallelize particular processing steps by means of threads. The specification forbids a bean from expressly beginning a new thread or from regulating threads (see also Chapter 3, the section "What an Enterprise Bean May Not Do"). One could attempt to parallelize processes on the client, in order to carry out several calls to Enterprise Beans simultaneously. Except for the case of stateless session beans, this is possible only with certain restrictions. Stateful session beans do not allow a client, for example, to execute several calls to an Enterprise Bean identity simultaneously. With an entity bean identity, simultaneously arriving calls (in different transactions) are serialized by the EJB container, that is, processed sequentially rather than in parallel (depending on the implementation of the EJB container). Such parallelism on the client side would increase the complexity of the client enormously. A reason for the introduction of Enterprise JavaBeans is not least in shifting complexity to the application server and simplifying and slimming down the client program. The application server is the place where one would like to use parallel processing for speeding up the application logic. With the help of the message service, message-driven beans make parallel processing possible.

For the proper implementation of message-driven beans a fundamental understanding of the Java Message Service is essential. The next section will therefore introduce the fundamentals of the Java Message Service. We shall restrict our attention to those aspects that are significant for an understanding of message-driven beans. A complete description of JMS can be found in [29]. It should be noted that as of the 2.1 specification, it is possible to use a non-JMS message service with message-driven beans via a resource adapter. This greatly expands and enhances the integration capabilities of message-driven beans. For the remainder of this chapter we will focus on the use of message-driven beans with the commonly used JMS standard.




Enterprise JavaBeans 2.1
Enterprise JavaBeans 2.1
ISBN: 1590590880
EAN: 2147483647
Year: 2006
Pages: 103

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net