Modify configuration properties of ejb-jar.xml during deployment in GlassFish 4.0

1.5k Views Asked by At

I have an ejb-jar.xml that contains configuration information for one of my MDB. In there is a configuration of:

 <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>

As my project is built and packaged and then distributed off to users I need to be able to make sure this value can be modified as users have different server addresses.

Currently I have the option to set the address in a properties file. Is there anyway that I could modify this xml during deployment on glassfish 4.0 with the property value?

If not am I going to have to set the value every time someone wants the application and re-build it?

I am open to putting the configuration else where I just need to have it dynamic so that users can set the server addresses in a properties file.

2

There are 2 best solutions below

1
On BEST ANSWER

I found a simple way of modifying the address list in glassfish 4.0. This solution allows the rest of your @ActivationConfigProperty to still be used. For me when a user uses a setup script for the installation I can run the following commands:

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

You set the default JMS host to type REMOTE this then tells the broker to use the address defined in the default JMS host.

You then set the host address with the asadmin set command.

Once that is done you need to restart your glassfish.

This is obviously glassfish container dependant but that is all I required.

8
On

One thing you can try is to use an @AroundConstruct interceptor to set the value on the MDB at runtime. It's worthwhile to note that while it is possible to use placeholders in your ejb-jar.xml, it's primarily container-dependent, and the apparent lack of reading material on how it's done for Glassfish specifically should be a source of worry for you. Let's try this:

  1. Define an interceptor on your MDB:

    @MessageDriven
    @Interceptors(AddressListInterceptor.class)
    public class YourMDB
    
  2. Define your interceptor

    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) {
    
               } 
       }
    

Apart from the unfortunate need to scan and modify the annotations yourself, what this approach buys you is that you're allowed to step into the lifecycle of the MDB and modify the value of the annotation, just before the bean is intantiated. By the time the bean is put into service, it'll take the value you've set and everything should be in order