|
Home TOC Index |
|
Search
Feedback |
The Message-Driven Bean Class
The code for the
SimpleMessageEJBclass illustrates the requirements of a message-driven bean class:
- It implements the
MessageDrivenBeanandMessageListenerinterfaces.- The class is defined as
public.- The class cannot be defined as
abstractorfinal.- It implements one
onMessagemethod.- It implements one
ejbCreatemethod and oneejbRemovemethod.- It contains a public constructor with no arguments.
- It must not define the
finalizemethod.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
onMessagemethod.The onMessage Method
When the queue receives a message, the EJB
container invokes the
onMessagemethod of the message-driven bean. In theSimpleMessageBeanclass, theonMessagemethod casts the incoming message to aTextMessageand 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:
- The access control modifier must be
public.- The return type must be
void.- The modifier cannot be
staticorfinal.- The
throwsclause must not define any application exceptions.- The method has no arguments.
In the
SimpleMessageBeanclass, theejbCreateandejbRemovemethods are empty.
|
Home TOC Index |
|
Search
Feedback |