Usage

To use JMX Extractor as a library add following to your Maven dependencies:

<dependency>
    <groupId>com.jlupin.jmx.extractor</groupId>
    <artifactId>jmx-extractor</artifactId>
    <version>1.6.1.0</version>
</dependency>

Then all you need to do is to create JMXExtractor object and use it. Nothing more is needed. Example usage:

final Logger logger = LoggerFactory.getLogger("JMXExtractor");
final JMXExtractor jmxExtractor = new JMXExtractor(logger);

final Map<String, Object> all = jmxExtractor.extractAll("127.0.0.1", 9010);

// all is a map where all field are Map or String, dump is shown below
// {
//   "currency-converter-chf": "MICROSERVICE_OUTPUT_OBJECT",
//   [...],
//   "MAIN_SERVER": "MAIN_SERVER_OUTPUT_OBJECT"
// }

Where MICROSERVICE_OUTPUT_OBJECT is an output with MBeans (full example can be found here: native, servlet, queue, channel) and where MAIN_SERVER_OUTPUT_OBJECT is an output with MBeans (full example can be found here).

final Logger logger = LoggerFactory.getLogger("JMXExtractor");
final JMXExtractor jmxExtractor = new JMXExtractor(logger);

final Map<String, Object> mainServerAttributesMap = new HashMap<>();
{
    final Map<String, Object> map = new HashMap<>();
    map.put("ThreadCount", Boolean.TRUE);
    mainServerAttributesMap.put("java.lang:type=Threading", map);
}

final Map<String, Object> microservicesCommonAttributesMap = new HashMap<>();
{
    final Map<String, Object> map = new HashMap<>();
    map.put("StartTime", Boolean.TRUE);
    microservicesCommonAttributesMap.put("java.lang:type=Runtime", map);
}

final Map<String, Object> currencyConverterChfAttributesMap = new HashMap<>();
{
    final Map<String, Object> map = new HashMap<>();
    map.put("LoadedClassCount", Boolean.TRUE);
    currencyConverterChfAttributesMap.put("java.lang:type=ClassLoading", map);
}

final Attributes mainServerAttributes = new Attributes(mainServerAttributesMap);
final Attributes microservicesCommonAttributes = new Attributes(microservicesCommonAttributesMap);
final Attributes currencyConverterChfAttributes = new Attributes(currencyConverterChfAttributesMap);
final Map<String, Attributes> microservicesSpecificAttributes = new HashMap<>();
microservicesSpecificAttributes.put("currency-converter-chf", currencyConverterChfAttributes);

final Map<String, Object> extract = jmxExtractor.extractSelected(
        "127.0.0.1", 9010, mainServerAttributes, microservicesCommonAttributes, microservicesSpecificAttributes
);


// extract is a map where all field are Map or String, dump is shown below
// {
//   "currency-converter-chf": {
//     "java.lang:type=ClassLoading": {
//       "LoadedClassCount": "5809"
//     },
//     "java.lang:type=Runtime": {
//       "StartTime": "1554802064316"
//     }
//   },
//   "channelMicroservice": {
//     "java.lang:type=Runtime": {
//       "StartTime": "1554802051778"
//     }
//   },
//   [...],
//   "MAIN_SERVER": {
//     "java.lang:type=Threading": {
//       "ThreadCount": "70"
//     }
//   }
// }