Hi,

Jay SenSharma

Jay SenSharma

Here is a Simple sample to get some very useful attributes of ClusterRuntimeMBean…like “AliveServerCount” and “MulticastMessagesLostCount” using ObjectName.

This is a very basic JMX Code to get the above mentioned informations of a Cluster…but the same program can be extended a little more to debug many useful informations of a Cluster which can help us to get the Runtime information of a Cluster along with the Configuration part of a Cluster. More details here:  http://download.oracle.com/docs/cd/E12840_01/wls/docs103/wlsmbeanref/mbeans/ClusterRuntimeMBean.html

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 ClusterRuntimeDetails
{
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 void printClusterInfo() 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");
server_states.put(serverName, serverstate);

ObjectName clusterRuntime=(ObjectName) connection.getAttribute(ser,"ClusterRuntime");
if (clusterRuntime !=null)
{
System.out.println("nt clusterRuntime.Name                       : "+(String) connection.getAttribute(clusterRuntime,"Name"));;
System.out.println("t clusterRuntime.MulticastMessagesLostCount : "+(Long) connection.getAttribute(clusterRuntime,"MulticastMessagesLostCount"));;
System.out.println("t clusterRuntime.CurrentMachine             : "+(ObjectName) connection.getAttribute(clusterRuntime,"CurrentMachine"));;
System.out.println("t clusterRuntime.AliveServerCount           : "+(Integer) connection.getAttribute(clusterRuntime,"AliveServerCount"));;
System.out.println("--------------------------------------------------------");
}
else
{
System.out.println("nt Not Part Of Any Cluster: "+serverName);
}
}
ObjectName domain1 = (ObjectName)connection.getAttribute(service,"DomainConfiguration");
ObjectName[] cluster_list = (ObjectName[])connection.getAttribute(domain1,"Clusters");
for(ObjectName cl:cluster_list)
{
System.out.println("######################## n Cluster Name              : " + (String) connection.getAttribute(cl,"Name"));
System.out.println("######################## n cl : " + cl);
ObjectName[] servers = (ObjectName[])connection.getAttribute(cl,"Servers");
for(ObjectName ser: servers)
{
String server_name = (String) connection.getAttribute(ser,"Name");
System.out.println("Server Name: " + server_name + ", Server State: " + server_states.get(server_name));
}
System.out.println("######################## n");
}
System.out.println();
}

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

OUTPUT:

C:DELETEBLOG_REPLYJMX_Multicastdetails>javac ClusterRuntimeDetails.java
Note: ClusterRuntimeDetails.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

C:DELETEBLOG_REPLYJMX_Multicastdetails>java ClusterRuntimeDetails

Not Part Of Any Cluster: AdminServer

clusterRuntime.Name                       : Cluster-A
clusterRuntime.MulticastMessagesLostCount : 0
clusterRuntime.CurrentMachine             : com.bea:Name=Machine-A,Location=MS1,Type=Machine
clusterRuntime.AliveServerCount           : 2
——————————————————–

clusterRuntime.Name                       : Cluster-A
clusterRuntime.MulticastMessagesLostCount : 2
clusterRuntime.CurrentMachine             : com.bea:Name=Machine-A,Location=MS2,Type=Machine
clusterRuntime.AliveServerCount           : 2
——————————————————–
########################
Cluster Name              : Cluster-A
########################
cl : com.bea:Name=Cluster-A,Location=7001_Domain,Type=Cluster
Server Name: MS2, Server State: RUNNING
Server Name: MS1, Server 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.