MDB 注释的可配置值

2024-01-07

我正在尝试使用这个方法 http://www.jboss.org/community/docs/DOC-10032用于在我们的 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 配置。

我该怎么做?


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

希望有帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

MDB 注释的可配置值 的相关文章

随机推荐