Getting instance name of a WebSphere app Server

I agree with specifying server name as an environment variable (Manglu's touch is also fine). Just to make the discussion complete, here is how you get get instance name via runtime (this API is deprecated in recent versions but still in use);

import com.ibm.websphere.runtime.ServerName;

System.out.println("Display name: " + ServerName.getDisplayName());
System.out.println("Full name: " + ServerName.getFullName());

Sample output would be like

Display name: server1
Full name: was7host01Node01Cell\was7host01Node01\server1


To keep it platform neutral you can set a variable as a JVM argument for the Websphere server (one for each node if its clustered). For Websphere 7, you will find the following in the Admin Console ...

Servers > Server Types > Websphere application servers > [your cluster node] >
  >  Java and Process Management  > Process Definition > Java Virtual Machine >
    > Generic JVM arguments 

and add a variable like this ...

-DServerName=serverNodeA

You can then access the value in your code as ...

String serverName = System.getproperty("ServerName");

This technique can be used with all application servers so long as you have access to add arguments to the JVM. I'm sure there must be Websphere specific API to query the node name, but then you're typing your code to the server which makes it difficult to unit test and is not portable. I prefer this approach.


An alternative, at least for WebSphere, is to look it up in the JNDI tree. This is what I use:

InitialContext ic = new javax.naming.InitialContext();
String serverName = ic.lookup("servername").toString();

This way I don't have to configure anything as WebSphere binds that information for me.

Cell and node name can also be retrieved using "thisNode/cell/cellname" and "thisNode/nodename". Something useful in clusters.

Tags:

Java

Websphere