4.6. 在应用程序中启用 Jakarta Enterprise Beans 和 MDB 属性替换
红帽 JBoss 企业应用平台允许您使用 @ActivationConfigProperty 和 @
Resource
注释在 Jakarta 企业 Bean 和 MDB 中启用属性替换。属性替换需要以下配置和代码更改:
- 您必须在 JBoss EAP 服务器配置文件中 启用属性替换。
- 您必须在 服务器配置文件中定义系统属性,并在启动 JBoss EAP 服务器时将它们作为参数传递。
- 您必须 修改应用程序代码 以使用替换变量。
以下示例演示了如何修改 JBoss EAP 附带的 helloworld-mdb
快速入门以使用属性替换。有关已完成的工作示例,请参见 helloworld-mdb-propertysubative 快速
入门。
4.6.1. 配置服务器以启用属性替换
要在 JBoss EAP 服务器中启用属性替换,您必须在服务器配置 的
属性设置为 ee
子系统中将 annotations-property-replacementtrue
。
备份服务器配置文件。
helloworld-mdb-propertysubative quick
start 示例需要单机服务器的完整配置集,因此这是EAP_HOME/standalone/configuration/standalone-full.xml
文件。如果您在受管域中运行服务器,这是EAP_HOME/domain/configuration/domain.xml
文件。导航到 JBoss EAP 安装目录,再使用 full 配置文件启动服务器。
$ EAP_HOME/bin/standalone.sh -c standalone-full.xml
注意对于 Windows Server,请使用
EAP_HOME\bin\standalone.bat
脚本。启动管理 CLI。
$ EAP_HOME/bin/jboss-cli.sh --connect
注意对于 Windows Server,请使用
EAP_HOME\bin\jboss-cli.bat
脚本。键入以下命令以启用注解属性替换:
/subsystem=ee:write-attribute(name=annotation-property-replacement,value=true)
您应看到以下结果:
{"outcome" => "success"}
检查对 JBoss EAP 服务器配置文件的更改。The
ee
子系统现在应包含以下 XML:示例
ee
子系统配置<subsystem xmlns="urn:jboss:domain:ee:4.0"> ... <annotation-property-replacement>true</annotation-property-replacement> ... </subsystem>
4.6.2. 定义系统属性
您可以在服务器配置文件中指定系统属性,也可以在启动 JBoss EAP 服务器时将它们作为命令行参数传递。服务器配置文件中定义的系统属性优先于启动服务器时在命令行中传递的属性。
4.6.2.1. 在服务器配置中定义系统属性
- 启动管理 CLI。
使用以下命令语法在 JBoss EAP 服务器中配置系统属性:
添加系统属性的语法
/system-property=PROPERTY_NAME:add(value=PROPERTY_VALUE)
为
helloworld-mdb-propertysubative 快速入门配置了以下系统属性:
添加系统属性的命令示例
/system-property=property.helloworldmdb.queue:add(value=java:/queue/HELLOWORLDMDBPropQueue) /system-property=property.helloworldmdb.topic:add(value=java:/topic/HELLOWORLDMDBPropTopic) /system-property=property.connection.factory:add(value=java:/ConnectionFactory)
检查对 JBoss EAP 服务器配置文件的更改。以下系统属性现在应出现在
<extensions> 后面
。系统属性配置示例
<system-properties> <property name="property.helloworldmdb.queue" value="java:/queue/HELLOWORLDMDBPropQueue"/> <property name="property.helloworldmdb.topic" value="java:/topic/HELLOWORLDMDBPropTopic"/> <property name="property.connection.factory" value="java:/ConnectionFactory"/> </system-properties>
4.6.2.2. 在服务器启动时将系统属性作为参数传递
如果您愿意,可以在您以 -DPROPERTY_NAME =PROPERTY_VALUE
的形式启动 JBoss EAP 服务器时在命令行中传递参数。以下是如何为上一节中定义的系统属性传递参数的示例。
服务器启动命令传递系统属性示例
$ EAP_HOME/bin/standalone.sh -c standalone-full.xml -Dproperty.helloworldmdb.queue=java:/queue/HELLOWORLDMDBPropQueue -Dproperty.helloworldmdb.topic=java:/topic/HELLOWORLDMDBPropTopic -Dproperty.connection.factory=java:/ConnectionFactory
4.6.3. 修改应用程序代码以使用系统属性替代
将硬编码的 @ActivationConfigProperty
和 @Resource
注释值替换为新定义的系统属性。以下是如何更改 helloworld-mdb
quickstart 以使用新定义的系统属性替换的示例:
更改
HelloWorldQueueMDB
类中的@ActivationConfigProperty
destination
属性值,以使用 system 属性替换。@MessageDriven
注释现在应如下所示:HelloWorldQueueMDB 代码示例
@MessageDriven(name = "HelloWorldQueueMDB", activationConfig = { @ActivationConfigProperty(propertyName = "destinationLookup", propertyValue = "${property.helloworldmdb.queue}"), @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"), @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge") })
更改
HelloWorldTopicMDB
类中的@ActivationConfigProperty
目标
属性值,以使用 system 属性替换。@MessageDriven
注释现在应如下所示:HelloWorldTopicMDB Code Example
@MessageDriven(name = "HelloWorldQTopicMDB", activationConfig = { @ActivationConfigProperty(propertyName = "destinationLookup", propertyValue = "${property.helloworldmdb.topic}"), @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"), @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge") })
更改
HelloWorldMDBServletClient
类中的@Resource
注释,以使用系统属性替换。现在,代码应该类似如下:HelloWorldMDBServletClient Code 示例
/** * Definition of the two Jakarta Messaging Service destinations used by the quickstart * (one queue and one topic). */ @JMSDestinationDefinitions( value = { @JMSDestinationDefinition( name = "java:/${property.helloworldmdb.queue}", interfaceName = "javax.jms.Queue", destinationName = "HelloWorldMDBQueue" ), @JMSDestinationDefinition( name = "java:/${property.helloworldmdb.topic}", interfaceName = "javax.jms.Topic", destinationName = "HelloWorldMDBTopic" ) }) /** * <p> * A simple servlet 3 as client that sends several messages to a queue or a topic. * </p> * * <p> * The servlet is registered and mapped to /HelloWorldMDBServletClient using the {@linkplain WebServlet * @HttpServlet}. * </p> * * @author Serge Pagop (spagop@redhat.com) * */ @WebServlet("/HelloWorldMDBServletClient") public class HelloWorldMDBServletClient extends HttpServlet { private static final long serialVersionUID = -8314035702649252239L; private static final int MSG_COUNT = 5; @Inject private JMSContext context; @Resource(lookup = "${property.helloworldmdb.queue}") private Queue queue; @Resource(lookup = "${property.helloworldmdb.topic}") private Topic topic; <!-- Remainder of code can be found in the `helloworld-mdb-propertysubstitution` quickstart. -->
修改
activemq-jms.xml
文件,以使用系统属性替换值。.activemq-jms.xml 文件示例
<?xml version="1.0" encoding="UTF-8"?> <messaging-deployment xmlns="urn:jboss:messaging-activemq-deployment:1.0"> <server> <jms-destinations> <jms-queue name="HELLOWORLDMDBQueue"> <entry name="${property.helloworldmdb.queue}"/> </jms-queue> <jms-topic name="HELLOWORLDMDBTopic"> <entry name="${property.helloworldmdb.topic}"/> </jms-topic> </jms-destinations> </server> </messaging-deployment>
-
部署应用。应用现在将系统属性指定的值用于
@Resource 和 @
ActivationConfigProperty
属性值。