Skip to content

Commit

Permalink
Merge pull request #663 from hector-client/bean-cleanup
Browse files Browse the repository at this point in the history
Unregister beans when connection manager is shutdown
  • Loading branch information
zznate committed May 29, 2014
2 parents c20b5da + 8def42c commit 658fc14
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,9 @@ public void shutdown() {
nodeAutoDiscoverService.shutdown();
if ( hostTimeoutTracker != null )
hostTimeoutTracker.shutdown();


JmxMonitor.getInstance().removeCassandraMonitor(this);

for (HClientPool pool : hostPools.values()) {
try {
pool.shutdown();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,8 @@
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;

import javax.management.*;
import me.prettyprint.cassandra.connection.HConnectionManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -61,6 +53,31 @@ public void registerMonitor(String name, String monitorType, Object monitoringIn

mbs.registerMBean(monitoringInterface, oName);
}

public void unregisterMonitor(String name, String monitorType)
throws MalformedObjectNameException, InstanceAlreadyExistsException,
MBeanRegistrationException, NotCompliantMBeanException {

String monitorName = generateMonitorName(name, monitorType);
log.info("Unregistering JMX {}", monitorName);

ObjectName oName = new ObjectName(monitorName);

// Check if the monitor is already registered
if (!mbs.isRegistered(oName)) {
log.info("Monitor is not registered: {}", oName);
return;
}

try
{
mbs.unregisterMBean(oName);
}
catch(InstanceNotFoundException e)
{
log.warn("Failed to unregister monitor: {}" + oName.toString(), e);
}
}

private String generateMonitorName(String className, String monitorType) {
StringBuilder sb = new StringBuilder();
Expand Down Expand Up @@ -132,4 +149,29 @@ public CassandraClientMonitor getCassandraMonitor(HConnectionManager connectionM
return cassandraClientMonitor;
}

public void removeCassandraMonitor(HConnectionManager connectionManager) {
CassandraClientMonitor cassandraClientMonitor = monitors.remove(connectionManager.getClusterName());
if(cassandraClientMonitor != null) {
try
{
unregisterMonitor("me.prettyprint.cassandra.service_"+connectionManager.getClusterName(), "hector");
}
catch(MalformedObjectNameException e)
{
log.error("Unable to unregister JMX monitor", e);
}
catch(InstanceAlreadyExistsException e)
{
log.error("Unable to unregister JMX monitor", e);
}
catch(MBeanRegistrationException e)
{
log.error("Unable to unregister JMX monitor", e);
}
catch(NotCompliantMBeanException e)
{
log.error("Unable to unregister JMX monitor", e);
}
}
}
}

0 comments on commit 658fc14

Please sign in to comment.