Identifying Message-Driven Beans in Enterprise Applications

Remote Procedure Call (RPC)-based communication, such as that employed by session beans, has the disadvantage that the sender must wait for a response. For instance, imagine that you invoke a business method on another session bean. You then need to wait for the method to return so you can undertake the next activity. What if the task you wanted to achieve was time-consuming and not particularly critical? Obviously, it would come down to a decision about how important the task really is and whether the performance impact is justifiable.

A message-based system, on the other hand, does not suffer from this drawback. So, in the same scenario, if the session bean were communicating with a message-driven bean, it could simply send the message along asynchronously to be processed by the message-driven bean in an independent fashion.

In the HomeDirect case study, we could use the message-driven bean to log any interesting operational information and report problems. HomeDirect components would just send the message on to the predetermined topic, and then go on about their business. When the message-driven bean received the message-based request, it would simply handle the message according to the message type. Message-driven beans can also be used to wrap legacy applications to make the integration effort easier.

An example of a session bean and message-driven bean interaction is shown in Figure 14-7. In this scenario, a session bean (after it looks up a Java Naming and Directory Interface [JNDI] destination) publishes a message to a destination. The message is retrieved by the container and leads to an onMessage method on the message-driven bean. At this time, the message-driven bean can process the message as indicated in the onMessage method.

Figure 14-7. Example of an interaction between a session bean and a message-driven bean


The question of how to send a response back to the sending party often comes up. There is no formally defined mechanism in the current version of the EJB 2.0 specification. Typical strategies for dealing with this involve some variation on either setting up a temporary or permanent reply destination and providing information about the reply destination to the message-driven bean in the message itself.

Another thing to watch out for during the development of a message-driven bean is the message type. A message-driven bean should be prepared to handle any of the supported types of messages that are sent to it. For example, a message-driven bean should be able to receive and process a TextMessage while expecting an ObjectMessage. This is because any object that supports the message interface can be a valid message.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.3.72