background image

Packaging simplemessage Example

<< The Message-Driven Bean Class | Creating the Administered Objects >>
<< The Message-Driven Bean Class | Creating the Administered Objects >>

Packaging simplemessage Example

A message listener method must follow these rules:
The method must be declared as public.
The method must not be declared as final or static.
The onMessage method is called by the bean's container when a message has arrived for the
bean to service. This method contains the business logic that handles the processing of the
message. It is the message-driven bean's responsibility to parse the message and perform the
necessary business logic.
The onMessage method has a single argument: the incoming message.
The signature of the onMessage method must follow these rules:
The return type must be void.
The method must have a single argument of type javax.jms.Message.
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;
logger.info(
"MESSAGE BEAN: Message received: " +
msg.getText());
} else {
logger.warning(
"Message of wrong type: " +
inMessage.getClass().getName());
}
} catch (JMSException e) {
e.printStackTrace();
mdc.setRollbackOnly();
} catch (Throwable te) {
te.printStackTrace();
}
}
Packaging, Deploying, and Running the simplemessage
Example
To package, deploy and run this example, go to the
tut-install/javaeetutorial5/examples/ejb/simplemessage/ directory.
Packaging, Deploying, and Running the simplemessage Example
The Java EE 5 Tutorial · September 2007
678