Oracle Containers for J2EE Enterprise JavaBeans開発者ガイド 10g(10.1.3.1.0) B31852-03 |
|
この章では、EJB 2.1メッセージドリブンBean(MDB)の実装方法を説明します。
詳細は、次を参照してください。
表17-1に、EJB 2.1メッセージドリブンBeanの重要な構成要素をまとめ、次の手順でこれらの構成要素の実装方法を説明します。一般的な実装は、「Javaの使用方法」を参照してください。
詳細は、「メッセージドリブンBeanとは」を参照してください。
EJB 2.1メッセージドリブンBeanを実装するには、次のようにします。
public
コンストラクタを実装します。
ejbCreate
メソッドを実装します。コンテナは、MDBをインスタンス化するときに、このメソッドを起動します。ejbCreate
メソッドの戻り型はvoid
です。
javax.ejb.MessageDrivenBean
インタフェース・コンテナのコールバック・メソッドの空の実装を提供します。詳細は、「EJB 2.1 MDBのライフ・サイクル・コールバック・メソッドの構成」を参照してください。
MessageDrivenContext
のインスタンスを取得するsetMessageDrivenContext
メソッドを実装します(「setMessageDrivenContextメソッドの実装」を参照)。
JMSメッセージドリブンBeanでは、javax.jms.MessageListener
インタフェースを実装してonMessages
メソッドにシグネチャを提供します。
public void onMessage(javax.jms.Message message)
JMS以外のメッセージ・サービス・プロバイダでは、指定する1つまたは複数のメッセージ・リスナー・インタフェースを実装します。
このメソッドは、着信メッセージを処理します。ほとんどのMDBは、メッセージをキューまたはトピックから受信し、メッセージ内のリクエストを処理するために、エンティティBeanを起動します。
Destination
をEJBデプロイメント・ディスクリプタ(ejb-jar.xml
)で定義します。永続的なサブスクリプションまたはメッセージ・セレクタを使用するかどうかを定義します。詳細は、次を参照してください。
ejb-jar.xml
ファイルで定義し、OC4J固有のデプロイメント・ディスクリプタ(orion-ejb-jar.xml
)で実際のJNDI名にマッピングします。
ejb-jar.xml
ファイルの<container-transaction>
要素にonMessage
メソッドを指定します。MDBに関するすべての手順は、onMessage
メソッドに記述されている必要があります。MDBはステートレスであるため、onMessage
メソッドがすべての作業を実行する必要があります。
一般に、ejbCreate
メソッドに、メッセージ・サービス接続およびセッションを作成しないでください。
注意
ただし、OEMS JMSを使用している場合(「OEMS JMS: メモリー内またはファイルベース・プロバイダ」を参照)は、JMSコネクションとセッションを |
例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();
}
}
}
ejb-jar.xml
ファイルを使用している場合は、message-driven
要素内で、MDBの名前、クラス、JNDI参照およびJMSのDestination
タイプ(キューまたはトピック)を定義します。トピックが指定されている場合は、それが永続的であるかどうかを定義します。リソース参照を使用している場合は、コネクション・ファクトリとDestination
オブジェクトの両方に対してリソース参照を定義します。
例17-2に、例17-1に示したMDBに対応するejb-jar.xml
ファイルのmessage-driven
要素を示します。
次の点に注意してください。
<ejb-name>
要素で指定されます。
<ejb-class>
要素で定義されます。この要素によって、<message-driven>
要素が特定のMDB実装に結び付けられます。
Destination
タイプは、<message-driven-destination><destination-type>
要素で指定されているQueue
です。
RECIPIENT
がMDB
のメッセージのみを受信することが指定されます。<transaction-type>
要素で定義されます。値は、Container
またはBean
のいずれかです。Container
を指定した場合は、<container-transaction>
要素内でCMTサポート・タイプを指定してonMessage
メソッドを定義します。
<resource-ref>
要素で定義されます。Destination
オブジェクトのリソース参照は<resource-env-ref>
要素で定義されます。...
<enterprise-beans>
<message-driven>
<display-name>testMdb</display-name>
<ejb-name>testMdb</ejb-name>
<ejb-class>rpTestMdb</ejb-class>
<transaction-type>Container</transaction-type>
<message-selector>RECIPIENT='MDB'</message-selector>
<message-driven-destination>
<destination-type>javax.jms.Queue</destination-type>
</message-driven-destination>
<resource-ref>
<description>description</description>
<res-ref-name>jms/myQueueConnectionFactory</res-ref-name>
<res-type>javax.jms.QueueConnectionFactory</res-type>
<res-auth>Application</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
<resource-env-ref>
<resource-env-ref-name>jms/persistentQueue
</resource-env-ref-name>
<resource-env-ref-type>javax.jms.Queue</resource-env-ref-type>
</resource-env-ref>
</message-driven>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>testMdb</ejb-name>
<method-name>onMessage</method-name>
<method-params>
<method-param>javax.jms.Message</method-param>
</method-params>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
...
キューのかわりに永続的なTopic
を構成しようとしている場合は、<message-driven-destination>
要素を例17-3のように構成します。
<message-driven-destination>
<destination-type>javax.jms.Topic</destination-type>
<subscription-durability>Durable</subscription-durability>
</message-driven-destination>
詳細は、「直接メッセージ・サービス・プロバイダにアクセスするためのEJB 2.1 MDBの構成」を参照してください。
MDBのインスタンスは、このメソッドを使用して、コンテキストへの参照を維持します。メッセージドリブンBeanには、コンテナによって維持され、Beanから使用可能なコンテキストが存在します。メッセージドリブン・コンテキスト内のメソッドを使用して、セキュリティおよびトランザクションのロールなどのBeanに関する情報の取得が、Beanによって行われる場合があります。Beanに関してコンテキストから取得可能なすべての情報は、Sun社のEJB仕様を参照してください。
コンテナは、最初にBeanをインスタンス化した後、setMessageDrivenContext
メソッドを起動して、Beanからコンテキストを取得できるようにします。コンテナは、トランザクション・コンテキストからはこのメソッドをコールしません。この時点でBeanがコンテキストを保存しなかった場合、Beanは二度とコンテキストにアクセスできなくなります。
例17-4に、メッセージドリブン・コンテキストをctx
変数に格納するMDBを示します。
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 }
|
![]() Copyright © 2002, 2008 Oracle Corporation. All Rights Reserved. |
|