Accessing WebSphere Variables from inside your J2EE application isn’t as easy as it looks. They’re not environment variables or system properties, but a portion of the WebSphere configuration that is used by the WebSphere Application Server Runtime.
You can access these variables by invoking an operation on “AdminOperations” MBean as follows:
try
{
AdminService adminService = AdminServiceFactory.getAdminService();
ObjectName queryName = new ObjectName( "WebSphere:*,type=AdminOperations" );
Set objs = adminService.queryNames( queryName, null );
if ( !objs.isEmpty() )
{
ObjectName thisObj = (ObjectName)objs.iterator().next();
String opName = "expandVariable";
String signature[] = { "java.lang.String" };
String params[] = { "${VARIABLE_NAME}" } ;
Object retVal = adminService.invoke( thisObj, opName, params, signature );
System.out.println( retVal );
}
} catch (MalformedObjectNameException e) {
e.printStackTrace();
} catch (InstanceNotFoundException e) {
e.printStackTrace();
} catch (MBeanException e) {
e.printStackTrace();
} catch (ReflectionException e) {
e.printStackTrace();
}
This code will only work from an application running inside the server.
4 comments ↓
You know I was just thinking the same thing the other day…
An easier method is that you can bind them to a JVM variable through the administration console (server -> process definition -> jvm -> properties). Then on startup they will be automatically populated and then you can access them as jvm variables.
This can a be pretty handy feature.
Will there be any change in accessing env var when application is deployed in cluster environment?
Becoz i tried accessing this way but its returning the variable name itself not its value!
Cool! this works. The example in IBM website doesnt. Awesome!!
Leave a Comment