Java源码示例:com.mongodb.MongoTimeoutException
示例1
private boolean serverNotRunning(IllegalStateException ex) {
@SuppressWarnings("serial")
NestedCheckedException nested = new NestedCheckedException("failed", ex) {
};
Throwable root = nested.getRootCause();
if (root instanceof MongoServerSelectionException
|| root instanceof MongoTimeoutException) {
if (root.getMessage().contains("Unable to connect to any server")) {
return true;
}
if (TIMEOUT_MESSAGE_PATTERN.matcher(root.getMessage()).matches()) {
return true;
}
}
return false;
}
示例2
@Test(expected = MongoTimeoutException.class)
public void testExceptionThrownIfWrongCredentialsAreGiven() {
createDefaultMongoUser();
Map<String, String> arguments = new HashMap<>();
arguments.put(RealMongoFactory.PORT, String.valueOf(mongo.getMappedPort(MONGO_PORT)));
arguments.put(RealMongoFactory.HOST, mongo.getContainerIpAddress());
arguments.put(RealMongoFactory.USE_AUTHENTICATION, "true");
arguments.put(RealMongoFactory.USERNAME, "wrongusername");
arguments.put(RealMongoFactory.PASSWORD, "wrongpassword");
arguments.put(RealMongoFactory.DATABASE_NAME, TEST_DATABASE);
try (RealMongoFactory mongoFactory = new RealMongoFactory(arguments)) {
MongoDatabase mongoDatabase = mongoFactory.createDatabase();
MongoCollection<Document> collection = mongoDatabase.getCollection("testCollection");
collection.insertOne(new Document().append("hello", "world"));
}
}
示例3
public void init(String dbName, MongoClient client) {
this.mongo = client;
this.mongoDatabase = this.mongo.getDatabase(dbName);
// wait for the DB
try {
var address = mongo.getAddress();
LOGGER.info("Connected to MongoDB @ {}/{}.", address, dbName);
this.initialized = true;
} catch (MongoTimeoutException ex) {
LOGGER.error("Fatal error. Could not connect to the MongoDB: {}", ex);
}
}
示例4
@Override
public List<StatusDetail> statusDetails() {
String databaseStatusName = "MongoDB Status";
Document document = new Document().append("ping", 1);
Document answer;
try {
answer = mongoDatabase.runCommand(document);
} catch (MongoTimeoutException e) {
return singletonList(
statusDetail(databaseStatusName, ERROR, "Mongo database check ran into timeout (" + e.getMessage() + ").")
);
} catch (Exception other) {
return singletonList(
statusDetail(databaseStatusName, ERROR, "Exception during database check (" + other.getMessage() + ").")
);
}
if (answer != null && answer.get("ok") != null && (Double)answer.get("ok") == 1.0d) {
return singletonList(
statusDetail(databaseStatusName, OK, "Mongo database is reachable.")
);
}
return singletonList(
statusDetail(databaseStatusName, ERROR, "Mongo database unreachable or ping command failed.")
);
}
示例5
@Test
public void shouldReturnErrorStatusWhenDatabaseTimesOut() {
//given
when(mongoDatabase.runCommand(new Document().append("ping", 1))).thenThrow(new MongoTimeoutException("Timeout"));
//when
final StatusDetail statusDetail = testee.statusDetails().get(0);
//then
assertThat(statusDetail.getStatus(), is(ERROR));
assertThat(statusDetail.getMessage(), containsString("Mongo database check ran into timeout"));
}
示例6
public void await() throws Throwable {
if (!latch.await(10, SECONDS)) {
throw new MongoTimeoutException("Publisher timed out");
}
if (error != null) {
throw error;
}
}
示例7
public ObservableSubscriber<T> await(final long timeout, final TimeUnit unit) throws Throwable {
subscription.request(Integer.MAX_VALUE);
if (!latch.await(timeout, unit)) {
throw new MongoTimeoutException("Publisher onComplete timed out");
}
if (!errors.isEmpty()) {
throw errors.get(0);
}
return this;
}
示例8
public ObservableSubscriber<T> await(final int request, final long timeout, final TimeUnit unit) throws Throwable {
subscription.request(request);
if (!latch.await(timeout, unit)) {
throw new MongoTimeoutException("Publisher onComplete timed out");
}
if (!errors.isEmpty()) {
throw errors.get(0);
}
return this;
}
示例9
public CountingSubscriber<T> await(final long timeout, final TimeUnit unit) throws Throwable {
if (!latch.await(timeout, unit)) {
if (!isCompleted()) {
subscription.cancel();
}
throw new MongoTimeoutException("Publisher onComplete timed out");
}
if (!isCompleted()) {
subscription.cancel();
}
if (error != null) {
throw error;
}
return this;
}