Hi,

Jay SenSharma

Jay SenSharma

Based on the query of one of our Magic User  Mr. Vibhor Rastogi (http://middlewaremagic.com/weblogic/?p=2110#comment-4287) We are developing this article.   It was asked that how we can get the Server’s actual lifecycle state using JMX Code when the server is not in Running state. Earlier we developed a JMX Code “Getting ServerState (Enhanced Version) for WLS9.x & Above” but that code was able to just fetch the Servers state which already started listening on it’s Listen Port so it was easy to get it’s State using ServerRuntimeMBean.

But in the current demonstration we will see how to fetch the Server’s different lifecycle states like “Starting”, “Stand By”, “Admin” , “Resuming”, “Running” etc while the Server is coming up and the Listen port is not actually opened so far.

.
Just use the following JMX Code and compile and run it after running the “$WLS_HOME/server/bin/setWLSEnv.sh” or “setWLSEnv.cmd” script so that the CLASSPATH and PATH will contain the required set of Jars and executables.
ServerLifecycleState.java

import weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean;
import weblogic.management.runtime.*;
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.jmx.MBeanServerInvocationHandler;
import java.util.Hashtable;
import java.io.IOException;
import java.net.MalformedURLException;
import weblogic.management.runtime.JDBCDataSourceRuntimeMBean;
import javax.management.*;
import javax.naming.*;

public class ServerLifecycleState {

private static MBeanServerConnection connection;
private static JMXConnector connector;
private static final ObjectName service;
private static String combea = "com.bea:Name=";
private static String service1 = "DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean";
private static String service2 = "RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean";

   static
      {
        try {
               service =new ObjectName(combea + service1);
            } 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 void printNameAndState() throws Exception
    {
	ObjectName arr[]=getServerRuntimes();
	ObjectName domain = (ObjectName) connection.getAttribute(service,"DomainConfiguration");
	System.out.println("\t Domain: " + domain.toString());
	ObjectName[] servers = (ObjectName[]) connection.getAttribute(domain,"Servers");
    String serverState="UNKNOWN";
	for (ObjectName server : servers)
              {
		        String aName = (String) connection.getAttribute(server,"Name");
       		    try{
 			         ObjectName ser= new ObjectName("com.bea:Name="+aName+",Location="+aName+",Type=ServerRuntime");
			         serverState=(String) connection.getAttribute(ser,"State");
			         System.out.println("\n\t Server: "+aName+"\t State: "+serverState );
		           }
		        catch(Exception e)
		           {
					 DomainRuntimeServiceMBean domainRuntimeService = (DomainRuntimeServiceMBean) MBeanServerInvocationHandler.newProxyInstance(connection, new ObjectName(DomainRuntimeServiceMBean.OBJECT_NAME));
					 DomainRuntimeMBean domainRuntime = domainRuntimeService.getDomainRuntime();
					 ServerLifeCycleRuntimeMBean serverLifeCycleRuntimeMBean = domainRuntime.lookupServerLifeCycleRuntime(aName);
			         System.out.println("\t Server: "+aName+"\t In LifeCycleState : "+ serverLifeCycleRuntimeMBean.getState());
		           }
              }
      System.out.println("\t -------------------------- data/time:\t"+new java.util.Date()+" ----------------------------");
    }

  public static void main(String[] args) throws Exception
   {
	String hostname = args[0];
	String portString = args[1];
	String username = args[2];
	String password = args[3];
	ServerLifecycleState s = new ServerLifecycleState();
	initConnection(hostname, portString, username, password);
	for(int i=0;i<1000;i++)
	   {
	       s.printNameAndState();
		   Thread.sleep(500);
	   }
	connector.close();
   }
 }

Compile and run the following program and for testing in parallel start any of your Managed Server.
you will see following kind of output Notice the Managed Server LifeCycle state is changing:

  -------------------------- data/time:  Mon Jun 20 00:16:36 IST 2011 ----------------------------
  Domain: com.bea:Name=9001_domain,Location=9001_domain,Type=Domain

  Server: AdminServer     State: RUNNING
  Server: MS1     In LifeCycleState : SHUTDOWN
  -------------------------- data/time:  Mon Jun 20 00:16:37 IST 2011 ----------------------------
  Domain: com.bea:Name=9001_domain,Location=9001_domain,Type=Domain

  Server: AdminServer     State: RUNNING
  Server: MS1     In LifeCycleState : STARTING
  -------------------------- data/time:  Mon Jun 20 00:16:38 IST 2011 ----------------------------
  Domain: com.bea:Name=9001_domain,Location=9001_domain,Type=Domain

  Server: AdminServer     State: RUNNING
  Server: MS1     In LifeCycleState : STARTING
  -------------------------- data/time:  Mon Jun 20 00:16:38 IST 2011 ----------------------------
  Domain: com.bea:Name=9001_domain,Location=9001_domain,Type=Domain

  Server: AdminServer     State: RUNNING
  Server: MS1     In LifeCycleState : STARTING
  -------------------------- data/time:  Mon Jun 20 00:16:39 IST 2011 ----------------------------
  Domain: com.bea:Name=9001_domain,Location=9001_domain,Type=Domain

  Server: AdminServer     State: RUNNING
  Server: MS1     In LifeCycleState : RESUMING
  -------------------------- data/time:  Mon Jun 20 00:16:39 IST 2011 ----------------------------
  Domain: com.bea:Name=9001_domain,Location=9001_domain,Type=Domain

  Server: AdminServer     State: RUNNING

  Server: MS1     State: RUNNING
Server State Using ServerLifeCycleRuntimeMBean

Server State Using ServerLifeCycleRuntimeMBean

.
.
Thanks
Jay SenSharma