【问题标题】:Configurable values to MDB annotationsMDB 注释的可配置值
【发布时间】:2010-09-23 10:02:47
【问题描述】:

我正在尝试使用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 配置。

我应该怎么做?

【问题讨论】:

    标签: java annotations ejb-3.0 jboss-mdb


    【解决方案1】:

    至少从 JBoss AS 5.1 开始,您可以使用 AOP 来配置 @ActivationConfigProperties。我通过查看 jboss 提供的示例here 发现了这一点。如果您不希望系统属性中的整个容器都可以使用您的用户名和密码,或者如果您像我一样,并且从不(我重复一遍,从不)想要部署其中包含用户名/密码的工件,这很有用。无论如何,这是jist...

    像这样注释 mdb...

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

    然后将 ${whatever}-aop.xml 添加到具有如下内部结构的部署目录。我把原来的 cmets 留在了那里,以防 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 吗?
    • 我们如何在 Spring 中做到这一点?
    【解决方案2】:

    您可以将注释外部化到您在 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 拾取。

    希望对您有所帮助。

    【讨论】:

    • 对于JBoss,还需要开启&lt;spec-descriptor-property-replacement&gt;
    • 对于 glassfish,您可以将其添加到 domain.xml 文件中或使用 asadmin 工具中的 create-jvm-options 命令。
    • JEE 标准中没有这种机制。
    猜你喜欢
    • 2023-03-04
    • 2012-05-10
    • 2012-08-07
    • 1970-01-01
    • 1970-01-01
    • 2012-10-08
    • 1970-01-01
    • 2020-10-14
    • 1970-01-01
    相关资源
    最近更新 更多