您可以利用「建立 Enterprise Bean」精靈來建立 EJB 2.0 或 EJB 2.1 訊息驅動 Bean。
EJB 2.0 引進訊息驅動 Bean 來支援處理取自 Java 傳訊服務 (JMS) 的非同步訊息。EJB 2.1 規格擴充了訊息驅動 Bean 的定義,使它能夠支援任何傳訊系統,而不只是 JMS。 因此,EJB 2.1 規格也改變了用來定義訊息驅動 Bean 的部署描述子元素。建立 EJB 2.1 訊息驅動 Bean 的精靈也包括其他頁面,供您選擇性定義您的非 JMS 傳訊系統及其啟動配置元素。
下列程式碼範例說明用來定義訊息驅動 Bean 的 EJB 2.0 和 EJB 2.1 部署描述子元素之間的差異。
<message-driven id="Mdb20"> <ejb-name>Mdb</ejb-name> <ejb-class>ejbs.MdbBean</ejb-class> <transaction-type>Bean</transaction-type> <message-selector>mdbMessage</message-selector> <acknowledge-mode>Auto-acknowledge</acknowledge-mode> <message-driven-destination> <destination-type>javax.jms.Topic</destination-type> <subscription-durability>Durable</subscription-durability> </message-driven-destination> </message-driven>
<message-driven id="Mdb21"> <ejb-name>Foo</ejb-name> <ejb-class>ejbs.FooBean</ejb-class> <messaging-type>javax.jms.MessageListener</messaging-type> <transaction-type>Bean/transaction-type> <message-destination-type>javax.jms.Topic</message-destination-type> <activation-config> <activation-config-property> <activation-config-property-name>destinationType</activation-config-property-name> <activation-config-property-value>javax.jms.Topic</activation-config-property-value> </activation-config-property> <activation-config-property> <activation-config-property-name>subscriptionDurability</activation-config-property-name> <activation-config-property-value>Durable</activation-config-property-value> </activation-config-property> <activation-config-property> <activation-config-property-name>acknowledgeMode</activation-config-property-name> <activation-config-property-value>AutoAcknowledge</activation-config-property-value> </activation-config-property> <activation-config-property> <activation-config-property-name>messageSelector</activation-config-property-name> <activation-config-property-value>fooSelector</activation-config-property-value> </activation-config-property> </activation-config> </transaction-type> </ejb-name> </message-driven>
在 EJB 2.1 中,規格定義了用來定義確認模式、訊息選取器、訂閱延續性和目的地類型等內容的 <activation-config-property> 元素。當您使用非 JMS 傳訊類型時,精靈可讓您定義 Bean 所需使用的 <activation-config-property> 名稱/值配對。 如果是 JMS 傳訊系統,精靈會提供 JMS 所需要的預設配置內容,如 subscriptionDurability 和 acknowledgeMode。
如果要建立 EJB 2.0 或 EJB 2.1 訊息驅動 Bean,請執行下列動作: