1. 程式人生 > >Spring boot + Websocket 再篇

Spring boot + Websocket 再篇

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
@Configuration
public 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)
@Component
public 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("異常發生...");
    }
}


相關推薦

Spring boot + Websocket

package com.demo.config; import com.demo.entity.CustomSpringConfigurator; import org.springframewor

Spring Boot:Thymeleaf

格式 驗證機制 html webapp 表單 freemark entity tac err Spring Boot幹貨系列:(四)Thymeleaf篇http://www.cnblogs.com/zheting/p/6707037.html 前言

Spring Boot MyBatis升級-註解-動態SQL(if test)-方案二:@Provider(8)

指定 ins pro builder except uil test 就是 class 1)動態語言註解(2)@Provider使用思路(3)@SelectProvider小試牛刀(4)@SelectProvider初露鋒芒(5)@SelectProvider過關斬將(6)

Spring Boot (Web ):配置 AOP 切面

目錄 AOP 簡介 AOP相關概念 AOP相關注解 編寫AOP 在pom.xml加入相關依賴 編寫切面類 頁面訪問(專案啟動) JoinPoint詳解 關於多個切面的執行順序 總結 原始碼下載 AOP 簡介 AOP相關概念 Tar

Spring Boot (測試):SpringBoot 測試

SpringBoot 測試 測試是對於一個CodeMonkey來說很重要的,也是必須要掌握的一項技能,凡事自測。 在pom.xml加入相關依賴 引入SpringBoot Test,這個預設新建 SpringBoot 專案就會自帶jar包。 編寫測試類 專案啟動

Spring Boot (Web ):整合攔截器Interceptor

目錄 說在前面 在pom.xml加入相關依賴 定義攔截器 編寫攔截器 Interceptor 註冊攔截器 Interceptor 頁面訪問(專案啟動) 總結 原始碼下載 說在前面 Struts2(Interceptor篇):攔截器的實現

Spring Boot (基礎):建立一個簡單的SpringBoot專案

Spring Boot建立   首先IDEA新建一個專案,File-New-Project-Spring Initializr,然後Next,如下圖: 填寫專案資訊: 填寫專案使用到的技術,上面的SpringBoot版本建議選擇最新的穩定版,下面勾選上Web就可

Spring Boot (Web ):支援JSP

  目錄 前言 在pom.xml加入相關依賴 配置application.properties 建立JSP頁面 Controller層 JSP頁面 專案啟動 第一種方式,SpringBoot直接啟動(內建整合tomcat) 瀏覽器訪問 第二種

Spring Boot (日誌):整合預設日誌logback

目錄 說在前面 logback(Spring Boot 預設日誌) 在pom.xml加入相關依賴 日誌級別 控制檯輸出 lombok方式 日誌檔案輸出 儲存路徑 級別控制 輸出格式 自定義日誌配置 根節點包含的屬性 屬性一: 設定上下

Spring Boot (配置):devtools實現熱部署

      初使用SpringBoot時候,是採用的Tomcat的Update class and resources,後來發現SpringBoot 有自帶的 熱部署工具。和Tomcat熱部署類似,只要有修改程式碼(編譯通過的)就會自動重新啟動程式(Tomcat),

Spring Boot (基礎):Spring Boot知識點總結

Spring Boot簡介 SpringBoot 以便捷開發、快速部署著稱。設計目的是用來簡化新Spring應用的初始搭建以及開發過程。 SpringBoot 致力於簡潔,讓開發者寫更少的配置,程式能夠更快的執行和啟動 SpringBoot 設計原則就是 約定優於配置。並且

Spring Boot (Web ):整合Fastjson

目錄 說在前面 Fastjson目標 在pom.xml加入相關依賴 整合 Fastjson 配置管理類WebMvcConfigurer 程式設計式配置 實體類Customer 控制器類IndexController @ResponseBody 頁面訪問

Spring Boot (Web ):整合Thymeleaf模板

  目錄 說在前面 Thymeleaf特點 在pom.xml加入相關依賴 配置application.properties Controller層 Thymeleaf頁面 專案啟動 第一種方式,SpringBoot直接啟動(內建整合tomcat)

Spring Boot (Web ):整合FreeMarker模板

目錄 前言 工作原理​ 在pom.xml加入相關依賴 配置application.properties Controller層 FreeMarker頁面 專案啟動 第一種方式,SpringBoot直接啟動(內建整合tomcat) 瀏覽器訪問 第二種方式,打

Spring Boot (Web ):整合Servlet

說在前面 Servlet簡介:https://blog.csdn.net/Soinice/article/details/82745918 當使用Spring-Boot時,嵌入式Servlet容器通過掃描註解的方式註冊Servlet、Filter和Servlet規範的所有監聽器(如Htt

Spring Boot (Web ):整合監聽器Listener

說在前面 Listener簡介:https://blog.csdn.net/Soinice/article/details/82787964 上一篇文章已經對定義Filter 的方法進行了說明,監聽器(Listener)的註冊方法和Filter一樣,不清楚的可以檢視下上一篇文章:http

Spring Boot (Web ):整合過濾器Filter

說在前面 Filter簡介:https://blog.csdn.net/Soinice/article/details/82787964 上一篇文章已經對定義Servlet 的方法進行了說明,過濾器(Filter)的註冊方法和 Servlet 一樣,不清楚的可以檢視下上一篇文章:http

Spring Boot WebSocket 單節點模擬實現單點登入擠退

1、建立WebSocketServer @ServerEndpoint("/websocket/{sid}") @Component  // 成分、元件 public class WebSocketServer {     //靜態變數,用來記錄當前線上

使用 Spring Boot WebSocket 建立聊天室

1    第2-10課:使用Spring Boot WebSocket建立聊天室 1.1.1       什麼是 WebSocket 以前,很多網站為了實現推送技術,所用的技術都是輪詢。輪詢是在特定的的時間間隔(如每

Spring boot webSocket從入門到放棄

在構建Spring boot專案時已經提供webSocket依賴的勾選。webSocket是TCP之上的一個非常薄的輕量級層 ,webSocket主要的應用場景離不開即時通訊與訊息推送,但只要應用程式需要在瀏覽器和伺服器之間來回傳送訊息,就可以使用webSocket來降低客戶端流量與伺服器的負載。 下面將基