springboot+websocket接口+定时器 无刷新页面向浏览器推送数据

websocket 接口

websocket 可以保持 客户端和后台的连接,关于websocket接口相关知识可以参考:

https://blog.csdn.net/zhangdehua678/article/details/78913839

spring boot 定时器

java 定时器有很多,这里只介绍spring boot 配置和使用。

  1. spring boot 定时器入口
package com.scheduling;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class Application {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class);
    }
}

EnableScheduling 注解为定时任务的入口

@Component
public class Scheduler{

    @Scheduled(cron=" 0 0 0 */7 * ?") //每七天执行一次
   //这里需要注意的是 使用 @Scheduled注解 方法后面是不可以跟参数的要不然会报错的,异常如下
     Only no-arg methods may be annotated with @Scheduled
    public void aa(){
     //业务代码
    }

@Component 需要类 注册进容器
@Scheduled 项目定时设置的类

参考地址:

https://www.cnblogs.com/ysySelf/p/10812900.html

实现思路和代码

websocket 实现了 前端和后端的连接,可以实时的从后端向前端推送相关的数据

我们保存每个连接到map 中,然后获取这个map,启用定时任务,向session中,添加数据,推送到前端就可以,提供的2个例子完全能实现这个。

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值