You can use the Create EJB 3.1 Message-Driven Bean wizard
to create a message-driven bean and add it to your project.
Before you begin
You must have a Java™ project,
an EJB project, or a web project created in your workspace.
About this task
The main difference between a message-driven bean and
a session bean is that a message-driven bean has no local or remote
interface. Instead, it has only a bean class.
Procedure
- In the Java EE perspective,
right-click your project, and select . The Create
EJB 3.1 Message-Driven Bean wizard open.
- In the Source folder field, select
the source folder for the new bean.
- In the Package field, type the package
name for the new bean.
- In the Name field, type the name that you want to
assign to the message-driven bean. By convention, bean names begin
with an uppercase letter.
- In the Destination name field, type the name that
you want to assign to the destination.
- Select JMS to use the Java messaging service, or clear JMS to
use another messaging service.
- In the Destination type field, select Queue or Topic destination
type, and click Next.
- In the Message-Driven bean-specific information page, in
the Transaction type field, select Container for
container-managed transactions or Bean for
bean-managed transactions.
- Click Finish. The Java editor contains the default code for your
message-driven bean class:
package com.ibm.test;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.Message;
import javax.jms.MessageListener;
/**
* Message-Driven Bean implementation class for: TestMdb
*
*/
@MessageDriven(
activationConfig = { @ActivationConfigProperty(
propertyName = "destinationType", propertyValue = "javax.jms.Queue"
) })
public class TestMdb implements MessageListener {
/**
* Default constructor.
*/
public TestMdb() {
// TODO Auto-generated constructor stub
}
/**
* @see MessageListener#onMessage(Message)
*/
public void onMessage(Message message) {
// TODO Auto-generated method stub
}
}
In this example of default message-driven bean code,
the following points are important to note:
- In EJB 3.1, the @MessageDriven annotation specifies
a set of activation configuration parameters. These parameters are
unique to the particular type of JCA 1.5 adapter that is used to drive
the Message-driven bean. Some adapters have configuration parameters
that let you specify the destination queue of the Message-driven bean.
In the case where the adapter does not support this, the destination
name must be specified using a <message-destination>entry in the
XML binding file.
- The bean class has to implement the MessageListener interface,
which defines only one method, onMessage. When a
message arrives in the queue monitored by this MDB, the container
calls the onMessage method of the bean class and
passes the incoming message in as the parameter.
- The ActivationConfigProperty of the @MessageDriven annotation
provides messaging system–specific configuration information.