可以使用“创建企业 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,请执行以下操作: