Hi,

Jay SenSharma

Jay SenSharma

Here is another demonstration of using Java Management Extension API (JMX)….

As the earlier version of the same program was not able to display the state informations of the Servers which are down … so i modified the ServerState Program a little bit to get the State informations of those Servers as well which are down.

So the current page contains the Modified version of the program mentioned in :

Getting All Server State Info in 9.x JMX Style

import weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean;
import weblogic.management.runtime.ServerRuntimeMBean;
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 DomainConfigurationMbeanTest {

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();       
	for(ObjectName temp : arr)
		System.out.println("nt servers: "+temp);

	ObjectName domain = (ObjectName) connection.getAttribute(service,"DomainConfiguration");
	System.out.println("nt Domain: " + domain.toString());
	ObjectName[] servers = (ObjectName[]) connection.getAttribute(domain,"Servers");
	for (ObjectName server : servers)
              {
                String serverState="UNKNOWN";
		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("nt Server: "+aName+"t State: "+serverState);
		    }
		catch(Exception e)
		    {
			System.out.println("nt Server: "+aName+"t State: SHUTDOWN (or) In State : "+ serverState);
		    }
              }
    }

  public static void main(String[] args) throws Exception 
   {
	String hostname = args[0];
	String portString = args[1];
	String username = args[2];
	String password = args[3];
	DomainConfigurationMbeanTest s = new DomainConfigurationMbeanTest();
	initConnection(hostname, portString, username, password);
	s.printNameAndState();
	connector.close();
   }
 }
Output …will be like following:
Getting ServerState using DomainConfigurationMBean Test JMX

Getting ServerState using DomainConfigurationMBean Test JMX

.

.

Thanks

Jay SenSharma

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