J2EE Messaging
907
Message Bean Requirements
The message-driven bean provider is responsible for providing the message-driven bean class. The
message-driven bean class must directly or indirectly implement the MessageDrivenBean interface.
Right now the only type of message-driven bean is a JMS message bean, but the EJB architects designed
the message-driven bean to be able to support different types of messaging. All JMS message-driven
beans must also implement the MessageListener interface.
The developer must implement the following methods on a message-driven bean:
public void ejbActivate(){}
public void ejbRemove(){}
public void ejbPassivate(){}
public void ejbCreate () throws CreateException{}
public void setMessageDrivenContext(MessageDrivenContext ctx){}
public void onMessage(Message message)
The ejbActivate(), ejbRemove(), ejbPassivate(), ejbCreate(), and
setMessageDrivenContext() methods are the same as the equivalents in entity and session beans.
For more details on this see Chapter 19.
Methods of a message driven bean may throw any exceptions. If a message-driven bean does throw an
exception the server will log the exception and the instance of the bean will be removed. The server can
still create new instances of the message-driven bean to service new messages sent to the bean.
When the server removes an instance of a message-driven bean because it throws an exception it does not
call ejbRemove(). Therefore, you cannot rely on ejbRemove() being called under all circumstances.
The OnMessage() Method
The onMessage() method is the heart of the message-driven bean. It is just like the onMessage()
methods we have seen for the other message consumers (Subscriber and Receiver).
A message-driven bean's onMessage() method is called when a message is sent to that bean's JMS
destination. The message is sent into the method in the message parameter. The onMessage()
method contains the business logic that is to be executed when the bean receives a message.
You must take extra care to ensure that the business logic within your message-driven
beans accounts for asynchronous message processing. For example, the bean cannot
assume that it receives messages in the order they are sent.
Like session and entity beans, a message-driven bean may implement other methods in addition to the
methods required by the EJB specification. Most message-driven beans have private helper methods
that are called from within the onMessage() method.