Message driven bean context

Message driven beans can inject the message driven bean context represented by javax.ejb.MessageDrivenBeanContext. This context inherits all the basic properties of the EJB.

It's very similar to the session context shown in Chapter 4, Implementing Business Logic, in the The session context paragraph. Here's a list of operations that you can do with the message driven bean context:

  • Lookup: Through this, you can look up EE components and resources using JNDI.
  • Caller principal: If you are in a secure context, you will get the login information by the java.security.Principal interface.
  • User transaction: If you are in a transactional context, you can get the javax.transaction.UserTransaction interface seen in Chapter 5, Working with Distributed Transactions and operate inside a transaction.
  • Timer service: This object is used to manage the timer. It will be covered in detail further on in this chapter.
  • Context data: A map of properties shared in the runtime execution of the bean.
  • Caller in role: A function to verify if the current logged user is authorized for a defined role.
  • Set rollback only: In the example, it is used through the setRollBackOnly(true) method. It executes the rollback inside a transaction.

Another mode to obtain the message driven bean context is through an interface. For example, our bean can implement the javax.ejb.MessageDrivenBean interface:

...
public
class WorkingBean implements MessageListener, MessageDrivenBean {
private MessageDrivenContext ctx;
...
@Override
public void setMessageDrivenContext(MessageDrivenContext ctx) throws EJBException {
this.ctx = ctx;
}
@Override
public void ejbRemove() throws EJBException {
...
}
...
}

The EJB container will automatically set the context through the setMessageDrivenContext method.

The ejbRemove method is also inherited by the MessageDrivenBean interface. Once the message driven bean is not used by the client, the ejbRemove method will be executed. It can be used to clean resources that are not used anymore.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.39.74