jakarta-ee - 在 GlassFish 4.0 中部署期间修改 ejb-jar.xml 的配置属性

标签 jakarta-ee glassfish ejb ejb-jar.xml

我有一个 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 中使用占位符,但它主要依赖于容器,并且明显缺乏有关如何为 Glassfish 完成此操作的阅读 Material ,这应该是您担心的根源。让我们试试这个:

  • 在您的 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 
                      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 投入使用时,它将采用您设置的值,并且一切都应该井井有条

    关于jakarta-ee - 在 GlassFish 4.0 中部署期间修改 ejb-jar.xml 的配置属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35063954/

    相关文章:

    java - 在 servlet 中抛出自定义异常

    java - 将数据从 Servlet 发布到 Rest webservice

    json - Tomcat servlet 对 json 对象表现得很奇怪

    java - 什么是 org.glassfish.webservices.monitoring.WebServiceTesterServlet?

    java - JDBC 连接池监控 GlassFish

    eclipse - 如何将JAR库添加到WAR项目而不面临java.lang.ClassNotFoundException?类路径 vs 构建路径 vs/WEB-INF/lib

    java - Glassfish 3.1.2.2。 linux环境build 5 ear应用部署问题

    java - 干扰器还是 JMS?

    java - 如何让这个定时器服务类处理 "change in scheduled time"?

    java - 如何触发@Timeout注解?