日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > HTML >内容正文

HTML

Spring Boot笔记-利用Quartz进行定时任务,利用websocket推送到浏览器(界面为thymeleaf)

發(fā)布時(shí)間:2025/3/15 HTML 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot笔记-利用Quartz进行定时任务,利用websocket推送到浏览器(界面为thymeleaf) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

效果如下,瀏覽器輸入U(xiǎn)RL:

等待一段時(shí)間,websocket主動(dòng)推送

后端打印:

程序結(jié)構(gòu)如下:

QuartzConfig.java

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

WebSocketConfig.java

@Component public class WebSocketConfig {@Beanpublic ServerEndpointExporter serverEndpointExporter(){return new ServerEndpointExporter();} }

MyController.java

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

QuartzJob.java

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

Goods.java

@Data public class Goods {Integer id;String name;Integer count; }

GoodsRepository.java

@Repository public class GoodsRepository {private static ArrayList<Goods> 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<Goods> getGoodsArrayList(){return goodsArrayList;} }

WebSocket.java

@Component @ServerEndpoint("/websocket") public class WebSocket {//與某個(gè)客戶(hù)端連接對(duì)話(huà),通過(guò)此對(duì)客戶(hù)端發(fā)送消息private Session session;//存放所有連接的客戶(hù)端private static ConcurrentLinkedQueue<WebSocket> concurrentLinkedQueue = new ConcurrentLinkedQueue<>();@OnOpenpublic void onOpen(Session session){//默認(rèn)客戶(hù)端,沒(méi)有重名this.session = session;concurrentLinkedQueue.add(this);System.out.println("【webSocket連接成功】當(dāng)前連接人數(shù)為:" + concurrentLinkedQueue.size());}@OnClosepublic void onClose() {Iterator<WebSocket> iterator = concurrentLinkedQueue.iterator();while (iterator.hasNext()){WebSocket next = iterator.next();if(next == this){concurrentLinkedQueue.remove(next);}}}@OnErrorpublic void onError(Session session, Throwable throwable){System.out.println("error:");throwable.printStackTrace();}@OnMessagepublic void onMessage(String message){Iterator<WebSocket> iterator = concurrentLinkedQueue.iterator();while (iterator.hasNext()){WebSocket next = iterator.next();try {next.session.getBasicRemote().sendText(message);}catch (IOException e) {e.printStackTrace();}}}}

DemoApplication.java

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

index.html

<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head><meta charset="UTF-8"><title>Title</title><script type="text/javascript">let ws = null;window.onload=function(){WebSocketTest();}function WebSocketTest(){if ("WebSocket" in window){alert("瀏覽器支持 WebSocket!");// 打開(kāi)一個(gè) web socketws = new WebSocket("ws://127.0.0.1:8080/websocket");ws.onmessage = function (evt){let received_msg = evt.data;alert("接收推送數(shù)據(jù):" + received_msg);};ws.onclose = function(){// 關(guān)閉 websocketalert("連接已關(guān)閉...");};}else{// 瀏覽器不支持 WebSocketalert("瀏覽器不支持 WebSocket!");}}</script></head> <body><h1>推送頁(yè)面</h1></body> </html>

?

源碼打包下載地址:

https://github.com/fengfanchen/Java/tree/master/QuartzDemo

總結(jié)

以上是生活随笔為你收集整理的Spring Boot笔记-利用Quartz进行定时任务,利用websocket推送到浏览器(界面为thymeleaf)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。