SpringBoot——定时任务+WebSocket(问题)
开发环境:win7 + idea2018 + jdk 1.8 + springboot 2.x
记一次出现问题,我在项目中先集成了websocket环境,并且测试通过,之后想要模拟实时推送的效果,虽然可以直接使用线程类进行模拟,但是想到最近看到了定时任务,就像试一试,没有想到,这不试不知道啊,这里有个坑,主要原因还是由于个人的基础太浅薄,英文水平太辣鸡。
WebSocket环境搭建可以参考我之前的记录(https://www.cnblogs.com/threadj/p/10552904.html),在这里只记录本次出现问题
首先是搭建定时任务环境:
1、创建定时任务,如下使用@Scheduled注解并指定其时间周期,该方法为一个定时任务,使用@Component将定时任务类加入Spring管理
未验证:都说定时任务只能放在一个单独的类里面,就比如说不可以在@Controller中直接用@Scheduled注解标注一个定时任务,这个暂时我还没有进行验证
package com.zyzj.site_civilization.task; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zyzj.site_civilization.net.websocket.WebSocketHandler;
import com.zyzj.site_civilization.net.websocket.WebSocketPool;
import com.zyzj.site_civilization.service.EquipmentService;
import com.zyzj.site_civilization.util.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import javax.websocket.Session;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map; @Slf4j
@Component
public class DataSupportTask implements InitializingBean { @Autowired
private EquipmentService equipmentService; private Map<String, Session> map;
private ObjectMapper mapper = new ObjectMapper();
private List<Equipment> equipmentList; public EquipmentDataHistory init(){
//初始化数据
类 data = new 类();//嘿嘿
return data;
} //定时任务执行体,5秒执行一次,fixedRate具体意义不说了,因为我忘了
@Scheduled(fixedRate = 5000)
public void dataSupport(){
map = WebSocketPool.getSession();
if (map.size() > 0){
try {
WebSocketHandler.sendMessageAll(mapper.writeValueAsString(init()));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}else {
log.info("当前无在线用户");
}
} @Override
public void afterPropertiesSet() throws Exception {
//这个方法会在IOC对当前类进行实例化时,当所有的依赖注入完成后调用此方法
//我不知道怎么表达对不对
//我这里因为我在init方法中需要使用到这个数据,所以在这里初始化初始化
equipmentList = equipmentService.queryEntityListAll();
}
}
2、在启动类加上@EnableScheduling注解,到此定时任务类完成创建
package com.zyzj.site_civilization; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication
@EnableScheduling
public class TestApplication { public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
} }
官方文档参考:https://spring.io/guides/gs/scheduling-tasks/
然而,在我启动程序的时候出事了,我可是看着官方例子写的好不好,
出错信息如下:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-03-30 17:28:17.789 ERROR 17644 --- [ main] o.s.boot.SpringApplication : 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:392) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:224) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1115) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1082) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.resolveSchedulerBean(ScheduledAnnotationBeanPostProcessor.java:313) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.finishRegistration(ScheduledAnnotationBeanPostProcessor.java:254) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:231) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:103) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:402) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:359) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:896) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.finishRefresh(ServletWebServerApplicationContext.java:163) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:552) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at com.zyzj.site_civilization.SiteCivilizationApplication.main(SiteCivilizationApplication.java:12) [classes/:na] Process finished with exit code 1
从报错信息上面看,是Bean named 'defaultSockJsTaskScheduler' is expected to be of type 'org.springframework.scheduling.TaskScheduler' but was actually of type 'org.springframework.beans.factory.support.NullBean',注入的类型不对,随后百度。
package com.jian.test.config; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; @Configuration
public class ScheduledConfig { @Bean
public TaskScheduler taskScheduler(){
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(10);
taskScheduler.initialize();
return taskScheduler;
}
}
待验证:至于为什么会出现这样的问题,猜测可能是因为websocket也需要定时任务(心跳检测),定时任务其实也是依赖于一个新的线程,这些线程需要管理起来,需要线程池,websocket里面好像默认实现了一个ThreadPoolTaskScheduler,(ThreadPoolTaskScheduler实现了TaskScheduler接口),定时任务也需要线程池,可能这里面有什么冲突

后记:最主要的问题,还是基础太差
SpringBoot——定时任务+WebSocket(问题)的更多相关文章
- springboot集成websocket的两种实现方式
WebSocket跟常规的http协议的区别和优缺点这里大概描述一下 一.websocket与http http协议是用在应用层的协议,他是基于tcp协议的,http协议建立链接也必须要有三次握手才能 ...
- Springboot整合Websocket遇到的坑
Springboot整合Websocket遇到的坑 一.使用Springboot内嵌的tomcat启动websocket 1.添加ServerEndpointExporter配置bean @Confi ...
- springboot 定时任务部署至linux服务器上后会执行两次问题
springboot定时任务在本地运行时,正常执行且只执行一次,但是在maven打包成war包,部署至linux服务器上之后,定时任务奇怪的执行了两次. 由于未做负载均衡,所以可以先排除是因为多台服务 ...
- SpringBoot 整合 WebSocket
SpringBoot 整合 WebSocket(topic广播) 1.什么是WebSocket WebSocket为游览器和服务器提供了双工异步通信的功能,即游览器可以向服务器发送消息,服务器也可以向 ...
- SpringBoot集成WebSocket【基于纯H5】进行点对点[一对一]和广播[一对多]实时推送
代码全部复制,仅供自己学习用 1.环境搭建 因为在上一篇基于STOMP协议实现的WebSocket里已经有大概介绍过Web的基本情况了,所以在这篇就不多说了,我们直接进入正题吧,在SpringBoot ...
- SpringBoot基于websocket的网页聊天
一.入门简介正常聊天程序需要使用消息组件ActiveMQ或者Kafka等,这里是一个Websocket入门程序. 有人有疑问这个技术有什么作用,为什么要有它?其实我们虽然有http协议,但是它有一个缺 ...
- Spring boot(三) springboot 定时任务
这个不多说,springboot 定时任务非常简单就可以实现了. 30s运行一次 , @Scheduled(cron="0,30 * * * * ?") 通过这个控制定时时间 cr ...
- springboot定时任务之旅
springboot定时任务 假设场景:单体应用的定时任务,假设我们已经有了一个搭建好的springboot应用,但是需要添加一个定时执行的部分(比如笔者遇到的是定时去请求一个接口数据来更新某个表), ...
- springboot整合websocket原生版
目录 HTTP缺点 HTTP websocket区别 websocket原理 使用场景 springboot整合websocket 环境准备 客户端连接 加入战队 微信公众号 主题 HTTP请求用于我 ...
随机推荐
- Git - 使用BitBucket和SourceTree进行源代码管理遇到POST git-receive-pack (chunked)
我使用的是SourceTree Mac版,提交到BitBucket时出现 一直处于 POST git-receive-pack (chunked) 状态,经过百度,解决问题 在使用SourceTre ...
- 获取页面所有链接的JS
写了一个实用的JS脚本,获取当前页面所有的JS: var str = " \n"; var list = document.getElementsByTagName("a ...
- RabbitMQ 安装和说明
一.安装 1. 下载源码,RabbitMQ是使用Erlang开发,所以安装RabbitMQ前需要先安装Erlang.官方推荐从源码安装Erlang,因此下面开始从源码安装OTP 17.0.下载OTP ...
- <input type=file>上传唯一控件
值得注意的是:当一个表单里面包含这个上传元素的时候,表单的enctype必须指定为multipart/form-data,method必须指定为post,浏览器才会认识并正确执行.但是还有一点,浏览器 ...
- windows乱码
对于支持 UNICODE的应用程序,Windows 会默认使用 Unicode编码.对于不支持Unicode的应用程序Windows 会采用 ANSI编码 (也就是各个国家自己制定的标准编码方式,如对 ...
- Thread和Runable的区别、Synchronized锁关键字
一.Thread和Runable的区别 Thread是基类,子类必继承他实现其run方法.其也是实现了Runable接口.Thread是普通的类,并非抽象类或者密封类等. Runnable是接口,子类 ...
- 使用sift特征点进行knn最近邻匹配
#include <opencv2/xfeatures2d/nonfree.hpp> #include <opencv2/features2d/features2d.hpp> ...
- python字典获取最大值的键的值
有时我们需要字典中数值最大的那个键的名字,使用max(dict, key=dict.get)函数非常的方便 key_name = max(my_dict, key=my_dict.get) 获取之后你 ...
- SEO工作中如何增加用户体验?10个细节要注意!
我们一直在做的网站SEO工作,如果你认为它的目的仅仅是为了提高网站的排名那就错了,还有一个同样很重要的方面就是增加用户的体验,使网站更加符合网民的浏览习惯,需要做到这个方面的成功我们有10个小细节是需 ...
- libevent 网络IO分析
libevent 网络IO分析 Table of Contents 1. 简介 2. 简单使用与入门 2.1. 定时器-timeout 超时回调 2.2. 信号事件 2.3. 读取 socket 3. ...