Pages

Monday, March 5, 2012

Getting application and module name on an application server thread


Often times you are in situations where you need a unique key for the application and module name from the Thread context classloader. At this point developers resort to hacks like using the Classloader.toString method.

A more suitable approach would be to use the unified portable JNDI name feature in EE6.
Starting WebSphere Application Server 8 developers can do this in their app code


 private String getApplicationName() {


     String uniqueAppName;
          try {
         String moduleName  = (String) new InitialContext().lookup("java:module/ModuleName");
         String applicationName = (String) new InitialContext().lookup("java:app/AppName");
         uniqueAppName = applicationName+":" + moduleName;
          } catch (NamingException e) {
              throw new RuntimeException(e);
          }
       
          return uniqueAppName;
      }


see http://javahowto.blogspot.com/2009/12/how-to-get-module-name-and-app-name.html

Please note this approach will work on ALL Java EE containers

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.