Java JMX FlightRecorderMXBean

JMX Java About 3,115 words

功能

FlightRecorderMXBean可以获取Java服务的飞行记录仪的信息。

  • 开启/停止/关闭飞行记录仪
  • 获取飞行记录仪数据
  • 获取飞行记录仪记录的事件类型
  • 导出飞行记录仪二进制文件(可使用JMC工具打开)

代码

public class FlightRecorderMXBeanTest {

    public static void main(String[] args) {
        FlightRecorderMXBean flightRecorderMXBean = ManagementFactory.getPlatformMXBean(FlightRecorderMXBean.class);
        long l = flightRecorderMXBean.newRecording();
        flightRecorderMXBean.startRecording(l);

        flightRecorderMXBean.stopRecording(l);


        List<RecordingInfo> recordings = flightRecorderMXBean.getRecordings();
        for (RecordingInfo recording : recordings) {
            System.out.println("RecordingInfo Id#" + recording.getId());
            System.out.println("RecordingInfo Name#" + recording.getName());
            System.out.println("RecordingInfo Destination#" + recording.getDestination());
            System.out.println("RecordingInfo State#" + recording.getState());
            System.out.println("RecordingInfo StartTime#" + recording.getStartTime());
            System.out.println("RecordingInfo StopTime#" + recording.getStopTime());
            System.out.println("RecordingInfo Duration#" + recording.getDuration());
            System.out.println("RecordingInfo MaxAge#" + recording.getMaxAge());
            System.out.println("RecordingInfo MaxSize#" + recording.getMaxSize());
            System.out.println("RecordingInfo Size#" + recording.getSize());
            System.out.println("RecordingInfo DumpOnExit#" + recording.getDumpOnExit());
            System.out.println("RecordingInfo Settings#" + recording.getSettings());
            System.out.println("------------------------------------------------------");
        }
        try {
            flightRecorderMXBean.closeRecording(l);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println("==========================================================");
//        List<EventTypeInfo> eventTypes = flightRecorderMXBean.getEventTypes();
//        for (EventTypeInfo eventType : eventTypes) {
//            System.out.println("EventTypeInfo Id#" + eventType.getId());
//            System.out.println("EventTypeInfo Name#" + eventType.getName());
//            System.out.println("EventTypeInfo Description#" + eventType.getDescription());
//            System.out.println("EventTypeInfo Label#" + eventType.getLabel());
//            System.out.println("EventTypeInfo CategoryNames#" + eventType.getCategoryNames());
//            System.out.println("EventTypeInfo SettingDescriptors#" + eventType.getSettingDescriptors());
//            System.out.println("------------------------------------------------------");
//        }
    }

}

输出

RecordingInfo Id#1
RecordingInfo Name#1
RecordingInfo Destination#null
RecordingInfo State#STOPPED
RecordingInfo StartTime#1704935942134
RecordingInfo StopTime#1704935942135
RecordingInfo Duration#0
RecordingInfo MaxAge#0
RecordingInfo MaxSize#0
RecordingInfo Size#112224
RecordingInfo DumpOnExit#false
RecordingInfo Settings#{}
------------------------------------------------------
Views: 588 · Posted: 2024-01-12

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh