温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

springboot2.0.6中观察器和启动的错报告以及Headless模式相关分析是怎样的

发布时间:2021-09-28 09:48:00 来源:亿速云 阅读:91 作者:柒染 栏目:大数据

这篇文章给大家介绍springboot2.0.6中观察器和启动的错报告以及Headless模式相关分析是怎样的,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

解析SpringApplication的run方法观察器、启动的错报告、Headless模式、监听器相关分析

public ConfigurableApplicationContext run(String... args) {
    // 构造一个任务执行观察器(Java 查看时间和性能的类)
   StopWatch stopWatch = new StopWatch();
    // 开始执行,记录开始时间
   stopWatch.start();
    // 定制配置上下文,顶级父类包含Bean Factory
   ConfigurableApplicationContext context = null;
    // 定义一个错误集合(用来支持报告关于启动的错)
   Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
    //定义一个 awt 的headless(设置headless模式,名字为 java.awt.headless 的系统属性设置为true)
   configureHeadlessProperty();
   ......//省略
}

1. StopWatch(观察器) 主要是spring为了计算加载耗时产生的。

  其内部包含一个 list(代表任务的个数),当开始调用 start 方法后,然后调用 stop,都会记录当前时间和 start 时间的耗时,然后封装成一个任务对象加入到 StopWatch 内部的list中,其中 start 和 stop 方法必须成对出现。

2. SpringBootExceptionReporter---用来支持报告关于启动的错

    执行 getSpringFactoriesInstances 方法从配置文件中获取具体的实现类,当 springboot 启动工程出现异常,就会调用 handleRunFailure 方法,具体如下:

private void handleRunFailure(ConfigurableApplicationContext context,
      Throwable exception,
      Collection<SpringBootExceptionReporter> exceptionReporters,
      SpringApplicationRunListeners listeners) {
   try {
      try {
         handleExitCode(context, exception);
         if (listeners != null) {
            listeners.failed(context, exception);
         }
      }
      finally {
         reportFailure(exceptionReporters, exception);
         if (context != null) {
            context.close();
         }
      }
   }
   catch (Exception ex) {
      logger.warn("Unable to close ApplicationContext", ex);
   }
   ReflectionUtils.rethrowRuntimeException(exception);
}

  首先看handleExitCode(context, exception)

private void handleExitCode(ConfigurableApplicationContext context,
      Throwable exception) {
   int exitCode = getExitCodeFromException(context, exception);
   if (exitCode != 0) {
      if (context != null) {
         context.publishEvent(new ExitCodeEvent(context, exitCode));
      }
      SpringBootExceptionHandler handler = getSpringBootExceptionHandler();
      if (handler != null) {
         handler.registerExitCode(exitCode);
      }
   }
}

    上述代码的意思就是从异常中获取退出状态码,如果退出状态码不等于0,就通过 ConfigurableApplicationContext 发布退出事件,在 SpringBootExceptionHandler 中注册下该状态码。

    下面主要看 reportFailure(exceptionReporters, exception);

private void reportFailure(Collection<SpringBootExceptionReporter> exceptionReporters,
      Throwable failure) {
   try {
      for (SpringBootExceptionReporter reporter : exceptionReporters) {
         if (reporter.reportException(failure)) {
            registerLoggedException(failure);
            return;
         }
      }
   }
   catch (Throwable ex) {
      // Continue with normal handling of the original failure
   }
   if (logger.isErrorEnabled()) {
      logger.error("Application run failed", failure);
      registerLoggedException(failure);
   }
}

    上述代码主要是依次调用 SpringBootExceptionReporter的reportException 方法(该方法主要是调用 analyzer 的集合去分析异常,如果有 analyzer 能分析成功,就把异常打印出来) 并把该异常注册到 SpringBootExceptionHandler中 。

3. 设置headless模式

    Headless模式是在缺少显示屏、键盘或者鼠标是的系统配置。在 java.awt.toolkit 和 java.awt.graphicsenvironment 类中有许多方法,除了对字体、图形和打印的操作外还可以调用显示器、键盘和鼠标的方法。但是有一些类中,比如 Canvas 和 Panel,可以在 headless 模式下执行

private void configureHeadlessProperty() {
    // 此处调用的是:java.awt.headless
    // 不提供外部设备的情况,自行运算。
    // this.headless 默认为true
   System.setProperty(SYSTEM_PROPERTY_JAVA_AWT_HEADLESS, System.getProperty(
         SYSTEM_PROPERTY_JAVA_AWT_HEADLESS, Boolean.toString(this.headless)));
}
public final class System {
    public static String setProperty(String key, String value) {
        // 判断空操作
        checkKey(key);
        // 获取安全管理员 默认null
        SecurityManager sm = getSecurityManager();
        if (sm != null) {
            sm.checkPermission(new PropertyPermission(key,
                SecurityConstants.PROPERTY_WRITE_ACTION));
        }
        // 返回
        return (String) props.setProperty(key, value);
    }
}

系统属性配置

  1. 为了启用 headless 模式,需要使用 setProperty 方法去设置相应的系统属性。    

    •  System.setProperty("java.awt.headless","true")

  2. 如果想在一个相同的程序 中使用 headless 和传统环境,你可以使用下面的命令行来完成:java -Djava.awt.headless=true

  3. 通过反射设置 java.awt.GraphicsEnvironment 中这个属性的值为true

如果名字为 java.awt.headless 的系统属性被设置true,那么 headless 工具包就会被使用。应用程序可以执行如下操作:

  1. 创建轻量级组件。

  2. 收集关于可用的字体、字体指标和字体设置的信息。

  3. 设置颜色来渲染准备图片。

  4. 创造和获取图像,为渲染准备图片。

  5. 使用java.awt.PrintJob,java.awt.print和javax.print类里的打印

关于springboot2.0.6中观察器和启动的错报告以及Headless模式相关分析是怎样的就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI