Hi,

Jay SenSharma

Jay SenSharma

Here is a Tiny code to display the WebLogic Cluster details…This code can be enhanced to do a lot more… once we get the Basic Informations of the Clusters available as part of our Domain.

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 ClusterDetails
{
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){
server_states.put((String) connection.getAttribute(ser,”Name”), (String) connection.getAttribute(ser,”State”));
}
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”));
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”;
ClusterDetails s = new ClusterDetails();
initConnection(hostname, portString, username, password);
s.printClusterInfo();
connector.close();
}
}
If you enjoyed this post, please considerleaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.