开发者

how to check from a driver, if mongoDB server is running

I wonder, if there is a way to check if mongoDB server is running from java driver for mongoDB?

According to the tutorial, I can do

Mongo m = new Mongo();
// or
Mongo m = new Mongo( "localhost" , 27017 );
// and
DB db = m.getDB( "mydb" );

But how to check that I can use these Mongo and DB? I see no isConnected() method in the API.

db.g开发者_Go百科etConnector().isOpen() 

returns true

The only way I found is call db.getDatabaseNames() and catch MongoException.

If there some more civilized approach?


You can run a ping command

 Mongo mongo = new Mongo();
 DBObject ping = new BasicDBObject("ping", "1");
 try {
       mongo.getDB("dbname").command(ping);
 } catch (MongoException e) {
       ...
 }


I've found this to be more direct than the ping command:

Mongo mongo = new Mongo();
try {
  mongo.getConnector().getDBPortPool(mongo.getAddress()).get().ensureOpen();
} catch (Exception e) {
  ...
}


if there is a way to check if mongoDB server is running from java driver for MongoDB?

So if you can do the following:

Mongo m = new Mongo( "localhost" , 27017 );
DB db = m.getDB( "mydb" );

Then you are connected to the database, otherwise that m.getDB() would be throwing an exception. If you can connect to the database, then the MongoDB server is running.

The only way I found is call db.getDatabaseNames() and catch MongoException. If there some more civilized approach?

Is there something specifically wrong with this approach?

The driver basically runs in a sandbox where it can or cannot connect. You're asking the driver to know something specific about the server (is process X running?), but that's not the driver's job. It can either connect or it can't, it's not responsible for operating the service/process, just for connecting to it.

To know that the process is actually running, you need administrative functions on that server that allow you to check that mongod is indeed running with the correct parameters.


public boolean keepAlive(Mongo mongo) {
    return mongo.getAddress() != null;
}

This will return null for address if mongo is down. You can look within the implementation of getAddress() to see why it is a good way to check the mongo's status.

I assume you've initialized the mongo parameter properly.


I haven't tested this thoroughly (only using a localhost mongo) but it appears to work so far:

public boolean mongoRunningAt(String uri) {
    try {
        Mongo mongo = new Mongo(new MongoURI(uri));
        try {
            Socket socket = mongo.getMongoOptions().socketFactory.createSocket();
            socket.connect(mongo.getAddress().getSocketAddress());
            socket.close();
        } catch (IOException ex) {
            return false;
        }
        mongo.close();
        return true;
    } catch (UnknownHostException e) {
        return false;
    }
}

And the tests I've used:

@Test
public void whenMongoNotAvailableAtSpecificURLThenTheLoaderKnows() {
    assertThat(mongoRunningAt("mongodb://127.0.0.1:12345"), is(false));
}

@Test
public void whenMongoAvailableAtSpecificURLThenTheLoaderKnows() {
    assertThat(mongoRunningAt("mongodb://127.0.0.1:27017"), is(true));
}

It's not exactly using a well defined public API so use at your own risk.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜