将Same Mdb java类关联到Ejb3 jboss 5.x服务器中的多个队列

时间:2012-03-24 11:53:49

标签: java-ee jms jboss5.x

 package jboss5.ejb3.mdb;

import java.util.HashMap;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.ejb.MessageDriven;
import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.naming.InitialContext;

@MessageDriven(
    activationConfig = {
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
    @ActivationConfigProperty(propertyName="destination", propertyValue="queue/Queue1"),
    }
    )

@TransactionManagement(TransactionManagementType.CONTAINER)
@EJB(name = "EJB3MessageDrivenLocal1", beanInterface = EJB3MessageDrivenLocal.class)

 public class EJB3MessageDrivenBean implements MessageDrivenBean, MessageListener
 {
ObjectMessage msg = null;
private MessageDrivenContext mdc = null;

public EJB3MessageDrivenBean() {}

public void setMessageDrivenContext(MessageDrivenContext mdc) {
    this.mdc = mdc;
}

public void onMessage(Message inMessage) {
        }

@Override
public void ejbRemove() throws EJBException {
    // TODO Auto-generated method stub
}
 }

上面的代码是我的消息驱动bean,适用于单个队列' Queue1'。现在我想将Queue2与同一个mdb相关联。 我可以通过创建另一个mdb类来做同样的事情" EJB3MessageDrivenBean2"并且只需更改activationConfig属性,如下所示。

    @MessageDriven(
    activationConfig = {
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
    @ActivationConfigProperty(propertyName="destination", propertyValue="queue/Queue2"),
    }
    )

但是在这个方法中我必须复制我的代码,并且将来如果我必须关联另一个队列Queue3,那么我必须再次复制java文件。

通过更改xml文件或任何其他方法的任何其他方法。

更多细节

让我们说队列1中的消息是JmsMsg11,JmsMsg12,JmsMsg13,JmsMsg14 让我们说队列2中的消息是JmsMsg21,JmsMsg22,JmsMsg23,JmsMsg24

我必须同时处理JmsMsg11和JmsMsg21。在处理JmsMsg11之后,我必须处理JmsMsg12然后处理JmsMsg13然后处理JmsMsg14。同样适用于queue2。

我通过以下代码在ejb-jar.xml和weblogic-ejb-jar.xml中进行了一些更改,在weblogic 10.x中做了同样的事情。不知道怎么做jboss 5.x. ejb-jar.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.0" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd">
    <enterprise-beans>
    <message-driven>
      <ejb-name>WeblogicEjb3MDB1</ejb-name>
      <ejb-class>jboss5.ejb3.mdb.EJB3MessageDrivenBean</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-property>
        <activation-config-property-name>
            DestinationJndiName
        </activation-config-property-name>
        <activation-config-property-value>
           Queue1
        </activation-config-property-value>
    </activation-config-property>
    <activation-config-property>
        <activation-config-property-name>
            ConnectionFactoryJndiName
        </activation-config-property-name>
        <activation-config-property-value>
            QFact1
        </activation-config-property-value>
    </activation-config-property>
   </activation-config>
 </message-driven>
     <message-driven>
       <ejb-name>WeblogicEjb3MDB2</ejb-name>
       <ejb-class>jboss5.ejb3.mdb.EJB3MessageDrivenBean</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-property>
        <activation-config-property-name>
            DestinationJndiName
        </activation-config-property-name>
        <activation-config-property-value>
           Queue2
        </activation-config-property-value>
    </activation-config-property>
    <activation-config-property>
        <activation-config-property-name>
            ConnectionFactoryJndiName
        </activation-config-property-name> 
        <activation-config-property-value>
            QFact2
        </activation-config-property-value>
    </activation-config-property>
     </activation-config>
   </message-driven>
 </enterprise-beans>

   <assembly-descriptor>
        <container-transaction>
          <method>
             <ejb-name>WeblogicEjb3MDB1</ejb-name>
             <method-name>onMessage</method-name>
         </method>
  <trans-attribute>NotSupported</trans-attribute>
</container-transaction>

<container-transaction>
  <method>
    <ejb-name>WeblogicEjb3MDB2</ejb-name>
    <method-name>onMessage</method-name>
  </method>
  <trans-attribute>NotSupported</trans-attribute>
</container-transaction>

weblogic-ejb-jar.xml

    <!DOCTYPE weblogic-ejb-jar PUBLIC '-//BEA Systems, Inc.//DTD WebLogic 8.1.0 EJB//EN' 'http://www.bea.com/servers/wls810/dtd/weblogic-ejb-jar.dtd'>


   <!-- Generated XML! -->

   <weblogic-ejb-jar>
    <weblogic-enterprise-bean>
      <ejb-name>WeblogicEjb3MDB1</ejb-name>
        <message-driven-descriptor>
         <pool>
        <max-beans-in-free-pool>1</max-beans-in-free-pool>
         </pool>

       <destination-jndi-name>Queue1</destination-jndi-name>
    </message-driven-descriptor>

      <transaction-descriptor>
    <trans-timeout-seconds>600</trans-timeout-seconds>
     </transaction-descriptor>

  </weblogic-enterprise-bean>

  <weblogic-enterprise-bean>
     <ejb-name>WeblogicEjb3MDB2</ejb-name>
       <message-driven-descriptor>
         <pool>
        <max-beans-in-free-pool>1</max-beans-in-free-pool>
         </pool>
       <destination-jndi-name>Queue2</destination-jndi-name>
      </message-driven-descriptor>

     <transaction-descriptor>
    <trans-timeout-seconds>600</trans-timeout-seconds>
     </transaction-descriptor>

  </weblogic-enterprise-bean>
 </weblogic-ejb-jar>

0 个答案:

没有答案