在 GlassFish 4.0 中部署期间修改 ejb-jar.xml 的配置属性
Modify configuration properties of ejb-jar.xml during deployment in GlassFish 4.0
我有一个 ejb-jar.xml,其中包含我的一个 MDB 的配置信息。
里面有一个配置:
<activation-config-property>
<activation-config-property-name>addressList</activation-config-property-name>
<activation-config-property-value>mq://test.server.uk:7676</activation-config-property-value>
</activation-config-property>
当我的项目构建和打包然后分发给用户时,我需要能够确保可以修改此值,因为用户具有不同的服务器地址。
目前我可以选择在属性文件中设置地址。无论如何,我可以在 glassfish 4.0 上部署期间使用 属性 值修改此 xml 吗?
如果不是,每次有人需要应用程序时我都必须设置值并重新构建它吗?
我愿意将配置放在我只需要动态配置的地方,以便用户可以在属性文件中设置服务器地址。
您可以尝试的一件事是使用 @AroundConstruct
拦截器在运行时在 MDB 上设置值。值得注意的是,虽然可以在您的 ejb-jar.xml 中使用占位符,但它主要依赖于容器,而且明显缺乏阅读 material 对 Glassfish 的具体处理方式应该是一个来源为你担心。让我们试试这个:
在您的 MDB 上定义一个拦截器:
@MessageDriven
@Interceptors(AddressListInterceptor.class)
public class YourMDB
定义拦截器
public class AddressListInterceptor {
@AroundConstruct
private void begin(InvocationContext iCtxt) {
/**load your property prior to this point */
ActivationConfigProperty addressList = new ActivationConfigProperty{
public String propertyName(){
return "addressList";
}
public String propertyValue(){
return theAddressList;
}
public Class<? extends Annotation> annotationType(){
return ActivationConfigProperty.class;
}
};
try {
/**get the annotations, with the intention of adding yours (addressList) to the array using the method demonstrated in
*/
Annotations[] annotations = iCtxt.getClass().getAnnotations();
iCtxt.proceed(); //this will allow processing to continue as normal
} catch (Exception ex) {
}
}
除了不幸的需要自己扫描和修改注释之外,这种方法为您带来的是允许您进入 MDB 的生命周期并修改注释的值,就在 bean 被删除之前实例化。当 bean 投入使用时,它将采用您设置的值,一切都应该井井有条
我在 glassfish 4.0 中找到了修改地址列表的简单方法。此解决方案允许仍然使用 @ActivationConfigProperty 的其余部分。对我来说,当用户使用安装脚本进行安装时,我可以 运行 以下命令:
asadmin server.jms-service.type = REMOTE
asadmin set configs.config.server-config.jms-service.jms-host.default_JMS_host.host=
"testserver.test.te.uk"
asadmin restart-domain
您将默认 JMS 主机设置为键入 REMOTE,然后告诉代理使用默认 JMS 主机中定义的地址。
然后使用 asadmin set 命令设置主机地址。
完成后,您需要重新启动 glassfish。
这显然依赖于 glassfish 容器,但这就是我所需要的。
我有一个 ejb-jar.xml,其中包含我的一个 MDB 的配置信息。 里面有一个配置:
<activation-config-property>
<activation-config-property-name>addressList</activation-config-property-name>
<activation-config-property-value>mq://test.server.uk:7676</activation-config-property-value>
</activation-config-property>
当我的项目构建和打包然后分发给用户时,我需要能够确保可以修改此值,因为用户具有不同的服务器地址。
目前我可以选择在属性文件中设置地址。无论如何,我可以在 glassfish 4.0 上部署期间使用 属性 值修改此 xml 吗?
如果不是,每次有人需要应用程序时我都必须设置值并重新构建它吗?
我愿意将配置放在我只需要动态配置的地方,以便用户可以在属性文件中设置服务器地址。
您可以尝试的一件事是使用 @AroundConstruct
拦截器在运行时在 MDB 上设置值。值得注意的是,虽然可以在您的 ejb-jar.xml 中使用占位符,但它主要依赖于容器,而且明显缺乏阅读 material 对 Glassfish 的具体处理方式应该是一个来源为你担心。让我们试试这个:
在您的 MDB 上定义一个拦截器:
@MessageDriven @Interceptors(AddressListInterceptor.class) public class YourMDB
定义拦截器
public class AddressListInterceptor { @AroundConstruct private void begin(InvocationContext iCtxt) { /**load your property prior to this point */ ActivationConfigProperty addressList = new ActivationConfigProperty{ public String propertyName(){ return "addressList"; } public String propertyValue(){ return theAddressList; } public Class<? extends Annotation> annotationType(){ return ActivationConfigProperty.class; } }; try { /**get the annotations, with the intention of adding yours (addressList) to the array using the method demonstrated in */ Annotations[] annotations = iCtxt.getClass().getAnnotations(); iCtxt.proceed(); //this will allow processing to continue as normal } catch (Exception ex) { } }
除了不幸的需要自己扫描和修改注释之外,这种方法为您带来的是允许您进入 MDB 的生命周期并修改注释的值,就在 bean 被删除之前实例化。当 bean 投入使用时,它将采用您设置的值,一切都应该井井有条
我在 glassfish 4.0 中找到了修改地址列表的简单方法。此解决方案允许仍然使用 @ActivationConfigProperty 的其余部分。对我来说,当用户使用安装脚本进行安装时,我可以 运行 以下命令:
asadmin server.jms-service.type = REMOTE
asadmin set configs.config.server-config.jms-service.jms-host.default_JMS_host.host=
"testserver.test.te.uk"
asadmin restart-domain
您将默认 JMS 主机设置为键入 REMOTE,然后告诉代理使用默认 JMS 主机中定义的地址。
然后使用 asadmin set 命令设置主机地址。
完成后,您需要重新启动 glassfish。
这显然依赖于 glassfish 容器,但这就是我所需要的。