在 Spring 引导中使用 MongoTemplate 检查 MongoDB 连接

Check MongoDB connection using MongoTemplate in Spring Boot

我正在研究 Spring Kafka Mongo 集成 insert/update 操作,并使用 mongotemplate 来执行这些操作。我想知道有什么方法可以检查 mongodb 连接是 up/down 以便万一我的数据库出现故障我想手动提交 kafka 偏移量。目前,所有数据库配置都在 application.properties 文件

中提供

类似的东西怎么样?

@Autowired
private MongoTemplate mt;

public String ping()
{
    DBObject ping = new BasicDBObject("ping", "1");
    try {
        CommandResult answer = mt.getDb().command(ping);
        return answer.getErrorMessage();
    } catch (Exception e) {
        return e.getMessage();
    }