Getting module and application name in Java EE

When running an Java EE application, in different situations it’s useful to know it’s own module and application name. In order to get these it is possible to access them via JNDI.

Why to access these names?

Each Java  container provides a number of different JNDI-Resources that can be accessed within an application (.ear). So the application name is provided as well as the module name. In case the name of the application was changed during deployment or the name of a module was changed in the application.xml, being able to get the name at runtime is really blessing. Accessing the application name is possible by using java:app/AppName, whereas accessing module name is possible by using java:module/ModuleName.
Furthermore for logging or debugging purpose having access to these might be exactly what you need. Accessing these names at runtime results in no questions whether there was an invalid configuration of the application or not.

Accessing the names

The following code example demonstrates accessing the names with Dependency Injection.

In case there is no Dependency Injection it is possible to access the values by directly using InitialContext.

Sources

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.