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. Ubuntu20.4 bs4安装的正确姿势

    一.背景 公司一小伙子反馈在内网机器上通过代理,还是安装不了bs4:于是乎,作为菜鸡的我开始排查.一直认为是网络和代理问题,所以关注点一直放在网络和安装包上:在网上搜索到,主要是以下问题: 1)更新a ...

  2. c语言:getchar() getch()回显

    //getch() 不回显函数,当用户按下某个字符时,函数自动读取,无需按回车 //所在头文件:conio.h 从控制台读取一个字符,但不显示在屏幕上 //int getchar() //头文件:#i ...

  3. 深度解析CSS中的单位以及区别

    css中有几个不同的单位表示长度,使用时数字加单位.如果长度为0,则可以省略单位. 长度单位可分为两种类型:相对和绝对. 绝对长度 绝对长度单位是一个固定的值,反应真实的物理尺寸,不依赖于显示器.分辨 ...

  4. endless 如何实现不停机重启 Go 程序?

    转载请声明出处哦~,本篇文章发布于luozhiyun的博客:https://www.luozhiyun.com/archives/584 前几篇文章讲解了如何实现一个高效的 HTTP 服务,这次我们来 ...

  5. CRC校验原理

    此文为转载文,原作者博客传送门 CRC校验原理 CRC校验原理看起来比较复杂,好难懂,因为大多数书上基本上是以二进制的多项式形式来说明的.其实很简单的问题,其根本思想就是先在要发送的帧后面附加一个数( ...

  6. OpenFaaS实战之二:函数入门

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  7. Attention和Transformer详解

    目录 Transformer引入 Encoder 详解 输入部分 Embedding 位置嵌入 注意力机制 人类的注意力机制 Attention 计算 多头 Attention 计算 残差及其作用 B ...

  8. 使用bind部署DNS主从服务器

    说明:这里是Linux服务综合搭建文章的一部分,本文可以作为单独搭建主从DNS服务器的参考. 注意:这里所有的标题都是根据主要的文章(Linux基础服务搭建综合)的顺序来做的. 如果需要查看相关软件版 ...

  9. python包安装

    python包安装: 一种是有网操作:pip install  包名:例子[pip install setuptools] 无网络服务器上操作: 先把包下载:传上去再安装[] 1.一种是   *.wh ...

  10. Pb代理工具之mitmproxy

    mitmproxy 一 . mitmproxy介绍 mitmproxy 就是用于 MITM 的 proxy,MITM 即中间人攻击(Man-in-the-middle attack). 不同于 fid ...