Check MongoDB connection using MongoTemplate in Spring Boot

问题: I am working on Spring Kafka Mongo Integration for insert/update operations and using mongotemplate to perform these actions. I want to know is there any way to check mongo...

问题:

I am working on Spring Kafka Mongo Integration for insert/update operations and using mongotemplate to perform these actions. I want to know is there any way to check mongodb connection is up/down so that in case my db goes down I want to commit kafka offset manually. Currently, all the db configurations are provided in application.properties file


回答1:

What about something like that?

@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();
    }
  • 发表于 2018-07-07 00:23
  • 阅读 ( 232 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除