可配置的值到MDB注释

10

我正在尝试在我们的EJB3应用程序中使用这种方法来接收邮件。简而言之,这意味着创建一个带有以下注解的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配置中。

我应该怎么做?

2个回答

14

您可以将注释外部化到ejb-jar.xml文件中,然后在您的jar文件的META-INF目录中进行部署,具体如下:

<?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>

你可以通过在应用程序服务器的命令行中使用-Dmdb.user.name=theUserName将mdb.user.name值设置为系统属性的方式,这样它就会神奇地被mdb接收到。

希望这能有所帮助。


1
对于JBoss,您还需要启用<spec-descriptor-property-replacement> - eis
1
对于GlassFish,您可以在domain.xml文件中添加此内容,或使用asadmin工具中的create-jvm-options命令。 - Martin Charlesworth
这个机制不在JEE标准中。 - weberjn

3
至少从 JBoss AS 5.1 开始,您可以使用 AOP 来配置 @ActivationConfigProperties。我通过查看 jboss 提供的示例 here 发现了这一点。如果您不想在系统属性中将用户名和密码可用于整个容器,或者像我一样永远不想部署带有用户名/密码的构件,则此功能非常有用。总之,以下是要点...

像这样注释 mdb...

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

然后在部署目录中添加一个${whatever}-aop.xml文件,其中包含以下内部信息。我保留了原始注释,以防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>

你尝试过使用JBoss MailListener而不是MessageListener吗? - Fenris_uy
我们如何在Spring中实现它? - PAA

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