in collector/jmx/src/main/java/org/apache/karaf/decanter/collector/jmx/JmxCollector.java [114:201]
public void run() {
LOGGER.debug("Karaf Decanter JMX Collector starts harvesting {}...", this.type);
JMXConnector connector = null;
MBeanServerConnection connection = null;
String host = null;
if (this.url == null || this.url.equalsIgnoreCase("local")) {
LOGGER.debug("Harvesting local MBeanServer ({})...", this.type);
connection = ManagementFactory.getPlatformMBeanServer();
} else {
try {
JMXServiceURL jmxServiceURL = new JMXServiceURL(this.url);
connector = JMXConnectorFactory.connect(jmxServiceURL, getEnv());
connection = connector.getMBeanServerConnection();
host = jmxServiceURL.toString();
} catch (Exception e) {
LOGGER.error("Can't connect to MBeanServer {} ({})", this.url, this.type, e);
}
}
if (connection == null) {
LOGGER.debug("MBean connection is null, nothing to do");
return;
}
String currentObjectName = null;
try {
LOGGER.debug("Creating harvester");
BeanHarvester harvester = new BeanHarvester(connection, this.type);
LOGGER.debug("Populating names ({})", this.objectNames);
Set<ObjectName> names = new HashSet<> ();
if (objectNames.size() > 0) {
for (String objectName : this.objectNames) {
LOGGER.debug("Query {}", objectName);
currentObjectName = objectName;
names.addAll(connection.queryNames(getObjectName(objectName), null));
}
} else {
names.addAll(connection.queryNames(getObjectName(null), null));
}
String topic = (properties.get(EventConstants.EVENT_TOPIC) != null) ? (String) properties.get(EventConstants.EVENT_TOPIC) : "decanter/collect/jmx/";
for (ObjectName name : names) {
LOGGER.debug("Harvesting {}", name);
try {
Map<String, Object> data = harvester.harvestBean(name);
PropertiesPreparator.prepare(data, properties);
data.put("host", host);
Event event = new Event(topic + this.type + "/" + getTopic(name), data);
LOGGER.debug("Posting for {}", name);
dispatcher.postEvent(event);
} catch (Exception e) {
LOGGER.warn("Can't read MBean {} ({})", name, this.type, e);
}
}
for (String operation : operations.keySet()) {
String raw = operations.get(operation);
String[] split = raw.split("\\|");
if (split.length == 4) {
ObjectName objectName = new ObjectName(split[0]);
String operationName = split[1];
String[] arguments = split[2].split(",");
String[] signatures = split[3].split(",");
Map<String, Object> data = harvester.executeOperation(operation, objectName, operationName, arguments, signatures);
PropertiesPreparator.prepare(data, properties);
data.put("host", host);
Event event = new Event(topic + this.type + "/" + getTopic(objectName), data);
dispatcher.postEvent(event);
} else {
LOGGER.warn("{} is not well configured ({})", operation, raw);
}
}
} catch (Exception e) {
LOGGER.warn("Can't query object name from {} ({}) {}", this.url, this.type, currentObjectName);
}
try {
connector.close();
} catch (Exception e) {
LOGGER.trace("Can't close JMX connector", e);
}
LOGGER.debug("Karaf Decanter JMX Collector harvesting {} done", this.type);
}