Spring boot + Websocket 再篇
来源:互联网 发布:汽车分期2016年数据 编辑:程序博客网 时间:2024/05/18 03:05
package com.demo.config;import com.demo.entity.CustomSpringConfigurator;import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@ConditionalOnWebApplication@Configurationpublic class WebSocketConfigurator { @Bean public CustomSpringConfigurator customSpringConfigurator(){ return new CustomSpringConfigurator(); }}
package com.demo.entity;import org.springframework.beans.BeansException;import org.springframework.beans.factory.BeanFactory;import org.springframework.context.ApplicationContextAware;import javax.websocket.server.ServerEndpointConfig;public class CustomSpringConfigurator extends ServerEndpointConfig.Configurator implements ApplicationContextAware { private static volatile BeanFactory context; @Override public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext) throws BeansException { CustomSpringConfigurator.context = applicationContext; } @Override public <T> T getEndpointInstance(Class<T> clazz) throws InstantiationException { return context.getBean(clazz); }}
package com.demo.entity;import com.demo.service.PerfService;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import javax.websocket.OnClose;import javax.websocket.OnError;import javax.websocket.OnOpen;import javax.websocket.Session;import javax.websocket.server.ServerEndpoint;import java.io.IOException;import java.util.Timer;import java.util.concurrent.CopyOnWriteArraySet;@ServerEndpoint(value = "/websocket-endpoint", configurator = CustomSpringConfigurator.class)@Componentpublic class OwnWebSocket { protected final Logger logger = LoggerFactory.getLogger(this.getClass()); private static CopyOnWriteArraySet<OwnWebSocket> webSockets = new CopyOnWriteArraySet<>(); private Session session; @Autowired private PerfService perfService; private Timer timer; @OnOpen public void onOpen(Session session) throws IOException{ this.session = session; webSockets.add(this); timer = new Timer(true); long delay = 0l; TimerDBTask dbTask = new TimerDBTask(session, perfService); timer.schedule(dbTask, delay, 60000); } @OnClose public void onClose() throws IOException{ webSockets.remove(this); } @OnError public void onError(Throwable throwable) throws IOException{ logger.info("异常发生..."); }}
阅读全文
0 0
- Spring boot + Websocket 再篇
- Spring Boot + WebSocket Demo
- spring-boot整合websocket
- spring boot Websocket
- spring boot-3(WebSocket)
- Spring boot 整合websocket
- spring boot websocket
- Spring boot + Websocket 初篇
- Spring boot websocket消息推送
- Spring boot websocket简单例子
- 173. Spring Boot WebSocket:群聊
- Spring Boot + Gradle + Websocket 构建推送服务
- Spring Boot WebSocket整合及nginx配置
- Spring Boot学习笔记(六) WebSocket
- Spring-boot集成Netty做websocket服务端
- Spring boot WebSocket stomp简单例子
- spring-boot + websocket 打包问题汇总
- java之Websocket后台(基于Spring boot)
- 智能一点
- 解决elasticsearch安装中文分词器
- JAVA总结(基本数据类型)
- 数论——Baby Step Giant Step及扩展算法
- 【Linux】 JDK安装及配置 (tar.gz版)
- Spring boot + Websocket 再篇
- sqlserver的几个分页语句
- Centos 安装JDK之如何下载jdk
- [LeetCode] 167.Two Sum II
- 【sql语句之】SELECT INTO 和 INSERT INTO SELECT 两种表复制语句
- 【hadoop】DataNode用jps查看无法启动解决办法
- 【Sublime Text 3】用户自定义设置文件 (Preferences.sublime-settings — User)——解决tab和space混用问题
- 我的Java数据结构和算法
- 【LeeCode】Reverse Integer 总结