ヘッダーをスキップ
Oracle® Fusion Middleware Oracle WebLogic ServerメッセージドリブンBeanのプログラミング
11g リリース1 (10.3.6)
B61425-04
  ドキュメント・ライブラリへ移動
ライブラリ
製品リストへ移動
製品
目次へ移動
目次

前
 
次
 

7 EJB 3.0準拠MDBの使用

次のトピックでは、EJB 3.0準拠MDBのプログラミングと実装の方法について説明します。

EJB 3.0準拠MDBの実装

EJB 3.0準拠MDBを実装するには、『Oracle WebLogic Server Enterprise JavaBeansバージョン3.0のプログラミング』の「EJB 3.0開発プロセスの概要」に説明されている手順を実行します。

EJB 3.0準拠MDBのプログラミング

EJB 3.0準拠MDBをプログラミングするには、『Oracle WebLogic Server Enterprise JavaBeansバージョン3.0のプログラミング』の「Beanファイルのプログラミング: 通常の手順」で説明されている手順を実行します。

EJBタイプをメッセージドリブンとして宣言するには、@javax.ejb.MessageDrivenアノテーションを使用する必要があります。次のオプション属性を指定できます。

「MDBとメッセージング・モデル」で説明されているメッセージング・モードをサポートするMDBの開発の詳細は、「MDBのプログラミングと構成: 詳細」を参照してください。

アノテーションを使用するMDBサンプル

例7-3はWebLogic MDBを示します。これは、(WebLogic Server 10.3.4以降の)WebLogic JMSキューへのサブスクリプションを使用し、メッセージ・トランザクションを処理してターゲットの宛先にメッセージを転送します。

JMS接続ファクトリMyCFを使用してMDBは接続し、キューMyQueueから受信します。そして、接続ファクトリMyTargetCFから生成された接続を使用して、メッセージをMyTargetDestに転送します。

リソース参照プーリング・ノート: MDBは、リソース参照を使用してMyTargetCFにアクセスします。リソース参照は、『Oracle WebLogic Server JMSのプログラミング』のWebLogic JMSをEJBおよびサーブレットと併用するための拡張サポートに関する項で説明されているように、自動的にJMSプロデューサ・プーリングを有効化します。

キューではなくトピックを使用する同様のサンプルは、例10-1「分散トピックを使用するMDBのサンプル」を参照してください。

例7-3 分散キューを使用するMDBのサンプル

package test;
import javax.annotation.Resources;
import javax.annotation.Resource;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.ejb.MessageDrivenContext;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.jms.*;
 
@MessageDriven(
  name = "MyMDB",
  activationConfig = {
    @ActivationConfigProperty(propertyName  = "destinationType", 
                              propertyValue = "javax.jms.Queue"),
 
    @ActivationConfigProperty(propertyName  = "connectionFactoryJndiName",
                              propertyValue = "MyCF"), // External JNDI Name
 
    @ActivationConfigProperty(propertyName  = "destinationJndiName",
                              propertyValue = "MyQueue") // Ext. JNDI Name
  }
)
 
@Resources ({
  @Resource(name="targetCFRef",        
            mappedName="MyTargetCF",   // External JNDI name 
            type=javax.jms.ConnectionFactory.class),
 
  @Resource(name="targetDestRef", 
            mappedName="MyTargetDest", // External JNDI name
            type=javax.jms.Destination.class)
})

public class MyMDB implements MessageListener {

  // inject a reference to the MDB context

  @Resource
  private MessageDrivenContext mdctx;  

  // cache targetCF and targetDest for re-use (performance) 

  private ConnectionFactory targetCF;
  private Destination targetDest;

  @TransactionAttribute(value = TransactionAttributeType.REQUIRED)
  public void onMessage(Message message) {

    System.out.println("My MDB got message: " + message);

    // Forward the message to "MyTargetDest" using "MyTargetCF"

    Connection jmsConnection = null;

    try {
      if (targetCF == null) 
        targetCF = (javax.jms.ConnectionFactory)mdctx.lookup("targetCFRef");

      if (targetDest == null)
        targetDest = (javax.jms.Destination)mdctx.lookup("targetDestRef");

      jmsConnection = targetCF.createConnection();
      Session s = jmsConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
      MessageProducer mp = s.createProducer(null);

      mp.send(targetDest, message);

    } catch (JMSException e) {

      System.out.println("Forcing rollback due to exception " + e);
      e.printStackTrace();
      mdctx.setRollbackOnly();

    } finally {

      // Closing a connection automatically returns the connection and
      // its session plus producer to the resource reference pool.

      try { if (jmsConnection != null) jmsConnection.close(); }
      catch (JMSException ignored) {};
    }
 
    // emulate 1 second of "think" time
 
    try { Thread.currentThread().sleep(1000); }
    catch (InterruptedException ie) {
      Thread.currentThread().interrupt(); // Restore the interrupted status
    }
  }
 
}