MDB 注释的可配置值 [英] Configurable values to MDB annotations

查看:31
本文介绍了MDB 注释的可配置值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用此方法在我们的 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")公共类 MailMessageBean 实现 MailListener {public void onMessage(最终消息消息){...剪...}}

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

最好使用 MBean,但我不知道如何将值从 MBean 获取到 MDB 配置.

我该怎么做?

解决方案

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

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

希望有所帮助.

I'm trying to use this method for receiving mail in our EJB3 app. In short, that means creating an MDB with the following annotations:

@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...
    }
}

I have this working, but the situation is less than ideal: The hostname, username and password are hardcoded. Short of using ant and build.properties to replace those values before compilation, I don't know how to externalize them.

It would be ideal to use an MBean, but I have no idea how to get the values from the MBean to the MDB configuration.

How should I do this?

解决方案

You can externalise the annotations into the ejb-jar.xml that you deploy in the META-INF of your jar file as follows:

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

Then you can set the mdb.user.name value as a system property as part of the command line to your application server using -Dmdb.user.name=theUserName and it will magically get picked up by the mdb.

Hope that helps.

这篇关于MDB 注释的可配置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆