Monday, June 20, 2011

Using API to get WebSphere Application Server Information–get WAS Applications information

Using WebSphere Application Server MBean to get the application status

private void getApplicationStatus() throws MalformedObjectNameException, NullPointerException, ConnectorException, AttributeNotFoundException, InstanceNotFoundException, MBeanException, ReflectionException{

String queryString="WebSphere:type=Application,*";
ObjectName queryAppObj=new ObjectName(queryString);
Set s=adminClient.queryNames(queryAppObj,null);
Iterator itApp=s.iterator();
while(itApp.hasNext()){
String appStr=itApp.next().toString();
int appNameidx=appStr.indexOf("name");
int appProcessidx=appStr.indexOf("process");
int startAppNameidx=appStr.indexOf("=",appNameidx);
int startAppProcessidx=appStr.indexOf("=",appProcessidx);
int endAppNameidx=appStr.indexOf(",",appNameidx);
int endAppProcessidx=appStr.indexOf(",",appProcessidx);
String appName=appStr.substring(startAppNameidx+1,endAppNameidx);
String processName=appStr.substring(startAppProcessidx+1,endAppProcessidx);

System.out.println(appName+"@"+processName+" is running");
}
}

However, the above procedure can only get the applications which are running on the server. If the application is stopped, this procedure will not get them.

The following procedure can get all applications deployed on the server. the procedure will get all WAS servers if there is a cluster in the environment.

private void  getApplicationList() throws Exception {

ArrayList appList=new ArrayList();
Hashtable props=new Hashtable();
AppManagement appM = AppManagementProxy.getJMXProxyForClient(adminClient);
ConfigService configService=new ConfigServiceProxy(adminClient);
Vector v=null;
Object[] appObj=null;
Session session=new Session();
ObjectName[] servers=configService.resolve(session,"Server");

for (ObjectName server:servers) {
String[] serverConfig=server.getKeyProperty("_Websphere_Config_Data_Id").split("/");
String serverName=server.getKeyProperty("_Websphere_Config_Data_Display_Name");
props.put(AppConstants.APPDEPL_LOCALE, Locale.getDefault());
String scope="WebSphere:cell="+serverConfig[1]+",node="+serverConfig[3]+",server="+serverName;
v=appM.listApplications(scope,props,session.getSessionId());
appObj=v.toArray();
for (Object app:appObj){
System.out.println(app.toString()+" is deployed "+serverName);
}
}
}

No comments:

Post a Comment