Hi,

Jay SenSharma

Jay SenSharma

An application represents a J2EE Enterprise application packaged in an EAR file or EAR exploded directory. The EAR file or directory contains a set of components such as WAR, EJB, and RAR connector components, each of which can be deployed on one or more targets. A target is a server or a cluster.

ApplicationRuntimeMBean encapsulates runtime information about a deployed Enterprise application.

Here is a Sample JMX code to display the State of all the Applications deployed on All the Servers…

import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.IntrospectionException;
import javax.management.MBeanException;
import javax.management.ReflectionException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Hashtable;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context;
import weblogic.management.runtime.*;
import weblogic.management.runtime.*;
public class ApplicationDetails
{
private static MBeanServerConnection connection;
private static JMXConnector connector;
private static final ObjectName service;

static
{
try
{
service = new ObjectName("com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean");
}
catch (MalformedObjectNameException e)
{
throw new AssertionError(e.getMessage());
}
}

public static void initConnection(String hostname, String portString,String username, String password) throws IOException,MalformedURLException
{
String protocol = "t3";
Integer portInteger = Integer.valueOf(portString);
int port = portInteger.intValue();
String jndiroot = "/jndi/";
String mserver = "weblogic.management.mbeanservers.domainruntime";
JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname,port, jndiroot + mserver);
Hashtable h = new Hashtable();
h.put(Context.SECURITY_PRINCIPAL, username);
h.put(Context.SECURITY_CREDENTIALS, password);
h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,"weblogic.management.remote");
connector = JMXConnectorFactory.connect(serviceURL, h);
connection = connector.getMBeanServerConnection();
}

public static ObjectName[] getServerRuntimes() throws Exception
{
return (ObjectName[]) connection.getAttribute(service,"ServerRuntimes");
}

public Object getObjectName(ObjectName objectName, String attributeName) throws AttributeNotFoundException,InstanceNotFoundException, MBeanException, ReflectionException, IOException
{
return connection.getAttribute(objectName, attributeName);
}

public void printClusterInfo() throws Exception
{
ObjectName[] serverRT = getServerRuntimes();
Hashtable server_states = new Hashtable();
for(ObjectName ser: serverRT)
{
System.out.println("--------------------------------------------------------------");
String serverName=(String) connection.getAttribute(ser,"Name");
System.out.print("\tSERVER NAME: "+serverName);
System.out.println("\t SERVER STATE: "+(String) connection.getAttribute(ser,"State"));
ObjectName[] appRT =(ObjectName[])connection.getAttribute(ser,"ApplicationRuntimes");
//ApplicationRuntimeMBean appRun=(ApplicationRuntimeMBean) connection.getAttribute(ser,"ApplicationRuntimes");
System.out.println("\n\tTotal Applications Targeted On # "+serverName+" # : "+appRT.length);
for(ObjectName app: appRT)
{
String appName = (String) connection.getAttribute(app,"Name");
//weblogic.health.HealthState healthState = (weblogic.health.HealthState) connection.getAttribute(app,"HealthState");
//System.out.println("\t Health ="+healthState.getState());
ObjectName[] componentRT =(ObjectName[])connection.getAttribute(app,"ComponentRuntimes");
for(ObjectName compRT:componentRT)
{
Integer stateInt=(Integer)connection.getAttribute(compRT,"DeploymentState");
String name=(String)connection.getAttribute(compRT,"Name");
String stateString=""+stateInt;
if (stateInt == ComponentRuntimeMBean.ACTIVATED)
stateString="ACTIVATED ";
if (stateInt == ComponentRuntimeMBean.NEW)
stateString="NEW";
if (stateInt == ComponentRuntimeMBean.PREPARED)
stateString="PREPARED ";
if (stateInt == ComponentRuntimeMBean.UNPREPARED)
stateString="UNPREPARED ";
System.out.println("\n\tSTATE: "+stateString+"      AppName: "+name);
}
}
}
}

public static void main(String[] args) throws Exception
{
String hostname = "localhost";
String portString = "7001";
String username = "weblogic";
String password = "weblogic";
ApplicationDetails s = new ApplicationDetails();
initConnection(hostname, portString, username, password);
s.printClusterInfo();
connector.close();
}
}
Above Program gives the Following Output:
F:\DELETE\BLOG_REPLIES\ApplicationChecker_JMX>java ApplicationDetails
————————————————————–
SERVER NAME: AdminServer         SERVER STATE: RUNNING
Total Applications Targeted On # AdminServer # : 7
STATE: PREPARED        AppName: helloWorldEar
STATE: ACTIVATED       AppName: bea_wls_management_internal2
STATE: ACTIVATED       AppName: consoleapp
STATE: ACTIVATED       AppName: consoleapp
STATE: ACTIVATED       AppName: bea_wls_internal
STATE: ACTIVATED       AppName: bea_wls_deployment_internal
STATE: ACTIVATED       AppName: mejb
STATE: ACTIVATED       AppName: bea_wls9_async_response
————————————————————–
SERVER NAME: MS1         SERVER STATE: RUNNING
Total Applications Targeted On # MS1 # : 5
STATE: ACTIVATED       AppName: bea_wls_internal
STATE: ACTIVATED       AppName: bea_wls9_async_response
STATE: ACTIVATED       AppName: bea_wls_deployment_internal
STATE: ACTIVATED       AppName: bea_wls_cluster_internal
—————–
Thanks
Jay SenSharma