Files
nuttyreading-server/src/main/java/com/peanut/modules/springai/McpHeartbeatService.java
2026-04-22 15:55:42 +08:00

28 lines
708 B
Java

package com.peanut.modules.springai;
import io.modelcontextprotocol.client.McpSyncClient;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class McpHeartbeatService {
private final List<McpSyncClient> mcpClients;
public McpHeartbeatService(List<McpSyncClient> mcpClients) {
this.mcpClients = mcpClients;
}
@Scheduled(fixedRate = 120000) // 每2分钟1次发送心跳
public void sendHeartbeat() {
try {
this.mcpClients.parallelStream().forEach(client -> {client.ping();});
} catch (Exception e) {
e.printStackTrace();
}
}
}