The J2EETM Tutorial
Home
TOC
Index
PREV TOP NEXT Search
Feedback

The Message-Driven Bean Class

The code for the SimpleMessageEJB class illustrates the requirements of a message-driven bean class:

Unlike session and entity beans, message-driven beans do not have the remote or local interfaces that define client access. Client components do not locate message-driven beans and invoke methods on them. Although message-driven beans do not have business methods, they may contain helper methods that are invoked internally by the onMessage method.

The onMessage Method

When the queue receives a message, the EJB container invokes the onMessage method of the message-driven bean. In the SimpleMessageBean class, the onMessage method casts the incoming message to a TextMessage and displays the text:

public void onMessage(Message inMessage) {	
    TextMessage msg = null;	
	
    try {	
        if (inMessage instanceof TextMessage) {	
            msg = (TextMessage) inMessage;	
            System.out.println	
                ("MESSAGE BEAN: Message received: " 	
                + msg.getText());	
        } else {	
            System.out.println	
                ("Message of wrong type: " 	
                + inMessage.getClass().getName());	
        }	
    } catch (JMSException e) {	
        e.printStackTrace();	
        mdc.setRollbackOnly();	
    } catch (Throwable te) {	
        te.printStackTrace();	
    }	
}
 

The ejbCreate and ejbRemove Methods

The signatures of these methods have the following requirements:

In the SimpleMessageBean class, the ejbCreate and ejbRemove methods are empty.

Home
TOC
Index
PREV TOP NEXT Search
Feedback