初始化

This commit is contained in:
qiminbao
2025-08-20 16:52:23 +08:00
commit c926ce7db8
61 changed files with 4682 additions and 0 deletions
@@ -0,0 +1,81 @@
package com.tongran.agentserver.scheduler;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.tongran.agentserver.server.collect.net.NetService;
import com.tongran.agentserver.server.collect.vo.NetVO;
import com.tongran.agentserver.server.netty.NettyTcpClient;
import com.tongran.agentserver.server.netty.model.Message;
import com.tongran.agentserver.utils.AgentUtil;
import com.tongran.agentserver.utils.AssertLog;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.ScheduledFuture;
@Component
@EnableScheduling
public class NetScheduler {
@Resource
private TaskScheduler taskScheduler;
private ScheduledFuture<?> scheduledTask;
@Resource
private NettyTcpClient nettyTcpClient;
@Resource
private NetService netService;
@PostConstruct
public void init() {
// 等待72秒
try {
Thread.sleep(72000);
} catch (InterruptedException e) {
e.printStackTrace();
}
startTask(180000); // 默认180秒间隔启动
}
public void startTask(long intervalMillis) {
if (scheduledTask != null) {
scheduledTask.cancel(false);
}
Runnable task = () -> {
AssertLog.info("网卡信息包准备={}", LocalDateTime.now());
// 业务逻辑
if (nettyTcpClient.isConnected()) {
// 发送网卡信息包
List<NetVO> list = netService.query();
String data = JSONArray.toJSONString(list);
String clientId = AgentUtil.getMotherboardUUID();
Message message = Message.builder().clientId(clientId).dataType("NET").data(data).build();
// 将对象转为 JSON 字符串
String json = "agent-tcp:"+JSON.toJSONString(message)+"@tong-ran";
AssertLog.info("发送网卡信息包={}",json);
nettyTcpClient.sendMessage(json);
}
};
scheduledTask = taskScheduler.scheduleAtFixedRate(task, intervalMillis);
}
public void updateInterval(long newIntervalMillis) {
startTask(newIntervalMillis);
}
public void stopTask() {
if (scheduledTask != null) {
scheduledTask.cancel(false);
scheduledTask = null;
}
}
}