public static void main(String[] args) {
        try {
            JmxToolOptions options = new JmxToolOptions(args);
            if (CommandLineUtils.isPrintHelpNeeded(options)) {
                CommandLineUtils.printUsageAndExit(options.parser, "Dump JMX values to standard output.");
                return;
            }
            if (CommandLineUtils.isPrintVersionNeeded(options)) {
                CommandLineUtils.printVersionAndExit();
                return;
            }

            Optional<String[]> attributesInclude = options.attributesInclude();
            Optional<DateFormat> dateFormat = options.dateFormat();
            String reportFormat = options.parseFormat();
            boolean keepGoing = true;

            MBeanServerConnection conn = connectToBeanServer(options);
            List<ObjectName> queries = options.queries();
            boolean hasPatternQueries = queries.stream().filter(Objects::nonNull).anyMatch(ObjectName::isPattern);

            Set<ObjectName> found = findObjects(options, conn, queries, hasPatternQueries);
            Map<ObjectName, Integer> numExpectedAttributes =
                    findNumExpectedAttributes(conn, attributesInclude, hasPatternQueries, queries, found);

            List<String> keys = new ArrayList<>();
            keys.add("time");
            keys.addAll(new TreeSet<>(queryAttributes(conn, found, attributesInclude).keySet()));
            maybePrintCsvHeader(reportFormat, keys, numExpectedAttributes);

            while (keepGoing) {
                long start = System.currentTimeMillis();
                Map<String, Object> attributes = queryAttributes(conn, found, attributesInclude);
                attributes.put("time", dateFormat.isPresent() ? dateFormat.get().format(new Date()) : String.valueOf(System.currentTimeMillis()));
                maybePrintDataRows(reportFormat, numExpectedAttributes, keys, attributes);
                if (options.isOneTime()) {
                    keepGoing = false;
                } else {
                    TimeUnit.MILLISECONDS.sleep(Math.max(0, options.interval() - (System.currentTimeMillis() - start)));
                }
            }
            Exit.exit(0);
        } catch (TerseException e) {
            System.err.println(e.getMessage());
            Exit.exit(1);
        } catch (Throwable e) {
            System.err.println(e.getMessage());
            System.err.println(Utils.stackTrace(e));
            Exit.exit(1);
        }
    }
A program for reading JMX metrics from a given endpoint.
<p>
This tool only works reliably if the JmxServer is fully initialized prior to invoking the tool.
See KAFKA-4620 for details.
tools/src/main/java/org/apache/kafka/tools/JmxTool.java