Configuring an MDB to listen to multiple queues
I'm using EJB 3.1 and I want to configure an MDB to listen to multiple 开发者_JAVA百科queues.
I'd prefer defining the queue names via XML but the other definitions via annotations. Can this be done?Once instantiated, an MDB can only listen to the resource specified in their destination ActivationConfigProperty, however you can create multiple instances of the same MDB with different destinations (queues, in your case).
Create two entries in your ejb-jar.xml with different destination and ejb-name properties, but the same ejb-class.
use ejb-jar.xml instead of ibm-ejb-jar-bnd.xml
<message-driven>
<ejb-name>MessageDrivenBean1</ejb-name>
<ejb-class>com.sample.MessageDrivenBean</ejb-class>
<messaging-type>javax.jms.MessageListener</messaging-type>
<transaction-type>Container</transaction-type>
<activation-config>
<activation-config-property>
<activation-config-property-name>destinationType</activation-config-property-name>
<activation-config-property-value>javax.jms.Queue</activation-config-property-value>
</activation-config-property>
</activation-config>
</message-driven>
<message-driven>
<ejb-name>MessageDrivenBean2</ejb-name>
<ejb-class>com.sample.MessageDrivenBean</ejb-class>
<messaging-type>javax.jms.MessageListener</messaging-type>
<transaction-type>Container</transaction-type>
<activation-config>
<activation-config-property>
<activation-config-property-name>destinationType</activation-config-property-name>
<activation-config-property-value>javax.jms.Queue</activation-config-property-value>
</activation-config-property>
</activation-config>
</message-driven>
</enterprise-beans>
And remove @MessageDriven annotation from your Java class
'@MessageDriven(activationConfig = { @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})'
精彩评论