felixu

同时使用 Quartz 和 Websocket 报错分析

背景

在同事负责的 Spring Boot 项目中,同时使用了 spring-boot-starter-quartzspring-boot-starter-websocket 两个 jar 包依赖,创建了定时任务,这时候项目无法启动,起初这个问题是同事在项目中发现的,Google 了一下也就解决了这个问题,但是解决这个 Bug 的帖子很多,但是却没有提及为什么会出现此情况,遂问我能不能帮他看看原因,撸了一下源码后便有了此文。

分析问题

首先来看一下启动过程中的报错吧:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2020-12-08 18:50:09.535 [main] ERROR org.springframework.boot.SpringApplication [line:837] - Application run failed
org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'defaultSockJsTaskScheduler' is expected to be of type 'org.springframework.scheduling.TaskScheduler' but was actually of type 'org.springframework.beans.factory.support.NullBean'
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:399)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:227)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1175)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1142)
at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.resolveSchedulerBean(ScheduledAnnotationBeanPostProcessor.java:327)
at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.finishRegistration(ScheduledAnnotationBeanPostProcessor.java:256)
at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:233)
at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:105)
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:404)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:361)
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:898)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:554)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)

我们从报错入手,BeanNotOfRequiredTypeException 显然是 Bean 的类型不对,然后我们再具体看名称叫 defaultSockJsTaskSchedulerBean 类型不对,那搜索大法,先分别从 @EnableWebSocket@EnableScheduling 点进行,把这两个包的源码下载了再说,然后全局搜索 defaultSockJsTaskScheduler,这时候找定义叫 defaultSockJsTaskScheduler 的,很容易定位到 WebSocketConfigurationSupport1 如下代码:

1
2
3
4
5
6
7
8
9
10
11
12
@Bean
@Nullable
public TaskScheduler defaultSockJsTaskScheduler() {
if (initHandlerRegistry().requiresTaskScheduler()) {
ThreadPoolTaskScheduler threadPoolScheduler = new ThreadPoolTaskScheduler();
threadPoolScheduler.setThreadNamePrefix("SockJS-");
threadPoolScheduler.setPoolSize(Runtime.getRuntime().availableProcessors());
threadPoolScheduler.setRemoveOnCancelPolicy(true);
this.scheduler = threadPoolScheduler;
}
return this.scheduler;
}

这里是一个关键点,我们来具体看一下这里的逻辑,如果这个 if 条件不满足,这里将返回 null,也就是会创建一个 NullBean 在容器中作为 TaskScheduler

经过以上步骤我们找到了 NullBean 产生的地方,那么为什么单独使用两个包都不会有问题呢,只能解释,当单独使用 spring-boot-starter-websocket 时,即使创建了 NullBean 但是实际并不会被使用,而增加 spring-boot-starter-quartz 的过程肯定使用到了 TaskScheduler。而单独使用 spring-boot-starter-quartz 又没有问题,所以可以猜想肯定还有地方实例化了 TaskScheduler,那么进入 TaskScheduler,然后来看看那些地方用到了它:

TaskScheduler引用

很容易可以猜到 TaskSchedulingAutoConfiguration,我们进去看到了如下代码:

1
2
3
4
5
6
@Bean
@ConditionalOnBean(name = TaskManagementConfigUtils.SCHEDULED_ANNOTATION_PROCESSOR_BEAN_NAME)
@ConditionalOnMissingBean({ SchedulingConfigurer.class, TaskScheduler.class, ScheduledExecutorService.class })
public ThreadPoolTaskScheduler taskScheduler(TaskSchedulerBuilder builder) {
return builder.build();
}

经过以上定位,其实我们已经找到了问题了,这里我们来总结一下,单独有 spring-boot-starter-websocket 时,会创建 defaultSockJsTaskScheduler,但是没地方用到,所以不会有问题,而引入 spring-boot-starter-quartz 时,由于前面产生的 NullBean 导致这个条件装配失效,所以不会产生合适的 TaskScheduler 实例。由于 @EnableScheduling 的存在,导致 ScheduledAnnotationBeanPostProcessor2 类中的 finishRegistration 方法会被执行到,为啥不用细说了吧(不明白可以截图找我问),此时该方法中会执行 this.registrar.setTaskScheduler(resolveSchedulerBean(this.beanFactory, TaskScheduler.class, false)); 这串代码,这里的 resolveSchedulerBean 将会去容器中找 TaskScheduler 的实例,通过名称找到了 defaultSockJsTaskScheduler,但是他的类型却是个 NullBean 此时就会触发上述错误。

这便是导致这个 Bug 的原因了,有些人可能疑惑,你上面那些类咋知道的,emmmm,其实是猜的,找这种问题一般步骤就是猜想、验证。所以先猜,再去 Debug 验证。

解决问题

既然已经知道了为什么会出现这个错了,就容易修复了,就是因为存在了一个 NullBean 类型的 TaskScheduler 实例,在查找的时候出现了该错误,那么我们只需要自己创建一个正确类型就好了:

1
2
3
4
@Bean
public ThreadPoolTaskScheduler taskScheduler(TaskSchedulerBuilder builder) {
return builder.build();
}

当然,根据前面提到的 if 条件不满足才会出现 NullBean,那么自然还有种方案,那就是让 if 条件满足,即实现一个 WebSocketConfigurer 也可以解决这个问题(看一下 if 附近的注释,实现应该不难,就不给案例了)。

第一种方案也是搜索引擎能找到的解决方案了,这里只是探究了一下这个错误的前因后果,问题解决,愉快的告诉同事咋回事就完事了。

一点思考

然而,此时突然萌生了另一个想法。这不应该是 Spring FrameworkBug 吗,虽然在几个使用 TaskScheduler 的地方都说明可以为 null,但是可以为 null 和创建了一个 NullBean 类型作为 TaskScheduler 的实例,并导致了真正在使用的时候产生了错误,这肯定是 Spring FrameworkBug 啊。

在继续翻看了源码中的版本信息,可以了解到该问题应该已经存在数个版本了,在网上也有不少关于这个错误的处理方案,那么为什么官方却迟迟没有解决这个问题呢,难道是官方一直没有发现这个 Bug,也不是没有这个可能,又花了点时间去搜索了一遍 issue 发现确实没有相关问题。

既然已经确定这肯定是 Spring Framework 本身的 Bug 了,且没有相关 issue 那我就自己提一个吧。

于是便有了 use spring-boot-starter-quartz and spring-boot-starter-websocket simultaneously 这个 issue,提完之后,官方回复的很快,就是过程有点曲折,一开始可能认为是 Spring Boot 的问题,所以被迁移了过去,然后经过讨论后又被迁回了 Spring Framework 下。

在确定属于 Spring Framework 的问题后,又重开了一个新的 Consistent type resolution handling for NullBean 并进行了后续修复。据说会在 5.3.3 中处理掉该问题。所以之后的版本就不需要我们再去处理这个问题了。

总结

  1. 找问题应该要多去猜想验证,不能为了解决问题而解决问题;
  2. 敢于质疑,不论对于 Spring Framework 还是对于其他大佬(现在很多大 V 写的文章也存在错误),不能一味相信,要有自己的判断力。

推荐阅读

  1. org.springframework.web.socket.config.annotation.WebSocketConfigurationSupport
  2. org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor