if (!task.equals("ClearAPIStats") && (!task.equals("StartWorker")) && (!task.equals("StopWorker")) && (host == null || host.equals(""))) {
logger.error("event=cns_manage_service error_code=missing_parameter_host");
throw new CMBException(CNSErrorCodes.MissingParameter,"Request parameter Host missing.");
}
AbstractDurablePersistence cassandraHandler = DurablePersistenceFactory.getInstance();
if (task.equals("ClearWorkerQueues")) {
List<CmbRow<String, String, String>> rows = cassandraHandler.readAllRows(AbstractDurablePersistence.CNS_KEYSPACE, CNS_WORKERS, 1000, 10, CMB_SERIALIZER.STRING_SERIALIZER, CMB_SERIALIZER.STRING_SERIALIZER, CMB_SERIALIZER.STRING_SERIALIZER);
List<CNSWorkerStats> statsList = new ArrayList<CNSWorkerStats>();
if (rows != null) {
for (CmbRow<String, String, String> row : rows) {
CNSWorkerStats stats = new CNSWorkerStats();
stats.setIpAddress(row.getKey());
if (row.getColumnSlice().getColumnByName("producerTimestamp") != null) {
stats.setProducerTimestamp(Long.parseLong(row.getColumnSlice().getColumnByName("producerTimestamp").getValue()));
}
if (row.getColumnSlice().getColumnByName("consumerTimestamp") != null) {
stats.setConsumerTimestamp(Long.parseLong(row.getColumnSlice().getColumnByName("consumerTimestamp").getValue()));
}
if (row.getColumnSlice().getColumnByName("jmxport") != null) {
stats.setJmxPort(Long.parseLong(row.getColumnSlice().getColumnByName("jmxport").getValue()));
}
if (row.getColumnSlice().getColumnByName("mode") != null) {
stats.setMode(row.getColumnSlice().getColumnByName("mode").getValue());
}
statsList.add(stats);
}
}
for (CNSWorkerStats stats : statsList) {
if (stats.getIpAddress().equals(host) && stats.getJmxPort() > 0) {
JMXConnector jmxConnector = null;
String url = null;
try {
long port = stats.getJmxPort();
url = "service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi";
JMXServiceURL serviceUrl = new JMXServiceURL(url);
jmxConnector = JMXConnectorFactory.connect(serviceUrl, null);
MBeanServerConnection mbeanConn = jmxConnector.getMBeanServerConnection();
ObjectName cnsWorkerMonitor = new ObjectName("com.comcast.cns.tools:type=CNSWorkerMonitorMBean");
CNSWorkerMonitorMBean mbeanProxy = JMX.newMBeanProxy(mbeanConn, cnsWorkerMonitor, CNSWorkerMonitorMBean.class, false);
mbeanProxy.clearWorkerQueues();
String res = CNSWorkerStatsPopulator.getGetManageWorkerResponse();
response.getWriter().println(res);
return true;
} finally {
if (jmxConnector != null) {
jmxConnector.close();
}
}
}
}
throw new CMBException(CMBErrorCodes.NotFound, "Cannot clear worker queues: Host " + host + " not found.");
} else if (task.equals("RemoveWorkerRecord")) {
cassandraHandler.delete(AbstractDurablePersistence.CNS_KEYSPACE, CNS_WORKERS, host, null, CMB_SERIALIZER.STRING_SERIALIZER, CMB_SERIALIZER.STRING_SERIALIZER);
String out = CNSPopulator.getResponseMetadata();
writeResponse(out, response);
return true;
} else if (task.equals("ClearAPIStats")) {
CMBControllerServlet.initStats();
String out = CNSPopulator.getResponseMetadata();
writeResponse(out, response);
return true;
} else if (task.equals("RemoveRecord")) {
cassandraHandler.delete(AbstractDurablePersistence.CNS_KEYSPACE, CNS_API_SERVERS, host, null, CMB_SERIALIZER.STRING_SERIALIZER, CMB_SERIALIZER.STRING_SERIALIZER);
String out = CNSPopulator.getResponseMetadata();
writeResponse(out, response);
return true;
} else if (task.equals("StartWorker")||task.equals("StopWorker")) {