Skip to content

Commit

Permalink
Change how mBeans names are generated to include the data source name…
Browse files Browse the repository at this point in the history
… in the attributes (if it has a name)

This allows you to query mbeans by stable name patterns, allowing for reliable instrumentation via JMX between VM instances.

I currently use these patches in production (nearly 18 months) to grab pool telemetry with collectd's JMX probe.
  • Loading branch information
Bryan Varner committed Oct 15, 2012
1 parent 3987bbf commit 0c8f43a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
<property>
<type>String</type>
<name>dataSourceName</name>
<bound/>
<default-value>null</default-value>
<getter><modifiers><modifier>public</modifier><modifier>synchronized</modifier></modifiers></getter>
<setter><modifiers><modifier>public</modifier><modifier>synchronized</modifier></modifiers></setter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,6 @@ public void attemptUnmanagePooledDataSource(PooledDataSource pds)
}

private String getPdsObjectNameStr(PooledDataSource pds)
{ return "com.mchange.v2.c3p0:type=PooledDataSource[" + pds.getIdentityToken() + "]"; }
{ return "com.mchange.v2.c3p0:type=PooledDataSource,identityToken=" + pds.getIdentityToken(); }
}

Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import javax.management.MBeanParameterInfo;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.ObjectInstance;
import javax.management.ReflectionException;
import javax.sql.ConnectionPoolDataSource;
import javax.sql.DataSource;
Expand Down Expand Up @@ -120,6 +121,7 @@ public class DynamicPooledDataSourceManagerMBean implements DynamicMBean

PooledDataSource pds;
String mbeanName;
String oldPdsName;
MBeanServer mbs;

ConnectionPoolDataSource cpds;
Expand All @@ -137,8 +139,14 @@ public void propertyChange(PropertyChangeEvent evt)
String propName = evt.getPropertyName();
Object val = evt.getNewValue();

if ("nestedDataSource".equals(propName) || "connectionPoolDataSource".equals(propName))
if ("nestedDataSource".equals(propName) || "connectionPoolDataSource".equals(propName)) {
reinitialize();
} else if ("dataSourceName".equals(propName)) {
if (evt.getOldValue() != null) {
oldPdsName = evt.getOldValue().toString();
}
reinitialize();
}
}
};

Expand All @@ -147,10 +155,11 @@ public DynamicPooledDataSourceManagerMBean(PooledDataSource pds, String mbeanNam
{
this.pds = pds;
this.mbeanName = mbeanName;
this.oldPdsName = pds.getDataSourceName();
this.mbs = mbs;

if (pds instanceof ComboPooledDataSource)
/* do nothing */;
((ComboPooledDataSource) pds).addPropertyChangeListener(pcl);
else if (pds instanceof AbstractPoolBackedDataSource)
((AbstractPoolBackedDataSource) pds).addPropertyChangeListener(pcl);
else
Expand Down Expand Up @@ -224,17 +233,18 @@ private synchronized Exception reinitialize()
// that the MBeanInfo is reread.
try
{
ObjectName oname = ObjectName.getInstance( mbeanName );
ObjectName oname = ObjectName.getInstance( mbeanName + ",name=" + oldPdsName );
if (mbs.isRegistered( oname ))
{
mbs.unregisterMBean( oname );
if (logger.isLoggable(MLevel.FINER))
logger.log(MLevel.FINER, "MBean: " + mbeanName + " unregistered, in order to be reregistered after update.");
logger.log(MLevel.FINER, "MBean: " + oname.toString() + " unregistered, in order to be reregistered after update.");
}
oname = ObjectName.getInstance(mbeanName + ",name=" + pds.getDataSourceName());
mbs.registerMBean( this, oname );
if (logger.isLoggable(MLevel.FINER))
logger.log(MLevel.FINER, "MBean: " + mbeanName + " registered.");
logger.log(MLevel.FINER, "MBean: " + oname.toString() + " registered.");
return null;
}
catch (Exception e)
Expand Down

0 comments on commit 0c8f43a

Please sign in to comment.