com.mongodb.MongoClientException Java Examples

The following examples show how to use com.mongodb.MongoClientException. 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: DataService.java    From canal-mongo with Apache License 2.0 6 votes vote down vote up
public void insertData(String schemaName, String tableName, DBObject naive, DBObject complete) {
    int i = 0;
    DBObject logObj = (DBObject) ObjectUtils.clone(complete);
    //保存原始数据
    try {
        String path = "/" + schemaName + "/" + tableName + "/" + CanalEntry.EventType.INSERT.getNumber();
        i++;
        naiveMongoTemplate.getCollection(tableName).insert(naive);
        i++;
        SpringUtil.doEvent(path, complete);
        i++;
    } catch (MongoClientException | MongoSocketException clientException) {
        //客户端连接异常抛出,阻塞同步,防止mongodb宕机
        throw clientException;
    } catch (Exception e) {
        logError(schemaName, tableName, 1, i, logObj, e);
    }
}
 
Example #2
Source File: DataService.java    From canal-mongo with Apache License 2.0 6 votes vote down vote up
public void updateData(String schemaName, String tableName, DBObject query, DBObject obj) {
    String path = "/" + schemaName + "/" + tableName + "/" + CanalEntry.EventType.UPDATE.getNumber();
    int i = 0;
    DBObject newObj = (DBObject) ObjectUtils.clone(obj);
    DBObject logObj = (DBObject) ObjectUtils.clone(obj);
    //保存原始数据
    try {
        obj.removeField("id");
        i++;
        naiveMongoTemplate.getCollection(tableName).update(query, obj);
        i++;
        SpringUtil.doEvent(path, newObj);
        i++;
    } catch (MongoClientException | MongoSocketException clientException) {
        //客户端连接异常抛出,阻塞同步,防止mongodb宕机
        throw clientException;
    } catch (Exception e) {
        logError(schemaName, tableName, 2, i, logObj, e);
    }
}
 
Example #3
Source File: DataService.java    From canal-mongo with Apache License 2.0 6 votes vote down vote up
public void deleteData(String schemaName, String tableName, DBObject obj) {
    int i = 0;
    String path = "/" + schemaName + "/" + tableName + "/" + CanalEntry.EventType.DELETE.getNumber();
    DBObject newObj = (DBObject) ObjectUtils.clone(obj);
    DBObject logObj = (DBObject) ObjectUtils.clone(obj);
    //保存原始数据
    try {
        i++;
        if (obj.containsField("id")) {
            naiveMongoTemplate.getCollection(tableName).remove(new BasicDBObject("_id", obj.get("id")));
        }
        i++;
        SpringUtil.doEvent(path, newObj);
    } catch (MongoClientException | MongoSocketException clientException) {
        //客户端连接异常抛出,阻塞同步,防止mongodb宕机
        throw clientException;
    } catch (Exception e) {
        logError(schemaName, tableName, 3, i, logObj, e);
    }
}
 
Example #4
Source File: MongoDBArtifactStore.java    From hawkbit-extensions with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Retrieves an artifact from the store by its SHA1 hash.
 *
 * @param sha1Hash
 *            the sha1-hash of the file to lookup.
 * 
 * @return The DbArtifact object or {@code null} if no file exists.
 */
@Override
public AbstractDbArtifact getArtifactBySha1(final String tenant, final String sha1Hash) {

    try {

        GridFSFile found = gridFs.findOne(new Query()
                .addCriteria(Criteria.where(FILENAME).is(sha1Hash).and(TENANT_QUERY).is(sanitizeTenant(tenant))));

        // fallback pre-multi-tenancy
        if (found == null) {
            found = gridFs.findOne(
                    new Query().addCriteria(Criteria.where(FILENAME).is(sha1Hash).and(TENANT_QUERY).exists(false)));
        }

        return createGridFsArtifact(found);
    } catch (final MongoClientException e) {
        throw new ArtifactStoreException(e.getMessage(), e);
    }
}
 
Example #5
Source File: MongoDBConfig.java    From datacollector with Apache License 2.0 6 votes vote down vote up
private MongoDatabase createMongoDatabase(
    Stage.Context context,
    List<Stage.ConfigIssue> issues,
    ReadPreference readPreference,
    WriteConcern writeConcern
) {
  MongoDatabase mongoDatabase = null;
  try {
    if (readPreference != null) {
      mongoDatabase = mongoClient.getDatabase(database).withReadPreference(readPreference);
    } else if (writeConcern != null) {
      mongoDatabase = mongoClient.getDatabase(database).withWriteConcern(writeConcern);
    }
  } catch (MongoClientException e) {
    issues.add(context.createConfigIssue(
        Groups.MONGODB.name(),
        MONGO_CONFIG_PREFIX + "database",
        Errors.MONGODB_02,
        database,
        e.toString()
    ));
  }
  return mongoDatabase;
}
 
Example #6
Source File: MongoDBConfig.java    From datacollector with Apache License 2.0 6 votes vote down vote up
private MongoCollection createMongoCollection(
    Stage.Context context,
    List<Stage.ConfigIssue> issues,
    ReadPreference readPreference,
    WriteConcern writeConcern
) {
  MongoCollection mongoCollection = null;
  try {
    if (readPreference != null) {
      mongoCollection = mongoDatabase.getCollection(collection).withReadPreference(readPreference);
    } else if (writeConcern != null) {
      mongoCollection = mongoDatabase.getCollection(collection).withWriteConcern(writeConcern);
    }
  } catch (MongoClientException e) {
    issues.add(context.createConfigIssue(
        Groups.MONGODB.name(),
        MONGO_CONFIG_PREFIX + "collection",
        Errors.MONGODB_03,
        collection,
        e.toString()
    ));
  }
  return mongoCollection;
}
 
Example #7
Source File: MongoDBArtifactStore.java    From hawkbit-extensions with Eclipse Public License 1.0 5 votes vote down vote up
private void deleteArtifact(final GridFSFile file) {
    if (file != null) {
        try {
            gridFs.delete(new Query().addCriteria(Criteria.where(ID).is(file.getId())));
        } catch (final MongoClientException e) {
            throw new ArtifactStoreException(e.getMessage(), e);
        }
    }
}
 
Example #8
Source File: MongoDBArtifactStore.java    From hawkbit-extensions with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected AbstractDbArtifact store(final String tenant, final DbArtifactHash base16Hashes, final String contentType,
        final String tempFile) throws IOException {

    final GridFSFile result = gridFs.findOne(new Query().addCriteria(
            Criteria.where(FILENAME).is(base16Hashes.getSha1()).and(TENANT_QUERY).is(sanitizeTenant(tenant))));

    if (result == null) {
        try {
            final GridFSFile temp = loadTempFile(tempFile);

            final Document metadata = new Document();
            metadata.put(SHA1, base16Hashes.getSha1());
            metadata.put(TENANT, tenant);
            metadata.put(FILENAME, base16Hashes.getSha1());
            metadata.put(CONTENT_TYPE, contentType);

            final GridFsResource resource = gridFs.getResource(temp);
            final ObjectId id = gridFs.store(resource.getInputStream(), base16Hashes.getSha1(), contentType, metadata);
            final GridFSFile file = gridFs.findOne(new Query().addCriteria(Criteria.where(ID).is(id)));

            return createGridFsArtifact(file, contentType, base16Hashes);

        } catch (final MongoClientException e) {
            throw new ArtifactStoreException(e.getMessage(), e);
        }
    }

    return createGridFsArtifact(result, contentType, base16Hashes);
}
 
Example #9
Source File: MongoDBArtifactStore.java    From hawkbit-extensions with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected String storeTempFile(final InputStream content) {
    final String fileName = findUnusedTempFileName();

    try {
        gridFs.store(content, getTempFilename(fileName));
    } catch (final MongoClientException e) {
        throw new ArtifactStoreException(e.getMessage(), e);
    }

    return fileName;
}
 
Example #10
Source File: MongoDBArtifactStore.java    From hawkbit-extensions with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void deleteByTenant(final String tenant) {
    try {
        gridFs.delete(new Query().addCriteria(Criteria.where(TENANT_QUERY).is(sanitizeTenant(tenant))));
    } catch (final MongoClientException e) {
        throw new ArtifactStoreException(e.getMessage(), e);
    }
}
 
Example #11
Source File: MongoHealthCheck.java    From dropwizard-mongo with Apache License 2.0 5 votes vote down vote up
/**
 * Checks if the system database, which exists in all MongoDB instances can be reached.
 * @return A Result object
 * @throws Exception
 */
@Override
protected Result check() throws Exception {

    try {
        mongoClient.getDB("system").getStats();
    }catch(MongoClientException ex) {
        return Result.unhealthy(ex.getMessage());
    }


    return Result.healthy();
}