ヘッダーをスキップ

Oracle Containers for J2EE Enterprise JavaBeans開発者ガイド
10g(10.1.3.1.0)

B31852-03
目次
目次
索引
索引

戻る 次へ

17 EJB 2.1メッセージドリブンBeanの実装

この章では、EJB 2.1メッセージドリブンBean(MDB)の実装方法を説明します。

詳細は、次を参照してください。

EJB 2.1 MDBの実装

表17-1に、EJB 2.1メッセージドリブンBeanの重要な構成要素をまとめ、次の手順でこれらの構成要素の実装方法を説明します。一般的な実装は、「Javaの使用方法」を参照してください。

表17-1    EJB 2.1 MDBエンティティBeanの構成要素 
構成要素  説明 

Beanの実装 

このクラスはpublicとして宣言される必要があり、空のデフォルトのpublicコンストラクタ、引数のない1つのpublic void ejbCreateメソッドが含まれ、finalize()メソッドは含まれません。

ライフ・サイクル・メソッドejbRemoveの空の実装およびsetMessageDrivenContextメソッドの実装を提供するようにjavax.ejb.MessageDrivenBeanを実装します。

onMessageメソッドの実装を提供するようにjavax.jms.MessageListenerを実装します。 

詳細は、「メッセージドリブンBeanとは」を参照してください。


注意

EJBコード例は、http://www.oracle.com/technology/tech/java/oc4j/demosからダウンロードできます。 


EJB 2.1メッセージドリブンBeanを実装するには、次のようにします。

  1. MDBエンティティBeanを実装します。

    1. 引数のないpublicコンストラクタを実装します。

    2. ビジネス・ロジックに使用されるBeanまたはパッケージに対してプライベートであるメソッドを実装します。これには、パブリック・メソッドがリクエストされた作業の完了に使用するプライベート・メソッドも含まれます。

    3. ejbCreateメソッドを実装します。コンテナは、MDBをインスタンス化するときに、このメソッドを起動します。

      ejbCreateメソッドの戻り型はvoidです。

    4. javax.ejb.MessageDrivenBeanインタフェース・コンテナのコールバック・メソッドの空の実装を提供します。

      詳細は、「EJB 2.1 MDBのライフ・サイクル・コールバック・メソッドの構成」を参照してください。

    5. MessageDrivenContextのインスタンスを取得するsetMessageDrivenContextメソッドを実装します(「setMessageDrivenContextメソッドの実装」を参照)。

    6. 適切なメッセージ・リスナー・インタフェースを実装します。

      JMSメッセージドリブンBeanでは、javax.jms.MessageListenerインタフェースを実装してonMessagesメソッドにシグネチャを提供します。

      public void onMessage(javax.jms.Message message)
      
      

      JMS以外のメッセージ・サービス・プロバイダでは、指定する1つまたは複数のメッセージ・リスナー・インタフェースを実装します。

      このメソッドは、着信メッセージを処理します。ほとんどのMDBは、メッセージをキューまたはトピックから受信し、メッセージ内のリクエストを処理するために、エンティティBeanを起動します。

  2. メッセージ・サービス・プロバイダ情報を構成します(「デプロイXMLの使用方法」を参照)。

    1. 使用するメッセージ・コネクション・ファクトリおよびDestinationをEJBデプロイメント・ディスクリプタ(ejb-jar.xml)で定義します。永続的なサブスクリプションまたはメッセージ・セレクタを使用するかどうかを定義します。

      詳細は、次を参照してください。

    2. リソース参照を使用する場合は、これらをejb-jar.xmlファイルで定義し、OC4J固有のデプロイメント・ディスクリプタ(orion-ejb-jar.xml)で実際のJNDI名にマッピングします。

    3. MDBでコンテナ管理のトランザクション境界が使用される場合は、ejb-jar.xmlファイルの<container-transaction>要素にonMessageメソッドを指定します。

      MDBに関するすべての手順は、onMessageメソッドに記述されている必要があります。MDBはステートレスであるため、onMessageメソッドがすべての作業を実行する必要があります。

      一般に、ejbCreateメソッドに、メッセージ・サービス接続およびセッションを作成しないでください。


      注意

      ただし、OEMS JMSを使用している場合(「OEMS JMS: メモリー内またはファイルベース・プロバイダ」を参照)は、JMSコネクションとセッションをejbCreateメソッドで作成し、ejbRemoveメソッドで破棄することで、MDBを最適化できます。 


Javaの使用方法

例17-1に、EJB 2.1 MDBの一般的な実装を示します。

例17-1    EJB 2.1 MDBの実装

import java.util.*;
import javax.ejb.*;
import javax.jms.*;
import javax.naming.*;

public class rpTestMdb implements MessageDrivenBean, MessageListener {

    private QueueConnection         m_qc    = null;
    private QueueSession            m_qs    = null;
    private QueueSender             m_snd   = null;
    private MessageDrivenContext    m_ctx   = null;

    // Constructor, which is public and takes no arguments
    public rpTestMdb() {
    }

    /**
     * Begin private methods. The following methods
     * are used internally.
     */

...

    /**
     * Begin EJB-required methods. The following methods are called
     * by the container, and never called by client code.
     */

    /**
     * ejbCreate method, declared as public (but not final or
     * static), with a return type of void, and with no arguments.
     */
    public void ejbCreate() {
    }

    public void setMessageDrivenContext(MessageDrivenContext ctx) {
      // As with all enterprise beans, you must set the context in order to be
      // able to use it at another time within the MDB methods
     m_ctx = ctx;
    }

    // life cycle Methods

    public void ejbRemove() {
    }


    /**
     * JMS MessageListener-required methods. The following
     * methods are called by the container, and never called by
     * client code.
     */

    // Receives the incoming Message and displays the text.
    public void onMessage(Message msg) {
       // MDB does not carry state for an individual client
       try {
            Context ctx = new InitialContext();
            // 1. Retrieve the QueueConnectionFactory using a
            // resource reference defined in the ejb-jar.xml file.
            QueueConnectionFactory qcf = (QueueConnectionFactory)
                ctx.lookup("java:comp/env/jms/myQueueConnectionFactory");
            ctx.close();

            // 2. Create the queue connection
            m_qc = qcf.createQueueConnection();
            // 3. Create the session over the queue connection.
            m_qs = m_qc.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
            // 4. Create the sender to send messages over the session.
            m_snd = m_qs.createSender(null);

            // When the onMessage method is called, a message has been sent.
            // You can retrieve attributes of the message using the Message object.
            String txt = ("mdb rcv: " + msg.getJMSMessageID());
            System.out.println(txt + " redel="
                + msg.getJMSRedelivered() + " cnt="
                + msg.getIntProperty("JMSXDeliveryCount"));

            // Create a new message using the createMessage method.
            // To send it back to the originator of the other message,
            // set the String property of "RECIPIENT" to "CLIENT."
            // The client only looks for messages with string property CLIENT.
            // Copy the original message ID into new msg's Correlation ID for
            // tracking purposes using the setJMSCorrelationID method. Finally,
            // set the destination for the message using the getJMSReplyTo method
            // on the previously received message. Send the message using the
            // send method on the queue sender.

            // 5. Create a message using the createMessage method
            Message rmsg = m_qs.createMessage();
            // 6. Set properties of the message.
            rmsg.setStringProperty("RECIPIENT", "CLIENT");
            rmsg.setIntProperty("count", msg.getIntProperty("JMSXDeliveryCount"));
            rmsg.setJMSCorrelationID(msg.getJMSMessageID());
            // 7. Retrieve the reply destination.
            Destination d = msg.getJMSReplyTo();
            // 8. Send the message using the send method of the sender.
            m_snd.send((Queue) d, rmsg);
            System.out.println(txt + " snd: " + rmsg.getJMSMessageID());
            // close the connection
            m_qc.close();
        }
        catch (Throwable ex) {
            ex.printStackTrace();
        }
    }
}

デプロイXMLの使用方法

ejb-jar.xmlファイルを使用している場合は、message-driven要素内で、MDBの名前、クラス、JNDI参照およびJMSのDestinationタイプ(キューまたはトピック)を定義します。トピックが指定されている場合は、それが永続的であるかどうかを定義します。リソース参照を使用している場合は、コネクション・ファクトリとDestinationオブジェクトの両方に対してリソース参照を定義します。

例17-2に、例17-1に示したMDBに対応するejb-jar.xmlファイルのmessage-driven要素を示します。

次の点に注意してください。

キューのかわりに永続的なTopicを構成しようとしている場合は、<message-driven-destination>要素を例17-3のように構成します。

例17-3    永続的なトピックのEJB 2.1 MDBのejb-jar.xml

<message-driven-destination>
<destination-type>javax.jms.Topic</destination-type>
<subscription-durability>Durable</subscription-durability>
</message-driven-destination>

詳細は、「直接メッセージ・サービス・プロバイダにアクセスするためのEJB 2.1 MDBの構成」を参照してください。

setMessageDrivenContextメソッドの実装

MDBのインスタンスは、このメソッドを使用して、コンテキストへの参照を維持します。メッセージドリブンBeanには、コンテナによって維持され、Beanから使用可能なコンテキストが存在します。メッセージドリブン・コンテキスト内のメソッドを使用して、セキュリティおよびトランザクションのロールなどのBeanに関する情報の取得が、Beanによって行われる場合があります。Beanに関してコンテキストから取得可能なすべての情報は、Sun社のEJB仕様を参照してください。

コンテナは、最初にBeanをインスタンス化した後、setMessageDrivenContextメソッドを起動して、Beanからコンテキストを取得できるようにします。コンテナは、トランザクション・コンテキストからはこのメソッドをコールしません。この時点でBeanがコンテキストを保存しなかった場合、Beanは二度とコンテキストにアクセスできなくなります。

例17-4に、メッセージドリブン・コンテキストをctx変数に格納するMDBを示します。

例17-4    setMessageDrivenContextメソッドの実装

import javax.ejb.*;

public class myBean implements MessageDrivenBean, MessageListener {

   MessageDrivenContext m_ctx;

    // setMessageDrivenContext method 
    public void setMessageDrivenContext(MessageDrivenContext ctx) {
     // As with all enterprise beans, you must set the context in order to be
      // able to use it at another time within the MDB methods
     m_ctx = ctx;
    }

   // other methods in the bean
}

戻る 次へ
Oracle
Copyright © 2002, 2008 Oracle Corporation.

All Rights Reserved.
目次
目次
索引
索引