Java 如何在JBoss7下为和MDB(EJB)定义自定义(实例)属性

Java 如何在JBoss7下为和MDB(EJB)定义自定义(实例)属性,java,jakarta-ee,jboss,ejb,message-driven-bean,Java,Jakarta Ee,Jboss,Ejb,Message Driven Bean,我想部署同一MDB的两个实例,用ActiveMQ处理jboss7下两个不同队列中的消息。因此,hier是我的ejb-jar.xml的一部分: <message-driven> <ejb-name>FirstInstanceOfMyMDB</ejb-name> <ejb-class>de.xx.xx.MyMDB</ejb-class> <activation-config> <ac

我想部署同一MDB的两个实例,用ActiveMQ处理jboss7下两个不同队列中的消息。因此,hier是我的ejb-jar.xml的一部分:

<message-driven>
    <ejb-name>FirstInstanceOfMyMDB</ejb-name>
    <ejb-class>de.xx.xx.MyMDB</ejb-class>
    <activation-config>
        <activation-config-property>
            <activation-config-property-name>destination</activation-config-property-name>
            <activation-config-property-value>activemq/queue/queue_1</activation-config-property-value>
        </activation-config-property>
        <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>SecondInstanceOfMyMDB</ejb-name>
    <ejb-class>de.xx.xx.MyMDB</ejb-class>
    <activation-config>
        <activation-config-property>
            <activation-config-property-name>destination</activation-config-property-name>
            <activation-config-property-value>activemq/queue/queue_2</activation-config-property-value>
        </activation-config-property>
        <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>
但是jboss似乎只将
System
设置为A或B一次。可能是因为相同的名称空间用于设置
System

所以我的问题是:设置自定义实例MDB(EJB)属性的最佳实践是什么

使用user1181247建议的方法:

@Resource(name="System")
private String System;
我可以在METH-INF目录中使用ejb-jar.xml在ejb模块中部署MDB,并根据需要完成您的工作。 尝试在war文件中使用WEB-INF文件夹中相同的ejb-jar.xml部署相同的类时,我会遇到以下异常:

[0m[31m09:13:56,823 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-2) MSC000001: Failed to start service jboss.deployment.unit."Server.war".INSTALL: org.jboss.msc.service.StartException in service jboss.deployment.unit."Server.war".INSTALL: JBAS018733: Failed to process phase INSTALL of deployment "Server.war"
    at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:127) [jboss-as-server-8.0.0.Alpha1-SNAPSHOT.jar:8.0.0.Alpha1-SNAPSHOT]
    ...
Caused by: java.lang.IllegalArgumentException: JBAS011053: Incompatible conflicting binding at java:comp/env/System source: org.jboss.as.ee.component.EnvEntryInjectionSource@1291e
如果两个实例的env entry值相同,则在没有异常的情况下完成部署


我是否需要为war文件进行另一个/附加配置?

我对MDB进行了几乎相同的注入,它曾在AS7中工作过(我目前正在使用EAP)。我能看到的唯一可能的区别是,我的资源和你的资源是一样的。因此,如果您还没有:

@Resource(name = "sysId")
protected String sysId;
更多细节…我已经在EAP上测试了以下内容,但之前在7.1.1上为我工作

我的mdb:

public class TestMDB implements MessageListener {
    private static final Logger l = Logger.getLogger(TestMDB.class);

    @Resource(name="sysId")
    private String sysId;

    public void onMessage(Message arg0) {
        try {
            l.info("Received message " + sysId + " - " + ((TextMessage) arg0).getText());
        }
        catch (JMSException e) {
            l.error("Failed to get message", e);
        }
    }

}
从我的ejb jar


接管人1
com.xxxx.test.JMSTest.TestMDB
目的型
javax.jms.Queue
目的地
队列/xxxin
系统ID
java.lang.String
ID1
接管人2
com.xxxx.test.JMSTest.TestMDB
目的型
javax.jms.Queue
目的地
队列/xxxinit
系统ID
java.lang.String
ID2
输出:

11:43:20082信息[com.xxxx.test.JMSTest.TestMDB](线程-2(HornetQ-client-global-threads-319126730))收到消息ID1-hi


11:43:25088信息[com.xxxx.test.JMSTest.TestMDB](Thread-2(HornetQ-client-global-threads-319126730))收到消息ID2-hi2

我在EJB规范中注意到这一点:

16.2.1环境条目的共享

对于封装在ejbjar中的企业bean,每个企业bean定义自己的一组环境条目。在这种情况下,企业bean的所有实例共享相同的环境条目;环境条目不与其他企业bean共享

在.war中,模块中的所有组件之间只共享一个命名环境。对于打包在.war中的企业bean,所有企业bean都共享这个单一的命名环境。企业bean与.war中的所有其他企业bean组件和web组件共享其环境条目


因此,出于某种原因,如果您部署的是ejb jar而不是war,那么env entry方法似乎可以工作。如果可能的话,可以将应用程序拆分并转移到ear部署。

谢谢您的回答。当MDB部署为jar(ejb模块)时,这种方法有效,但如果MDB部署在war文件中,则不起作用。我已经完成了我的问题。@sam-Hmmmmm…你有没有在更稳定的版本(如7.1.3)上尝试过这个?
public class TestMDB implements MessageListener {
    private static final Logger l = Logger.getLogger(TestMDB.class);

    @Resource(name="sysId")
    private String sysId;

    public void onMessage(Message arg0) {
        try {
            l.info("Received message " + sysId + " - " + ((TextMessage) arg0).getText());
        }
        catch (JMSException e) {
            l.error("Failed to get message", e);
        }
    }

}
    <enterprise-beans>
        <message-driven>
            <ejb-name>receiver1</ejb-name>
            <ejb-class>com.xxxx.test.JMSTest.TestMDB</ejb-class>
            <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>destination</activation-config-property-name>
                    <activation-config-property-value>queue/xxxin</activation-config-property-value>
                </activation-config-property>
            </activation-config>
            <env-entry>
                <env-entry-name>sysId</env-entry-name>
                <env-entry-type>java.lang.String</env-entry-type>
                <env-entry-value>ID1</env-entry-value>
            </env-entry>
        </message-driven>
        <message-driven>
            <ejb-name>receiver2</ejb-name>
            <ejb-class>com.xxxx.test.JMSTest.TestMDB</ejb-class>
            <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>destination</activation-config-property-name>
                    <activation-config-property-value>queue/xxxinit</activation-config-property-value>
                </activation-config-property>
            </activation-config>
            <env-entry>
                <env-entry-name>sysId</env-entry-name>
                <env-entry-type>java.lang.String</env-entry-type>
                <env-entry-value>ID2</env-entry-value>
            </env-entry>
        </message-driven>
    </enterprise-beans>