A Message Driven Bean or MDB is a JMS message consumer that usually implements some message processing and/or routing logic. The bean registers interest (via deployment descriptor) in a Queue or Topic of its choice, implements MessageListener and MessageDrivenBean interface, and awaits arrival of asynchronous messages.
1 2 3 4 5 6 7 8 | package javax.ejb; public interface MessageDrivenBean extends EnterpriseBean { public void ejbRemove() throws EJBException; public void setMessageDrivenContext(MessageDrivenContext ctx) throws EJBException; } |
setMessageDrivenContext() method receives the context as an argument and is called by the container during the bean object creation time. The container calls the ejbRemove() method at the time of destroying the bean instance, giving the bean a chance to free any previously allocated resources (in ejbCreate() mostly).
The MessageListener interface defines just a single method:
1 2 3 4 5 | package javax.jms; public interface MessageListener { public void onMessage(Message message); } |
onMessage() method is invoked whenever a message is delivered to the client implementing this interface. The Message object is passed in as a parameter and the implementing class can then process this message.
The method should not throw an application or a javax.rmi.RemoteException. In general, an MDB should not throw any exception, although it may legally do so if desired. From the client view the message consumer continues to exist even if the bean does throw an application exception. In such a case, the container may assign another bean instance to the client.
begin() commit() rollback() getStatus() setRollbackOnly() setTransactionTimeout()
There are several other methods inherited from the EJBContext interface and may be irrelevant for a message-driven bean. For example, the methods getEJBHome() and getCallerPrincipal() should not be used with an MDB.
In case of bean managed, non-declarative transaction, the bean must close the UserTransaction by calling its commit() or rollback() before the onMessage() method returns.

0 Response to “JMS - Message Driven Bean”