- Characteristics of MDBs
- Elements of MDBs
- The Life Cycle of MDBs
- Comparing MDBs with Session and Entity Beans
- Rules for Writing the MDB Class and Its Methods
- MDB EJB Sample Application: RosterMDB
- RosterApp Deployment Descriptors
- Summary
Rules for Writing the MDB Class and Its Methods
Let's look at the rules for how to implement the MDB class and the ejbCreate(), onMessage(),and ejbRemove()methods.
MDB Class
A bean developer must follow the several important requirements when writing an MDB. The MDB class must directly or indirectly implement both the javax.ejb.MessageDrivenBean and javax.jms.MessageListener interfaces. The class must be defined as public and must not be final or abstract. The MDB class must have a public constructor that takes no arguments and must not define the finalize() method. The class must implement the ejbCreate() method and onMessage() and is allowed to include other methods that are internally invoked by the onMessage() methods, in addition to methods required by the EJB specification.
The MDB class can have superclasses and/or superinterfaces. When this occurs, the ejbCreate() method and methods of MessageDrivenBean and MessageListener interfaces may be defined in the MDB class or any of its superclasses.
ejbCreate Method
The MDB class must define only one ejbCreate() method, which must be named ejbCreate(). It must be declared as public and must not be declared as final or static. The return type must be void and must not have any arguments. The throw clause must not define any application exceptions.
onMessage Method
When a message arrives, the container picks an MDB instance from the pool, invokes onMessage(msg), and passes the JMS message an argument. Let's look at the rules on how to write an onMessage method in an MDB.
The MDB class must define one onMessage() method, which must be declared as public. It cannot be declared final or static. The return must be void and must have a single argument of the type javax.jms.Message. The throw clause may not define any application exceptions.
ejbRemove Method
The container calls the ejbRemove() method to remove an MDB instance from the container. Let's look at rules for writing the ejbRemove() method. The MDB class must define one remove method, which must be named ejbRemove(). It must be declared as public. The method must not be declared as final or static. The return type must be void and it must have no argument. The throw clause must not define any application exceptions.