3

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

user8363477
  • 655
  • 4
  • 14
  • 24

1 Answers1

4

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();
    }
Sampisa
  • 1,487
  • 2
  • 20
  • 28