enkara enkara - 3 months ago 20
Java Question

Different value of JNDI parameter for different standalone instances in GlassFish

I need to have the same application deployed in GlassFish several times, with different JNDI parameters, but I can't find a way to do that.

I know I can have different standalone instances and apply the different JNDI resources to one or several instances, but I can't have the same resource name with different values for the different instances.
What is the way to achieve what I need?

Answer

So in the end what I did is to use this:

   @Resource(lookup = "java:app/AppName")
   private String appName;

   private String myJndiParameter;

And then since I can't use a variable to get the custom jndi parameter in an annotation, I created a @PostConstruct method, where I get the jndi value that I want, something like this:

   @PostConstruct
   public void initialize ()
   {
      try
      {
         myJndiParameter = (String) new javax.naming.InitialContext().lookup(appName + "/" + "my.jndi.parameter.name");
      } catch (NamingException e)
      {
         // Treat exception
      }
   }

In GlassFish, my custom resource name looks like:

myapp/my.jndi.parameter.name

I'm open to suggestions to improve it :)