Jay SenSharma

Jay SenSharma

Here is a Simple Example which we can use to get the WebLogic DataSource property Details using JMX.

import java.io.IOException;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;

import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.IntrospectionException;
import javax.management.MBeanException;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context;

import weblogic.jndi.Environment;
import weblogic.management.MBeanHome;
import weblogic.management.WebLogicMBean;
import weblogic.management.configuration.ComponentMBean;
import weblogic.management.configuration.JDBCConnectionPoolMBean;
import weblogic.management.configuration.JDBCTxDataSourceMBean;
public class DataSourceProperties_JMX
{
static MBeanServerConnection connection;
static JMXConnector connector;
static ObjectName service;
public static void main(String [] args)
{
try
{
String protocol = “t3”;
int port = 7001;
String hostname = “localhost”;
String jndiroot = “/jndi/”;
String mserver = “weblogic.management.mbeanservers.domainruntime”;
JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname, port,
jndiroot + mserver);
service = new ObjectName(“com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean”);
Hashtable h = new Hashtable();
h.put(Context.SECURITY_PRINCIPAL, “weblogic”);
h.put(Context.SECURITY_CREDENTIALS, “weblogic”);
h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,”weblogic.management.remote”);
connector = JMXConnectorFactory.connect(serviceURL, h);
connection = connector.getMBeanServerConnection();
new DataSourceProperties_JMX().getJdbcRuntime();
}
catch(Exception e)
{
e.printStackTrace();
}
}

private ObjectName getJDBCDriverParamsBean(ObjectName jdbcSystemResourceMBean) throws AttributeNotFoundException, InstanceNotFoundException, MBeanException, ReflectionException, IOException, IntrospectionException
{
ObjectName jdbcDataSourceBean = (ObjectName) getObjectName(jdbcSystemResourceMBean, “JDBCResource”);
return (ObjectName) getObjectName(jdbcDataSourceBean, “JDBCDriverParams”);
}

private Object getObjectName(ObjectName objectName, String attributeName) throws AttributeNotFoundException,InstanceNotFoundException, MBeanException, ReflectionException, IOException
{
return connection.getAttribute(objectName, attributeName);
}

public void getJdbcRuntime() throws Exception
{
ObjectName[] serverRT = (ObjectName[])connection.getAttribute(service, “ServerRuntimes”);
int length = (int) serverRT.length;
for (int i = 0; i < length; i++)
{
String name = (String) connection.getAttribute(serverRT[i],”Name”);
ObjectName[] appRT = (ObjectName[]) connection.getAttribute(new ObjectName(“com.bea:Name=”+name+”,ServerRuntime=”+name+”,Location=”+name+”,Type=JDBCServiceRuntime”),”JDBCDataSourceRuntimeMBeans”);
int appLength = (int) appRT.length;
for (int x = 0; x < appLength; x++)
{
System.out.println(“n……..           .<“+name+”   : JDBCDataSourceRuntimeMBeans>” + (String)connection.getAttribute(appRT[x], “Name”)+”……….”);
System.out.println(“ActiveConnectionsCurrentCount : ” + connection.getAttribute(appRT[x], “ActiveConnectionsCurrentCount”));
System.out.println(“ActiveConnectionsAverageCount : ” + connection.getAttribute(appRT[x], “ActiveConnectionsAverageCount”));
System.out.println(“ActiveConnectionsAverageCount : ” + connection.getAttribute(appRT[x], “ActiveConnectionsAverageCount”));
System.out.println(“ConnectionsTotalCount         : ” + connection.getAttribute(appRT[x], “ConnectionsTotalCount”));
System.out.println(“CurrCapacity                    : ” + connection.getAttribute(appRT[x], “CurrCapacity”));
System.out.println(“CurrCapacityHighCount         : ” + connection.getAttribute(appRT[x], “CurrCapacityHighCount”));
System.out.println(“HighestNumAvailable           : ” + connection.getAttribute(appRT[x], “HighestNumAvailable”));
System.out.println(“HighestNumAvailable           : ” + connection.getAttribute(appRT[x], “HighestNumAvailable”));
System.out.println(“LeakedConnectionCount         : ” + connection.getAttribute(appRT[x], “LeakedConnectionCount”));
System.out.println(“WaitSecondsHighCount          : ” + connection.getAttribute(appRT[x], “WaitSecondsHighCount”));
System.out.println(“WaitingForConnectionCurrentCount: ” + connection.getAttribute(appRT[x], “WaitingForConnectionCurrentCount”));
System.out.println(“WaitingForConnectionFailureTotal: ” + connection.getAttribute(appRT[x], “WaitingForConnectionFailureTotal”));
System.out.println(“WaitingForConnectionTotal     : ” + connection.getAttribute(appRT[x], “WaitingForConnectionTotal”));
System.out.println(“WaitingForConnectionHighCount : ” + connection.getAttribute(appRT[x], “WaitingForConnectionHighCount”));
System.out.println(“…………………………………………………………………..n”);
String poolName = (String)connection.getAttribute(appRT[x], “Name”);          ObjectName jdbcSystemResource = new ObjectName(“com.bea:Name=” + poolName+ “,Type=JDBCSystemResource”);
ObjectName jdbcDataSourceBean = (ObjectName) getObjectName(jdbcSystemResource, “JDBCResource”);
ObjectName jdbcDriverParams=(ObjectName) getObjectName(jdbcDataSourceBean, “JDBCDriverParams”);
ObjectName jdbcProperties=(ObjectName) getObjectName(jdbcDriverParams, “Properties”);
ObjectName[] jdbcPropertyBeans = (ObjectName[]) getObjectName(jdbcProperties, “Properties”);
for (int j = 0; j < jdbcPropertyBeans.length; j++)
{
ObjectName jdbcPropertyBean = null;
jdbcPropertyBean = jdbcPropertyBeans[j];
String jdbcPropertyName = (String) getObjectName(jdbcPropertyBean, “Name”);
String jdbcPropertyValue = (String) getObjectName(jdbcPropertyBean, “Value”);
System.out.println(“nt===== j “+j+”t”+jdbcPropertyName+”t”+jdbcPropertyValue);
}
}
}
}
}

Regards

JaySenSharma

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