应用启动的时候会初始化AgentManagerService中的定时任务checkAgentState检查代理状态
scheduledTaskService.addFixedDelayedScheduledTaskInTransactionContext(runnable, 1000);
定时任务做两件事情
- 检查数据库中agent是否连接
- 检查所有连接是否为新连接,如果为新连接则写入数据库
public void updateAgents(List<AgentInfo> newAgents,
List<AgentInfo> updatedAgents,
List<AgentInfo> stateUpdatedAgents,
List<AgentInfo> removedAgents) {
if (CollectionUtils.isNotEmpty(newAgents)) {
agentManagerRepository.save(newAgents);
}
if (CollectionUtils.isNotEmpty(updatedAgents)) {
agentManagerRepository.save(updatedAgents);
}
for (AgentInfo each : stateUpdatedAgents) {
agentManagerRepository.updateState(each.getId(), each.getState());
}
if (CollectionUtils.isNotEmpty(removedAgents)) {
agentManagerRepository.delete(removedAgents);
}
agentManagerRepository.flush();
}