Spring Boot笔记-利用Quartz进行定时任务,利用websocket推送到浏览器(界面为thymeleaf)
发布日期:2021-06-30 10:46:53 浏览次数:2 分类:技术文章

本文共 3793 字,大约阅读时间需要 12 分钟。

效果如下,浏览器输入URL:

等待一段时间,websocket主动推送

后端打印:

程序结构如下:

QuartzConfig.java

@Configurationpublic class QuartzConfig {    @Bean    public JobDetail job(){        return JobBuilder.newJob(QuartzJob.class).storeDurably().build();    }    @Bean    public Trigger trigger(){        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("*/30 * * * * ?");        return TriggerBuilder.newTrigger()                .forJob(job())                .withSchedule(scheduleBuilder)                .build();    }}

WebSocketConfig.java

@Componentpublic class WebSocketConfig {    @Bean    public ServerEndpointExporter serverEndpointExporter(){        return new ServerEndpointExporter();    }}

MyController.java

@Controllerpublic class MyController {    @GetMapping("/")    public String test(){        return "index";    }}

QuartzJob.java

public class QuartzJob extends QuartzJobBean {    @Autowired    GoodsRepository goodsRepository;    @Autowired    WebSocket webSocket;    @Override    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {        String str = "";        for(Goods goods : goodsRepository.getGoodsArrayList()){            str += goods.toString();        }        webSocket.onMessage(str);    }}

Goods.java

@Datapublic class Goods {    Integer id;    String name;    Integer count;}

GoodsRepository.java

@Repositorypublic class GoodsRepository {    private static ArrayList
goodsArrayList = new ArrayList<>(); static { Goods goods1 = new Goods(); goods1.setId(1); goods1.setName("石油50L"); goods1.setCount(100); goodsArrayList.add(goods1); Goods goods2 = new Goods(); goods2.setId(1); goods2.setName("石油150L"); goods2.setCount(100); goodsArrayList.add(goods2); Goods goods3 = new Goods(); goods3.setId(1); goods3.setName("石油250L"); goods3.setCount(100); goodsArrayList.add(goods3); Goods goods4 = new Goods(); goods4.setId(1); goods4.setName("石油300L"); goods4.setCount(100); goodsArrayList.add(goods4); } public ArrayList
getGoodsArrayList(){ return goodsArrayList; }}

WebSocket.java

@Component@ServerEndpoint("/websocket")public class WebSocket {    //与某个客户端连接对话,通过此对客户端发送消息    private Session session;    //存放所有连接的客户端    private static ConcurrentLinkedQueue
concurrentLinkedQueue = new ConcurrentLinkedQueue<>(); @OnOpen public void onOpen(Session session){ //默认客户端,没有重名 this.session = session; concurrentLinkedQueue.add(this); System.out.println("【webSocket连接成功】当前连接人数为:" + concurrentLinkedQueue.size()); } @OnClose public void onClose() { Iterator
iterator = concurrentLinkedQueue.iterator(); while (iterator.hasNext()){ WebSocket next = iterator.next(); if(next == this){ concurrentLinkedQueue.remove(next); } } } @OnError public void onError(Session session, Throwable throwable){ System.out.println("error:"); throwable.printStackTrace(); } @OnMessage public void onMessage(String message){ Iterator
iterator = concurrentLinkedQueue.iterator(); while (iterator.hasNext()){ WebSocket next = iterator.next(); try { next.session.getBasicRemote().sendText(message); } catch (IOException e) { e.printStackTrace(); } } }}

DemoApplication.java

@SpringBootApplicationpublic class DemoApplication {    public static void main(String[] args) {        SpringApplication.run(DemoApplication.class, args);    }}

index.html

    
Title

推送页面

 

源码打包下载地址:

转载地址:https://it1995.blog.csdn.net/article/details/114866936 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Qt文档阅读笔记-Object Model初步解析
下一篇:Python&Rabbitmq文档阅读笔记-生产者数据直接送入队列消费者消费

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月15日 22时47分09秒