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

前
 
次
 

7 EJB 3.0準拠MDBの使用

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

EJB 3.0準拠MDBの実装

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

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

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

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

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

アノテーションを使用したMDBの例

この例では、注入されないリソースを参照するMDBのEJB 3.0アノテーションを示します。参照は、MDBインスタンスがインスタンス化されるときではなく、MDBが呼び出されると実行時に解決されます。

例7-1 非注入リソースMDBの例

 
package test;
 
import javax.annotation.Resources;
import javax.annotation.Resource;
import javax.naming.*;
import javax.ejb.*;
import javax.jms.*;
 
import weblogic.javaee.MessageDestinationConfiguration;
import weblogic.javaee.TransactionTimeoutSeconds;
 
@MessageDriven(
  name = "MyMDB",
  mappedName = "JNDINameOfMDBSourceDest"
)
 
// optionally specify a connection factory
// there's no need to specify a connection factory if the source
// destination is a WebLogic JMS destination
 
@MessageDestinationConfiguration( 
  connectionFactoryJNDIName = "JNDINameOfMDBSourceCF"  
)
 
// optionally set a tx timeout, the default timeout is typically 30 seconds
 
@TransactionTimeoutSeconds(value = 60) 
 
// resources that are not injected
 
@Resources ({
  @Resource(name="targetCFRef", 
            mappedName="TargetCFJNDIName",
            type=javax.jms.ConnectionFactory.class), 
 
  @Resource(name="targetDestRef",
            mappedName="TargetDestJNDIName",
            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(TransactionAttributeType.REQUIRED)
  public void onMessage(Message message) {
 
    Connection jmsConnection = null;
 
    try {
      System.out.println("My MDB got message: " + message);
 
      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);
 
      if (message.getJMSReplyTo() != null) 
        mp.send(message.getJMSReplyTo(), s.createTextMessage("My Reply"));
      else
        mp.send(targetDest, message);
      
    } catch (JMSException e) {
      throw new EJBException(e); 
 
    } finally {
 
      // Return JMS resources to the resource reference pool for later re-use.
      // Closing a connection automatically also closes its sessions, etc.
 
      try { if (jmsConnection != null) jmsConnection.close(); }
      catch (JMSException ignored) {};
    }
  }
}

activationConfigのプロパティ

activationConfigのプロパティは名前と値のペアで、MDBがデプロイされるとMDBコンテナに渡されます。プロパティは、MDB Beanクラスでデプロイメント記述子ejb-jar.xmlまたはアノテーション@ActivationConfigPropertyを使用して宣言できます。『Oracle Fusion Middleware Oracle WebLogic Server Enterprise JavaBeansバージョン3.0のプログラミング』の「javax.ejb.ActivationConfigProperty」を参照してください。

例7-2 @ActivationConfigPropertyコード例

. . .
@ActivationConfigProperties(
    {
        @ActivationConfigProperty(
            name="connectionFactoryJndiName", value="JNDINameOfMDBSourceCF"
        ),
        @ActivationConfigProperty(
            name="initialContextFactory", value="weblogic.jndi.WLInitialContextFactory"
        )
    }
)
. . .

注意:

activationConfigのプロパティの名前が既存の記述子と競合する場合、記述子の優先度を使用して競合を解決します。記述子の優先度(高い順から低い順)は、weblogic-ejb-jar.xml、WebLogic Server 10.0アノテーション、ejb-jar.xmlactivationConfigのプロパティの順になります。たとえば、同じ記述子にweblogic-ejb-jar.xmlejb-jar.xmlがある場合、weblogic-ejb-jar.xmlの方が優先度が高いため、ejb-jar.xmlの値はオーバーライドされます。

表10-4は、WebLogic ServerでサポートされるactivationConfigの名前と値のペアをまとめたものです。