MDB注释的可​​配置值

时间:2008-11-21 08:31:40

标签: java annotations ejb-3.0 jboss-mdb

我正在尝试使用this method在我们的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 个答案:

答案 0 :(得分: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>

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

希望有所帮助。

答案 1 :(得分:2)

至少从JBoss AS 5.1开始,您可以使用AOP配置@ActivationConfigProperties。我通过查看jboss提供的示例here来发现这一点。如果您不希望您的用户名和密码可用于系统属性中的整个容器,或者如果您像我一样,并且从不重复,我希望部署一个带有用户名/密码的工件,这非常有用。不管怎么样,这里是一个...... ...

像这样注释mdb ......

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

然后将$ {whatever} -aop.xml添加到deploy dir,内部如下所示。我在那里留下了原始评论,以防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>