6

我有一个 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 吗?

如果不是,我每次有人想要应用程序并重新构建它时都必须设置值吗?

我愿意将配置放在我只需要动态的地方,以便用户可以在属性文件中设置服务器地址。

4

2 回答 2

3

您可以尝试的一件事是使用@AroundConstruct拦截器在运行时设置 MDB 上的值。值得注意的是,虽然可以在您的 ejb-jar.xml 中使用占位符,但它主要依赖于容器,并且明显缺乏有关如何为 Glassfish 完成它的阅读材料,这应该是您担心的根源。让我们试试这个:

  1. 在您的 MDB 上定义一个拦截器:

    @MessageDriven
    @Interceptors(AddressListInterceptor.class)
    public class YourMDB
    
  2. 定义你的拦截器

    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 
                      http://stackoverflow.com/a/14276270/1530938  */
                   Annotations[] annotations = iCtxt.getClass().getAnnotations(); 
    
                    iCtxt.proceed(); //this will allow processing to continue as normal
               } catch (Exception ex) {
    
               } 
       }
    

除了不幸需要自己扫描和修改注解之外,这种方法给您带来的好处是,您可以在 bean 实例化之前进入 MDB 的生命周期并修改注解的值。当 bean 投入使用时,它将采用您设置的值,并且一切都应该井井有条

于 2016-02-28T00:30:49.563 回答
0

我找到了一种在 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 容器,但这就是我所需要的。

于 2016-03-03T17:07:18.817 回答