接收服务端指令-更新采集时间

This commit is contained in:
qiminbao
2025-08-26 13:00:30 +08:00
parent bddbf10503
commit 1351585ed5
19 changed files with 790 additions and 490 deletions
@@ -8,12 +8,17 @@ 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.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.ScheduledFuture;
@@ -21,61 +26,80 @@ import java.util.concurrent.ScheduledFuture;
@Component
@EnableScheduling
public class NetScheduler {
@Resource
private TaskScheduler taskScheduler;
private ScheduledFuture<?> scheduledTask;
private final TaskScheduler taskScheduler;
@Resource
private NettyTcpClient nettyTcpClient;
private ScheduledFuture<?> currentTask;
private final Object lock = new Object();
@Value("${task.initial.delay:65000}")
private long initialDelay;
@Value("${task.interval:300000}")
private long interval;
@Resource
private NetService netService;
@PostConstruct
public void init() {
// 等待72秒
try {
Thread.sleep(72000);
} catch (InterruptedException e) {
e.printStackTrace();
}
startTask(300000); // 默认180秒间隔启动
@Resource
private NettyTcpClient nettyTcpClient;
public NetScheduler(TaskScheduler taskScheduler) {
this.taskScheduler = taskScheduler;
}
public void startTask(long intervalMillis) {
if (scheduledTask != null) {
scheduledTask.cancel(false);
}
@EventListener(ApplicationReadyEvent.class)
public void startDynamicTask() {
AssertLog.info("启动网络信息采集定时任务 - 延迟: {}ms, 间隔: {}ms", initialDelay, interval);
scheduleTask(initialDelay, interval);
}
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);
public void scheduleTask(long delayMillis, long intervalMillis) {
synchronized (lock) {
// 取消现有任务
if (currentTask != null) {
currentTask.cancel(false);
}
};
scheduledTask = taskScheduler.scheduleAtFixedRate(task, intervalMillis);
// 安排新任务
currentTask = taskScheduler.scheduleWithFixedDelay(
this::dynamicPeriodicTask,
Instant.now().plusMillis(delayMillis),
Duration.ofMillis(intervalMillis)
);
}
}
public void updateInterval(long newIntervalMillis) {
startTask(newIntervalMillis);
public void dynamicPeriodicTask() {
AssertLog.info("网络信息采集定时任务执行 - 时间: {}", LocalDateTime.now());
performDynamicTask();
}
public void stopTask() {
if (scheduledTask != null) {
scheduledTask.cancel(false);
scheduledTask = null;
private void performDynamicTask() {
// 判定客户端与服务端是否连接
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);
}
}
public void updateSchedule(long newDelay, long newInterval) {
AssertLog.info("更新调度配置 - 延迟: {}ms, 间隔: {}ms", newDelay, newInterval);
scheduleTask(newDelay, newInterval);
}
@PreDestroy
public void cleanup() {
if (currentTask != null) {
currentTask.cancel(false);
}
}
}