2013-11-28 59 views
3

我想部署同一個MDB的兩個實例,以使用ActiveMQ處理來自jboss7下兩個不同隊列的消息。所以,票數是我的ejb-jar.xml中的一部分:如何在JBoss 7下定義custome(實例)屬性和MDB(EJB)

<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 = AFirstInstanceOfMyMDBSystem = BSecondInstanceOfMyMDB

我已經嘗試中的使用,以獲得注入@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)Proerties的最佳實踐是什麼?

使用user1181247提出的計算策略:

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

我可以根據需要在METH-INF目錄ejb-jar.xml和你的工作的ejbModule內部署我的MDB。 Tryint在與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: [email protected] 

如果將env-entry-值是這兩種情況下是相同的,部署在沒有例外的情況下完成!

我需要另一個/額外配置的戰爭文件?

回答

0

我已經做了幾乎相同的注射與我一起工作的AS7(我目前正在使用EAP)的MDBs。唯一可能的區別是我可以看到與我的工作有什麼不同,我已經命名了我的資源。所以,試試這個,如果你還沒有準備好:

@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:20082 INFO [com.xxxx.test.JMSTest.TestMDB] (Thread-2(HornetQ-client-global-threads-319126730))收到消息ID1-嗨

11:43:25,088 IN FO [com.xxxx.test.JMSTest。TestMDB(線程2(HornetQ的客戶端全局線程-319126730))收到的報文ID2 - HI2

+0

謝謝你的回答。 – sam

+0

當MDB部署爲jar(ejb-module)時,此方法可用,但如果部署在war文件中,則不會。我完成了我的問題。 – sam

+0

@sam嗯mmmm ...你有沒有試過這個更像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而不是戰爭,env-entry方法似乎會工作。如果可能的話,可能會將應用程序拆分並移至耳朵部署。