com.mongodb.MongoTimeoutException Java Examples

The following examples show how to use com.mongodb.MongoTimeoutException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SampleMongoApplicationTests.java    From spring-boot-samples with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #2
Source File: MongoFactoryIT.java    From baleen with Apache License 2.0 6 votes vote down vote up
@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"));
  }
}
 
Example #3
Source File: PersistenceManager.java    From clouditor with Apache License 2.0 5 votes vote down vote up
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);
  }
}
 
Example #4
Source File: MongoStatusDetailIndicator.java    From edison-microservice with Apache License 2.0 5 votes vote down vote up
@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.")
    );
}
 
Example #5
Source File: MongoStatusDetailIndicatorTest.java    From edison-microservice with Apache License 2.0 5 votes vote down vote up
@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"));
}
 
Example #6
Source File: ChangeStreamSamples.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
public void await() throws Throwable {
    if (!latch.await(10, SECONDS)) {
        throw new MongoTimeoutException("Publisher timed out");
    }
    if (error != null) {
        throw error;
    }
}
 
Example #7
Source File: SubscriberHelpers.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #8
Source File: Fixture.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #9
Source File: Fixture.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
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;
}