站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > The J2EE 1.4 Tutorial

The Message-Driven Bean Class - The J2EE 1.4 Tutorial

The Message-Driven Bean Class

The code for the SimpleMessageBean class illustrates the requirements of a message-driven bean class:

  • It must implement the MessageDrivenBean and MessageListener interfaces.
  • The class must be defined as public.
  • The class cannot be defined as abstract or final.
  • It must implement one onMessage method.
  • It must implement one ejbCreate method and one ejbRemove method.
  • It must contain a public constructor with no arguments.
  • It must not define the finalize method.

Unlike session and entity beans, message-driven beans do not have the remote or local interfaces that define client access. Client components do not locate message-driven beans and invoke methods on them. Although message-driven beans do not have business methods, they may contain helper methods that are invoked internally by the onMessage method.

The onMessage Method

When the queue receives a message, the EJB container invokes the onMessage method of the message-driven bean.

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 message-driven bean class defines one onMessage method, whose signature must follow these rules:

  • The method must be declared as public and must not be declared as final or static.
  • The return type must be void.
  • The method must have a single argument of type javax.jms.Message.
  • The throws clause must not define any application exceptions.
  • The onMessage method must be invoked in the scope of a transaction that is determined by the transaction attribute specified in the deployment descriptor.

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();
  }
} 

The ejbCreate and ejbRemove Methods

The signatures of these methods have the following requirements:

  • The access control modifier must be public.
  • The return type must be void.
  • The modifier cannot be static or final.
  • The throws clause must not define any application exceptions.
  • The method has no arguments.

In the SimpleMessageBean class, the ejbCreate and ejbRemove methods are empty.