parent
3f6611d5f9
commit
043876f09d
@ -1,39 +0,0 @@
|
|||||||
package com.dsic.gj_erp.wsclient;
|
|
||||||
|
|
||||||
|
|
||||||
import com.dsic.gj_erp.wsclient.handler.HeartHandler;
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
|
||||||
|
|
||||||
@Component
|
|
||||||
@RequiredArgsConstructor
|
|
||||||
public class WebSocketClientComponent {
|
|
||||||
|
|
||||||
@Value("${ws.client.url}")
|
|
||||||
private String url;
|
|
||||||
|
|
||||||
@Getter
|
|
||||||
@Value("${ws.client.id}")
|
|
||||||
private String erpId;
|
|
||||||
|
|
||||||
private final WebSocketService webSocketService;
|
|
||||||
private final HeartHandler heartHandler;
|
|
||||||
|
|
||||||
@PostConstruct
|
|
||||||
public void init() {
|
|
||||||
// 发送消息示例
|
|
||||||
try {
|
|
||||||
webSocketService.connect(this.url);
|
|
||||||
webSocketService.sendMessage(Message.reg(this.erpId).toString());
|
|
||||||
heartHandler.execute(Message.heart(erpId));
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,56 +0,0 @@
|
|||||||
package com.dsic.gj_erp.wsclient.handler;
|
|
||||||
|
|
||||||
import cn.hutool.cron.CronUtil;
|
|
||||||
import cn.hutool.cron.task.Task;
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
|
||||||
import com.dsic.gj_erp.wsclient.Message;
|
|
||||||
import com.dsic.gj_erp.wsclient.WebSocketService;
|
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
@RequiredArgsConstructor
|
|
||||||
public class HeartHandler implements IHandlerService{
|
|
||||||
|
|
||||||
private final WebSocketService webSocketService;
|
|
||||||
|
|
||||||
private boolean scheduleStartFlag=false;
|
|
||||||
|
|
||||||
@Value("${ws.client.url}")
|
|
||||||
private String url;
|
|
||||||
|
|
||||||
@Getter
|
|
||||||
@Value("${ws.client.id}")
|
|
||||||
private String erpId;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean execute(Message message) {
|
|
||||||
if (this.scheduleStartFlag){
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
CronUtil.schedule("*/10 * * * * *", (Task) () -> {
|
|
||||||
try {
|
|
||||||
System.out.println("心跳");
|
|
||||||
webSocketService.sendMessage(message.toString());
|
|
||||||
} catch (Exception e) {
|
|
||||||
if (webSocketService.getSession()==null){
|
|
||||||
webSocketService.connect(this.url);
|
|
||||||
try {
|
|
||||||
webSocketService.sendMessage(Message.reg(this.erpId).toString());
|
|
||||||
} catch (Exception ex) {
|
|
||||||
throw new RuntimeException(ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// 支持秒级别定时任务
|
|
||||||
CronUtil.setMatchSecond(true);
|
|
||||||
CronUtil.start();
|
|
||||||
this.scheduleStartFlag=true;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in new issue