Spring Task 實(shí)現(xiàn)定時任務(wù)
一、Spring Task使用步驟:
1.maven坐標(biāo) spring-context
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
2.啟動類添加注解 @EnableScheduling 開啟任務(wù)調(diào)度
@EnableScheduling//開啟任務(wù)調(diào)度
3.自定義定時任務(wù)類
去設(shè)置網(wǎng)站設(shè)置要 進(jìn)行得定時任務(wù) cron表達(dá)式在線生成器:https://cron.qqe2.com/
@Component
@Slf4j
//創(chuàng)建類 然后再啟動類啟動 即可看到控制臺打印
public class MyTask {
@Scheduled(cron = "0/5 * * * * ?")
public void executeTask(){
log.info("定時任務(wù): {}",new Date());
}
}
二、Web Socket消息通知
1.導(dǎo)入maven坐標(biāo)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
2.導(dǎo)入websocket組件
@Component
@ServerEndpoint("/ws/{sid}")
public class WebSocketServer {
//存放會話對象
private static Map<String, Session> sessionMap = new HashMap();
/**
* 連接建立成功調(diào)用的方法
*/
@OnOpen
public void onOpen(Session session, @PathParam("sid") String sid) {
System.out.println("客戶端:" + sid + "建立連接");
sessionMap.put(sid, session);
}
/**
* 收到客戶端消息后調(diào)用的方法
*
* @param message 客戶端發(fā)送過來的消息
*/
@OnMessage
public void onMessage(String message, @PathParam("sid") String sid) {
System.out.println("收到來自客戶端:" + sid + "的信息:" + message);
}
/**
* 連接關(guān)閉調(diào)用的方法
*
* @param sid
*/
@OnClose
public void onClose(@PathParam("sid") String sid) {
System.out.println("連接斷開:" + sid);
sessionMap.remove(sid);
}
/**
* 群發(fā)
*
* @param message
*/
public void sendToAllClient(String message) {
Collection<Session> sessions = sessionMap.values();
for (Session session : sessions) {
try {
//服務(wù)器向客戶端發(fā)送消息
session.getBasicRemote().sendText(message);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
3.設(shè)置配置類
@Configuration
public class WebSocketConfiguration {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
4.導(dǎo)入代碼文章來源:http://www.zghlxwxcb.cn/news/detail-828034.html
@Component
public class WebSocketTask {
@Autowired
private WebSocketServer webSocketServer;
/**
* 通過WebSocket每隔5秒向客戶端發(fā)送消息
*/
@Scheduled(cron = "0/5 * * * * ?")
public void sendMessageToClient() {
webSocketServer.sendToAllClient("這是來自服務(wù)端的消息:" + DateTimeFormatter.ofPattern("HH:mm:ss").format(LocalDateTime.now()));
}
}
前端測試代碼文章來源地址http://www.zghlxwxcb.cn/news/detail-828034.html
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>WebSocket Demo</title>
</head>
<body>
<input id="text" type="text" />
<button onclick="send()">發(fā)送消息</button>
<button onclick="closeWebSocket()">關(guān)閉連接</button>
<div id="message">
</div>
</body>
<script type="text/javascript">
var websocket = null;
var clientId = Math.random().toString(36).substr(2);
//判斷當(dāng)前瀏覽器是否支持WebSocket
if('WebSocket' in window){
//連接WebSocket節(jié)點(diǎn)
websocket = new WebSocket("ws://localhost:8080/ws/"+clientId);
}
else{
alert('Not support websocket')
}
//連接發(fā)生錯誤的回調(diào)方法
websocket.onerror = function(){
setMessageInnerHTML("error");
};
//連接成功建立的回調(diào)方法
websocket.onopen = function(){
setMessageInnerHTML("連接成功");
}
//接收到消息的回調(diào)方法
websocket.onmessage = function(event){
setMessageInnerHTML(event.data);
}
//連接關(guān)閉的回調(diào)方法
websocket.onclose = function(){
setMessageInnerHTML("close");
}
//監(jiān)聽窗口關(guān)閉事件,當(dāng)窗口關(guān)閉時,主動去關(guān)閉websocket連接,防止連接還沒斷開就關(guān)閉窗口,server端會拋異常。
window.onbeforeunload = function(){
websocket.close();
}
//將消息顯示在網(wǎng)頁上
function setMessageInnerHTML(innerHTML){
document.getElementById('message').innerHTML += innerHTML + '<br/>';
}
//發(fā)送消息
function send(){
var message = document.getElementById('text').value;
websocket.send(message);
}
//關(guān)閉連接
function closeWebSocket() {
websocket.close();
}
</script>
</html>
到了這里,關(guān)于Spring Task 實(shí)現(xiàn)定時任務(wù) 以及 WebSocket 實(shí)現(xiàn) 訂單提醒 (學(xué)習(xí)筆記)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!