java MDB 注释的可配置值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/308188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Configurable values to MDB annotations
提问by Sietse
I'm trying to use this methodfor receiving mail in our EJB3 app. In short, that means creating an MDB with the following annotations:
我正在尝试使用此方法在我们的 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...
}
}
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.
我有这个工作,但情况不太理想:主机名、用户名和密码是硬编码的。在编译之前没有使用 ant 和 build.properties 来替换这些值,我不知道如何将它们具体化。
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.
最好使用 MBean,但我不知道如何将值从 MBean 获取到 MDB 配置。
How should I do this?
我该怎么做?
回答by Brett Hannah
You can externalise the annotations into the ejb-jar.xml that you deploy in the META-INF of your jar file as follows:
您可以将注释外部化到您在 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>
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.
然后,您可以使用 -Dmdb.user.name=theUserName 将 mdb.user.name 值设置为系统属性,作为应用程序服务器命令行的一部分,它会神奇地被 mdb 接收。
Hope that helps.
希望有帮助。
回答by Joseph Valerio
As of JBoss AS 5.1 at least, you can use AOP to configure the @ActivationConfigProperties. I discovered this by looking at the examples that jboss provides here. This is useful if you do not want your username and passwords available to the entire container in a systems property, or if you are like me and never, I repeat NEVER, want to deploy an artifact with a username/password in it. Any how, here is the jist...
至少从 JBoss AS 5.1 开始,您可以使用 AOP 来配置 @ActivationConfigProperties。我通过查看 jboss在此处提供的示例发现了这一点。如果您不希望系统属性中的整个容器都可以使用您的用户名和密码,或者如果您像我一样并且从不,我再说一遍,从不希望部署包含用户名/密码的工件,这将非常有用。不管怎样,这里是 jist...
Annotate the mdb like this...
像这样注释mdb...
...
@MessageDriven
@AspectDomain("TestMDBean")
public class TestMDBean implements MessageListener {
...
Then add a ${whatever}-aop.xml to the deploy dir with internals like below. I left the original comments in there in case Jaikiran does make the changes mentioned...
然后将 ${whatever}-aop.xml 添加到部署目录,内部结构如下所示。我在那里留下了原始评论,以防 Jaikiran 确实进行了提到的更改......
Note: the annotation must be on one line only.
注意:注释只能在一行上。
<?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>

