如何在JBoss 7下为MDB(EJB)定义自定义(实例)属性

4

我希望在jboss7和ActiveMQ下部署两个相同的MDB实例,以处理来自两个不同队列的消息。因此,这是我的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>

这个配置可以正常工作。

现在我想为每个实例添加一些特定的属性:System = A 用于 FirstInstanceOfMyMDBSystem = B 用于 SecondInstanceOfMyMDB

我已经尝试使用 <env-entry><message-driven> 中注入带有 @Resource 注释的 System

<message-driven>
        <ejb-name>FirstInstanceOfMyMDB</ejb-name>
        ...
<env-entry>
          <env-entry-name>System</env-entry-name>
          <env-entry-type>java.lang.String</env-entry-type>
          <env-entry-value>A</env-entry-value>
    </env-entry>
</message-driven>

<message-driven>
        <ejb-name>SecondInstanceOfMyMDB</ejb-name>
        ...
    <env-entry>
          <env-entry-name>System</env-entry-name>
          <env-entry-type>java.lang.String</env-entry-type>
          <env-entry-value>B</env-entry-value>
    </env-entry>
</message-driven>

但是,JBoss似乎只将System设置为A或B。可能是因为同一命名空间用于设置System

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

使用用户1181247建议的方法:

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

我可以在ejbmodule中使用ejb-jar.xml将我的MDBs部署到METH-INF目录中,并且它们可以按预期工作。 尝试将相同的类与相同的ejb-jar.xml部署在war文件中,放置在WEB-INF文件夹中时,我遇到了以下异常:

[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-value 对于两个实例来说是相同的,则可以无异常地部署!您是否需要为war文件进行另外/附加的配置?
2个回答

0

我在我的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中返回
    <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>

输出:

11:43:20,082 信息 [com.xxxx.test.JMSTest.TestMDB] (Thread-2 (HornetQ-client-global-threads-319126730)) 收到消息 ID1 - hi

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


这种方法适用于将MDB部署为jar(ejb模块),但如果将其部署在war文件中,则不适用。我已经完成了我的问题。 - sam
@sam 嗯嗯嗯……你试过在更稳定的版本,比如7.1.3上运行吗? - Marc

0

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

16.2.1 环境条目的共享

对于打包在ejb-jar中的企业Bean,每个企业Bean定义自己的环境条目集。在这种情况下,所有企业Bean实例共享相同的环境条目;环境条目不与其他企业Bean共享。

在.war中,所有组件之间共享单个命名环境。对于打包在.war中的企业Bean,所有企业Bean共享此单个命名环境。企业Bean将其环境条目与.war中的所有其他企业Bean组件和Web组件共享。

因此,似乎如果您部署ejb jar而不是war,则env-entry方法将起作用。如果可能的话,可以拆分应用程序并移动到ear部署。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接