[Enterprise Extensions only]

Developing a message bean to use extended messaging

Use this task to develop a message bean that a JMS listener invokes when a message arrives on the input queue that the listener is monitoring. You are recommended to develop the message bean to delegate the business processing of incoming messages to another Enterprise JavaBean, to provide clear separation of message handling and business processing. This also enables the business processing to be invoked by either the arrival of incoming messages or, for example, from a WebSphere EJB client. Responses can be handled by another Enterprise JavaBean acting as a sender bean, or handled in the message bean.

You develop a message bean to be invoked by a JMS listener like any other Enterprise JavaBean used by your enterprise applications. To develop a message bean, you complete the following stages:

  1. Write the message bean class.
    By convention, the message bean class is named nameBean, where name is the name you assign to the message bean.

    The message bean class must meet the same requirements as any session bean. It must also define and implement the onMessage() method, which must meet the following requirements:

    • The method must be declared as public.
    • The method 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.

    For example, a basic onMessage() method for a message bean is shown in the following code extract:

    public void onMessage(javax.jms.Message msg)
    {
        try
       {
            System.out.println("MySessBean.onMessage(), msg text: "+((TextMessage)msg).getText());
        }
        catch  (Exception err) 
        { 
            err.printStackTrace(); 
        }
    }
     

    Code example: The onMessage() method of a message bean. This figure shows a code extract for a basic onMessage() method of a message bean. The method extracts the text from an incoming message and sends that text to System.out.

    For more information about writing the message bean class (as a session bean class), see Writing the enterprise bean class (session).

  2. Write the message bean's home interface.
    By convention, the message bean's home interface is named nameHome, where name is the name you assign to the message bean.

    For more information about writing the message bean's home interface, see Writing the home interface (session).

  3. Write the message bean's remote interface.
    By convention, the message bean's remote interface is named name, where name is the name you assign to the message bean.

    For more information about writing the message bean's remote interface, see Writing the remote interface (session).

When you have developed a message bean to use the extended messaging service, you must package and deploy the bean as part of your enterprise application. For more information about packaging and deploying an enterprise application, see:

  1. Packaging enterprise beans.
  2. Deploying enterprise beans.