Hi,

Jay SenSharma

Jay SenSharma

Here is a Simple deonstration on getting the Server State information using the help of ServerRuntimeMBean.  In the below link you will find that How can we get the Server State Information using WLS8.1 Style of JMX coding with the help of MBeanHome ( serverstate-in-wls8-1-jmx-style/ ) . MBeanHome class is deprecated. So In the Current Page we have the Latest version of JMX coding api which is able to fetch the Runtime Information of all the Servers…

Below program u can use for Any WebLogic Version which is WLS9.x or Higher.

NOTE: The Current JMX Code is able to display the State information of the Servers which are in RUNNING state Only….So if you want to see the Enhanced version of this Code then Please refer to:

Enhanced Version of the same Code (Getting ServerState (Enhanced Version) for WLS9.x & Above)

Step1).Create a Directory some where in your File System ..

Example: C:Basic-Jmx

Step2).open a Note pad or textEditor and write the following program “GetServerState.java

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;
public class GetServerState
{
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<String,String> h = new Hashtable<String,String>();
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 void printServerState() throws Exception {
ObjectName[] serverRT = getServerRuntimes();
Hashtable server_states = new Hashtable();
for(ObjectName ser: serverRT)
{
String serverName=(String) connection.getAttribute(ser,"Name");
String serverState=(String) connection.getAttribute(ser,"State");
System.out.println("nt Server: "+serverName+"t State: "+serverState);
}
}

public static void main(String[] args) throws Exception
{
String hostname = "localhost";
String portString = "7001";
String username = "weblogic";
String password = "weblogic";
GetServerState s = new GetServerState();
initConnection(hostname, portString, username, password);
s.printServerState();
connector.close();
}
}

Step3). Open a Command prompt and then run “. ./setWLSEnv.sh” or “setWLSEnv.cmd”

Step4). Move inside the directory where u have placed the above program in the same command window.. and then compile and Run the program..

C:Basic-Jmx> javac  GetServerState.java

C:Basic-Jmx> java GetServerState

OUTPUT:

Server: AdminServer     State: RUNNING

Server: MS1                        State: RUNNING

Server: MS2                        State: RUNNING

Server: MS3                        State: RUNNING

.

.

Thanks

Jay SenSharma

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.