2008-11-21 45 views
8

我试图在我们的EJB3应用程序中使用this method来接收邮件。总之,这意味着与以下注释创建一个MDB:对MDB注释的可​​配置值

@MessageDriven(activationConfig = { @ActivationConfigProperty(propertyName = "mailServer", propertyValue = "imap.company.com"), 
    @ActivationConfigProperty(propertyName = "mailFolder", propertyValue = "INBOX"), 
    @ActivationConfigProperty(propertyName = "storeProtocol", propertyValue = "imap"), 
    @ActivationConfigProperty(propertyName = "debug", propertyValue = "false"), 
    @ActivationConfigProperty(propertyName = "userName", propertyValue = "username"), 
    @ActivationConfigProperty(propertyName = "password", propertyValue = "pass") }) 
@ResourceAdapter("mail-ra.rar") 
@Name("mailMessageBean") 
public class MailMessageBean implements MailListener { 
    public void onMessage(final Message msg) { 
     ...snip... 
    } 
} 

我有这个工作,但情况不太理想:主机名,用户名和密码是硬编码。在编译之前使用ant和build.properties替换这些值的时候,我不知道如何将它们外化。

这将是使用MBean的理想选择,但我不知道如何从MBean获取值到MDB配置。

我该怎么做?

回答

13

可以外化注解到您在您的jar文件的META-INF部署ejb-jar.xml中,如下所示:

<?xml version="1.0" encoding="UTF-8"?> 

<ejb-jar version="3.0"> 
    <enterprise-beans> 
     <message-driven> 
      <ejb-name>YourMDB</ejb-name> 
      <ejb-class>MailMessageBean</ejb-class>   
      <activation-config> 
       <activation-config-property> 
        <activation-config-property-name>username</activation-config-property-name> 
        <activation-config-property-value>${mdb.user.name}</activation-config-property-value> 
       </activation-config-property> 
... 
... 
      </activation-config> 
     </message-driven> 
    </enterprise-beans> 

然后你可以设置mdb.user .name作为系统属性的值,作为使用-Dmdb.user.name = theUserName命令行到应用程序服务器的一部分,它会神奇地被mdb拾取。

希望有所帮助。

+1

对于JBoss,你还需要启用`<规格描述符属性替换>` – eis 2014-01-09 14:06:38

+1

对于GlassFish你在domain.xml文件中添加此或使用create-JVM选项的asadmin命令工具。 – 2014-05-12 08:28:49

2

从JBoss AS 5.1起,至少可以使用AOP来配置@ActivationConfigProperties。我通过查看jboss提供的示例发现了这个问题here。如果您不希望您的用户名和密码可用于系统属性中的整个容器,或者如果您像我一样,永远不会,我永远不会重复,想要在其中部署带有用户名/密码的工件,这非常有用。任何如何,这里是JIST ...

注释这样的MDB ...

... 
@MessageDriven 
@AspectDomain("TestMDBean") 
public class TestMDBean implements MessageListener { 
... 

然后,$ {}任何与-aop.xml内部添加到deploy目录像下面。我离开了原来的意见在里面,以防Jaikiran确实让提到的更改...

注:注释必须在只有一个 线。

<?xml version="1.0" encoding="UTF-8"?> 
<aop xmlns="urn:jboss:aop-beans:1.0"> 
    <!-- TODO: Jaikiran - These interceptor declarations need not be here since they 
    are already declared through the ejb3-interceptors-aop.xml. Duplicating them leads to 
    deployment errors. However, if this custom-ejb3-interceptors-aop.xml needs to be 
    independent, then we must find a better way of declaring these. Right now, commenting these 
    out, can be looked at later. --> 
    <!--  
    <interceptor class="org.jboss.ejb3.AllowedOperationsInterceptor" scope="PER_VM"/> 
    <interceptor class="org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor" scope="PER_VM"/> 
    <interceptor factory="org.jboss.ejb3.security.RunAsSecurityInterceptorFactory" scope="PER_CLASS"/> 
    <interceptor class="org.jboss.ejb3.stateless.StatelessInstanceInterceptor" scope="PER_VM"/> 

    <interceptor factory="org.jboss.ejb3.interceptor.EJB3InterceptorsFactory" scope="PER_CLASS_JOINPOINT"/> 
    <interceptor factory="org.jboss.aspects.tx.TxInterceptorFactory" scope="PER_CLASS_JOINPOINT"/> 
    --> 
    <domain name="TestMDBean" extends="Message Driven Bean" inheritBindings="true"> 
     <annotation expr="!class(@org.jboss.ejb3.annotation.DefaultActivationSpecs)"> 
     @org.jboss.ejb3.annotation.DefaultActivationSpecs (value={@javax.ejb.ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Queue"), @javax.ejb.ActivationConfigProperty(propertyName="destination", propertyValue="queue/MyQueue"), @javax.ejb.ActivationConfigProperty(propertyName="user", propertyValue="testusr"), @javax.ejb.ActivationConfigProperty(propertyName="password", propertyValue="testpwd")}) 
     </annotation> 
    </domain> 
</aop>