An MDB class implements the javax.ejb.MessageDrivenBean and javax.jms.MessageListener interfaces, as shown in Figure 22.4. Figure 22.4. The implementation of an MDB class.
The MessageDrivenBean interface, which extends the javax.ejb.EnterpriseBean interface, declares two methods : ejbRemove() and setMessageDrivenContext() . The bean developer must implement these methods along with the ejbCreate() method inherited from the EnterpriseBean interface. The MessageListener interface declares only one method that the bean developer must implement: onMessage() . Listing 22.1 shows the basic components required to implement an MDB. Listing 22.1 The Basic Components of an MDB Classpublic class myMDB implements MessageDrivenBean, MessageListener{ public myMDB() {...}; // Required Bean Constructor public void ejbCreate() {...} public void setMessageDrivenContext(MessageDrivenContext ...) {...} public void onMessage(javax.jms.Message MessageName) {...} public void ejbRemove() {...} } The following sections briefly describe the four methods that constitute an MDB class and discuss how to handle exceptions in MDBs. Note The MDB class is allowed to implement other methods, such as utility methods called internally by onMessage() . However, these methods are never called directly by the container or any client. The ejbCreate() MethodThe container calls the ejbCreate() method for bean instantiation into the WebLogic EJB container's free pool, which is an area of memory WebLogic Server reserves to maintain a set of method-ready stateless bean instances (session and MDBs). This method is a convenient place to implement logic for allocating the necessary resources to process messages. For example, if an MDB sends messages to other destinations, you can place code in the ejbCreate() method to look up JMS API connection factories and destinations and create the JMS API connection. The MDB class must define one ejbCreate() method, and its signature must abide by the following rules:
The setMessageDrivenContext() MethodThe setMessageDrivenContext() method is called before ejbCreate() and invoked by the EJB container to associate an MDB instance with its context, which is maintained by the container. The EJB container passes the javax.ejb. MessageDrivenContext object into this method, which is usually established as a member variable, thus allowing the MDB instance to access its context information in subsequent methods. For example, the transactional context that the EJB container sets for an MDB can be accessed via the following methods from the MessageDrivenContext interface:
Even though the getCallerPrincipal() , isCallerInRole() , getEJBHome() , and getEJBLocalHome() methods are inherited as part of the MessageDrivenContext interface, they should not be called by MDBs for these reasons:
The MDB class must define one setMessageDrivenContext() method. This method's signature must abide by the following rules:
The onMessage() MethodThe EJB container calls the onMessage() method when it determines that an MDB instance should receive a message from a JMS queue or topic. The onMessage() method accepts an object of type javax.jms.Message object as an input argument. Within this method, the bean provider implements the business logic to process the received message, which is cast to one of the five JMS message types. After a message has been processed , the bean instance is available to receive other requests . The MDB class must define one onMessage() method; its signature must abide by the following rules:
The ejbRemove() MethodThe EJB container calls this method to remove the bean from WebLogic Server's free pool. You can also use this method to implement the code for releasing any resources allocated in the bean's ejbCreate() or setMessageDrivenContext() methods, such as JMS or JDBC connections. The MDB class must define one ejbRemove() method. This method's signature must abide by the following rules:
Handling ExceptionsMDBs do not propagate exceptions to the client, and for this reason they should not throw an application exception or a RemoteException . If any of the bean's methods throws such an exception, WebLogic Server immediately removes the EJB instance without calling ejbRemove() . From a client's perspective, the MDB still exists, as future messages are forwarded to a new bean instance that WebLogic Server allocates from the free pool. However, from a WebLogic Server perspective, these types of exceptions are undesirable because any resources the MDB allocated would not be released appropriately. To counter these exceptions, the bean provider must handle all exceptions within the MDB's methods, for example:
In addition to the preceding exception-handling techniques, a bean developer should implement additional internal methods to perform resource cleanup routines outside the ejbRemove() method ”for example, to clean up resources allocated through the ejbCreate() and setMessageDrivenContext() methods. |