连接到mongoDB

时间:2017-07-31 18:39:42

标签: java mongodb jenkins

我正在尝试连接到mongoDB,并获取MongoTimeoutException。在本地运行时,一切都按预期工作,但是当从Jenkins项目内部运行时,会发生超时。

 private JsonNode getMongoResponse(String mongoURL, String mongoPort, String mongoDB, String mongoCollection, String userId, String userPW, String requestId) {
    ObjectMapper objectMapperResponse = new ObjectMapper();
    String responseStringFull = "[null]";
    MongoClient mongoClient = null;
    JsonNode responseObj = null;
    String responseString = null;
    Logger mongoLogger = Logger.getLogger("org.mongodb.driver");
    mongoLogger.setLevel(Level.SEVERE);
    mongoClient = new MongoClient(new MongoClientURI("mongodb://" + userId + ":" + userPW + "@" + mongoURL + ":" + mongoPort));
    MongoDatabase mongoDatabase = mongoClient.getDatabase(mongoDB);
    MongoCollection<Document> collection = mongoDatabase.getCollection(mongoCollection);
    try {
        FindIterable<Document> tempVal = collection.find(eq("extId", requestId)).sort(eq("timestamp", -1));
        responseStringFull = tempVal.first().toJson();
        responseObj = objectMapperResponse.readValue(responseStringFull, JsonNode.class).get("payload");
        responseString = "[" + responseObj.toString() + "]";
        responseObj = objectMapperResponse.readValue(responseString, JsonNode.class);
    } catch (SocketTimeoutException stoex) {
        System.out.println("Socket Timeout: " + stoex.getMessage());
        return null;
    } catch (MongoTimeoutException mtoe) {
        System.out.println("Mongo Timeout: " + mtoe.getMessage());
        Logger.getLogger(Thread_fetchMongoResults.class.getName()).log(Level.SEVERE, null, mtoe);
        return null;
    } catch (IOException ioe) {
        System.out.println("i/o exception: " + ioe.getMessage());
        Logger.getLogger(Thread_fetchMongoResults.class.getName()).log(Level.SEVERE, null, mtoe);
        return null;
    } catch (NullPointerException npe) {
        return null;
    } catch (Exception ex) {
        System.out.println(ex.getMessage());
        Logger.getLogger(Thread_fetchMongoResults.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
    mongoClient.close();
    return responseObj;
}

以下是我收到的错误消息:

Mongo Timeout: com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=[dbAddress]:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketTimeoutException: connect timed out}}]
at com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:377)
at com.mongodb.connection.BaseCluster.selectServer(BaseCluster.java:104)
at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:75)
at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:71)
at com.mongodb.binding.ClusterBinding.getReadConnectionSource(ClusterBinding.java:63)
at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:402)
at com.mongodb.operation.FindOperation.execute(FindOperation.java:510)
at com.mongodb.operation.FindOperation.execute(FindOperation.java:81)
at com.mongodb.Mongo.execute(Mongo.java:836)
at com.mongodb.Mongo$2.execute(Mongo.java:823)
at com.mongodb.FindIterableImpl$FindOperationIterable.first(FindIterableImpl.java:216)
at com.mongodb.FindIterableImpl.first(FindIterableImpl.java:156)
...

我目前正在使用mongo-java-driver-3.4.2.jar库。

请原谅所有单独的错误;我把它们放进去试图调试这个问题,我花了大部分时间试图找到一种方法来延长mongo连接超时,但我似乎无法找到正确的关键词来为goggle提出一些东西这很有效。

如果有人能帮助我,我会永远感激。

ETA:这是在服务器迁移后开始发生的,上述代码在非迁移的mongo数据库上运行良好。我愿意打赌他们在迁移过程中设置方式存在问题,但我仍然可以使用java代码之外的mongoclient连接到数据库,因此我知道数据库是可访问的。

0 个答案:

没有答案