java - 如何在 JBoss 7 下为 MDB (EJB) 定义客户(实例)属性

标签 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>
        <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

我已经尝试在 System 中使用 @Resource 注释:

<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) 属性的最佳做法是什么?

使用 user1181247 建议的方法:

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

我可以在 METH-INF 目录中使用 ejb-jar.xml 的 ejb 模块中部署我的 MDB,并根据需要进行工作。 尝试在 WEB-INF 文件夹中使用相同的 ejb-jar.xml 在 war 文件中部署相同的类我得到以下异常:

[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 文件进行其他/额外的配置?

最佳答案

我已经用我的 MDB 完成了几乎相同的注入(inject),它一次与 AS7 一起工作(我目前正在使用 EAP)。我能看到的与我的资源和你拥有的资源之间唯一可能的区别是我命名了我的资源。所以如果你还没有尝试这个:

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

更多详细信息...我已经在 EAP 上测试了以下内容,但之前在 7.1.1 上为我工作过。

我的主数据库:

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);
        }
    }

}

来 self 的 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 INFO [com.xxxx.test.JMSTest.TestMDB] (Thread-2 (HornetQ-client-global-threads-319126730)) 收到消息 ID1 - hi

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

关于java - 如何在 JBoss 7 下为 MDB (EJB) 定义客户(实例)属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20268119/

相关文章:

Java "for"循环填充 TextView

java - 每次我想运行或不运行时都创建线程的新实例

eclipse - 64 位 Windows 7 上的 32 位 Java,用于在 Eclipse Juno 中运行 JBoss JSP 编辑器

Java webapp 部署 : explode or not to explode?

鼠标退出时的 Java jpopupmenu

java - 嵌套 Hashmap 覆盖其他键

java - 调用 Post-Method 会导致 "405 Method not allowed"- 错误

java - 从 war/ear 外部访问多个应用程序服务器的属性文件

java - "Java programming language type"的定义是什么

java - 在 JBoss 中公开 RMI 服务器?