SpringWebSocketConfig配置

package com.meeno.chemical.socket.task.config;

import com.meeno.chemical.socket.task.handler.TaskProgressWebSocketHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; /**
* @description: SpringBoot WebSocket config
* @author: Wzq
* @create: 2020-07-16 10:50
*/
@Configuration
@EnableWebSocket
public class SpringWebSocketConfig implements WebSocketConfigurer { @Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
// 注册自定义消息处理,消息路径为`/ws/taskProgress`
registry.addHandler(new TaskProgressWebSocketHandler(),"/ws/taskProgress").setAllowedOrigins("*");
}
}

TaskProgressWebSocketHandler

package com.meeno.chemical.socket.task.handler;

import org.springframework.http.HttpHeaders;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.adapter.standard.StandardWebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler; import java.io.IOException;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap; /**
* @description: 任务进度WebSocketHandler
* @author: Wzq
* @create: 2020-07-16 10:51
*/
public class TaskProgressWebSocketHandler extends TextWebSocketHandler { /**
* concurrent包的线程安全Map,用来存放每个客户端对应的MyWebSocket对象。
*/
public final static ConcurrentHashMap<String, WebSocketSession> webSocketMap = new ConcurrentHashMap<String, WebSocketSession>(); /**
* 处理消息
* @param session
* @param message
* @throws Exception
*/
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
//在这里自定义消息处理...
TextMessage textMessage = new TextMessage("${message body}");
session.sendMessage(textMessage);
} /**
* 连接建立后
* @param session
* @throws Exception
*/
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
StandardWebSocketSession standardWebSocketSession = (StandardWebSocketSession) session;
List<String> taskNameList = standardWebSocketSession.getNativeSession().getRequestParameterMap().get("taskName");
String taskName = taskNameList.get(0);
//保存所有会话
webSocketMap.put(taskName,session);
} /**
* 连接关闭后
* @param session
* @param status
* @throws Exception
*/
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception { if(!webSocketMap.isEmpty()){
// remove session after closed
WebSocketSession webSocketSession = webSocketMap.get(session.getId());
if(webSocketSession != null){
webSocketMap.remove(session.getId());
}
}
} /**
* 发送消息给所有人
* @param content
*/
public static void sendMessageAll(String content){
webSocketMap.forEach((taskNameKey,session) ->{
TextMessage textMessage = new TextMessage(content);
try {
session.sendMessage(textMessage);
} catch (IOException e) {
e.printStackTrace();
}
});
} /**
* 发送消息给指定某个任务
* @param taskName
* @param content
*/
public static void sendMessage(String taskName,String content){
webSocketMap.forEach((taskNameKey,session) ->{
if(taskName.equals(taskNameKey)){
TextMessage textMessage = new TextMessage(content);
try {
session.sendMessage(textMessage);
} catch (IOException e) {
e.printStackTrace();
}
}
});
} }

问题

启用是会和定时任务冲突

需要配置

ScheduledConfig.java

package com.meeno.chemical.common.scheduling.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; /**
* @description: 任务config
* @author: Wzq
* @create: 2020-07-16 11:05
*/
@Configuration
public class ScheduledConfig { @Bean
public TaskScheduler taskScheduler(){
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(10);
taskScheduler.initialize();
return taskScheduler;
} }

SpringBoot集成websocket(Spring方式)的更多相关文章

  1. springboot集成websocket的两种实现方式

    WebSocket跟常规的http协议的区别和优缺点这里大概描述一下 一.websocket与http http协议是用在应用层的协议,他是基于tcp协议的,http协议建立链接也必须要有三次握手才能 ...

  2. springboot集成drools的方式一

    springboot集成drools的方式一(spring-drools.xml) 本文springboot采用1.5.1.RELEASE版本,drools采用的6.5.0.Final,一共会讲三种方 ...

  3. springboot集成websocket实现向前端浏览器发送一个对象,发送消息操作手动触发

    工作中有这样一个需示,我们把项目中用到代码缓存到前端浏览器IndexedDB里面,当系统管理员在后台对代码进行变动操作时我们要更新前端缓存中的代码怎么做开始用想用版本方式来处理,但这样的话每次使用代码 ...

  4. springboot集成websocket实现大文件分块上传

    遇到一个上传文件的问题,老大说使用http太慢了,因为http包含大量的请求头,刚好项目本身又集成了websocket,想着就用websocket来做文件上传. 相关技术 springboot web ...

  5. SpringBoot集成websocket发送后台日志到前台页面

    业务需求 后台为一个采集系统,需要将采集过程中产生的日志实时发送到前台页面展示,以便了解采集过程. 技能点 SpringBoot 2.x websocket logback thymeleaf Rab ...

  6. SpringBoot集成WebSocket【基于纯H5】进行点对点[一对一]和广播[一对多]实时推送

    代码全部复制,仅供自己学习用 1.环境搭建 因为在上一篇基于STOMP协议实现的WebSocket里已经有大概介绍过Web的基本情况了,所以在这篇就不多说了,我们直接进入正题吧,在SpringBoot ...

  7. SpringBoot集成websocket(java注解方式)

    第一种:SpringBoot官网提供了一种websocket的集成方式 第二种:javax.websocket中提供了元注解的方式 下面讲解简单的第二种 添加依赖 <dependency> ...

  8. SpringBoot集成redis + spring cache

    Spring Cache集成redis的运行原理: Spring缓存抽象模块通过CacheManager来创建.管理实际缓存组件,当SpringBoot应用程序引入spring-boot-starte ...

  9. springboot集成webSocket能启动,但是打包不了war

    1.pom.xml少packing元素 https://www.cnblogs.com/zeussbook/p/10790339.html 2.SpringBoot项目中增加了WebSocket功能无 ...

随机推荐

  1. 深入理解Java并发类——AQS

    目录 什么是AQS 为什么需要AQS AQS的核心思想 AQS的内部数据和方法 如何利用AQS实现同步结构 ReentrantLock对AQS的利用 尝试获取锁 获取锁失败,排队竞争 参考 什么是AQ ...

  2. python pil 图像加工处理

    from PIL import Imagefrom PIL import ImageEnhanceim=Image.open("d://aa.jpg","r") ...

  3. C语言:标识符 关键字 保留字 表达式 语句

    标识符(Identifier)标识符就是程序员自己起的名字,符号常量(宏).变量名.函数名.宏名.结构体名等,它们都是标识符.遵守规范:C语言规定,标识符只能由字母(A~Z, a~z).数字(0~9) ...

  4. python -- 程序异常与调试(识别异常)

    一.识别异常 程序中出现的错误又称为异常.异常通常分为两大类:编译错误和运行错误. 如下源码是已经修改: # -----------------------------------------# 编程 ...

  5. javascript学习五---OOP

    面向对象:JavaScript的所有数据都可以看成对象 JavaScript的面向对象编程和大多数其他语言如Java.C#的面向对象编程都不太一样.如果你熟悉Java或C#,很好,你一定明白面向对象的 ...

  6. 在js中对属性的操作

    一:访问属性 两种方法: ①:对象名.属性名 function  test(sno,age,sex){      this.sno=sno,      this.age=age, this.sex=s ...

  7. endnote x9.3.3 for windows安装教程

    EndNote X9.3.3 是一款非常nice的实用型文献管理软件,EndNote X9功能极其强劲,便捷好用.本文提供EndNote X9.3.3安装破解激活教程.方法,内附EndNote x9. ...

  8. Tom_No_02 Servlet向流中打印内容,之后在调用finsihResponse,调用上是先发送了body,后发送Header的解释

    上次在培训班学上网课的时候就发现了这个问题,一直没有解决,昨天又碰到了,2-3小时也未能发现点端倪,今早又仔细缕了下,让我看了他的秘密 1.Servlet向流中打印内容,之后在调用finsihResp ...

  9. Hyper-V下Internal vSwitch的配置和Linux虚拟机的SSH连接

    最近工作中要在Windows Server 2016/Hyper-V 10中运行Ubuntu16实例,需要制作出"即插即用"的镜像文件,也就是安装好后即可从外部SSH进去.之前我使 ...

  10. web知识架构思维导图

    图片双击放大还是很清晰的.原图大小5.1M