gpt4 book ai didi

java - MDB 注释的可配置值

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:36:37 25 4
gpt4 key购买 nike

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

我应该怎么做?

最佳答案

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

希望对您有所帮助。

关于java - MDB 注释的可配置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/308188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com