Java Code Examples for com.mongodb.Mongo#close()

The following examples show how to use com.mongodb.Mongo#close() . 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: MongoDbGridFSIO.java    From beam with Apache License 2.0 6 votes vote down vote up
@Override
public long getEstimatedSizeBytes(PipelineOptions options) throws Exception {
  Mongo mongo = spec.connectionConfiguration().setupMongo();
  try {
    GridFS gridfs = spec.connectionConfiguration().setupGridFS(mongo);
    DBCursor cursor = createCursor(gridfs);
    long size = 0;
    while (cursor.hasNext()) {
      GridFSDBFile file = (GridFSDBFile) cursor.next();
      size += file.getLength();
    }
    return size;
  } finally {
    mongo.close();
  }
}
 
Example 2
Source File: SchedulingInformationTest.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
@Test
@Ignore
public void test() throws Exception {
	Mongo mongo = new Mongo();
	Platform platform = Platform.getInstance();
	platform.setMongo(mongo);
	platform.initialize();
	
	SchedulingInformationCollection col = platform.getProjectRepositoryManager().getProjectRepository().getSchedulingInformation();
	
	String id = "foo";
	
	SchedulingInformation si = new SchedulingInformation();
	si.setWorkerIdentifier(id).setIsMaster(false).getCurrentLoad().add("foo");
	
	col.add(si);
	col.sync();
	
	si = col.findOneByWorkerIdentifier(id);
	
	assertEquals(1, si.getCurrentLoad().size());

	// Attempt to clear it
	si.getCurrentLoad().clear();
	si.setWorkerIdentifier(si.getWorkerIdentifier()); // FIXME: We have to force dirtying
	col.sync();
	
	si = col.findOneByWorkerIdentifier(id);
	assertEquals(0, si.getCurrentLoad().size());
	
	// Clean up
	si = col.findOneByWorkerIdentifier(id);
	col.remove(si);
	col.sync();
	
	mongo.close();
}
 
Example 3
Source File: TestSchedulingInfo.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
@Test
@Ignore
public void test() throws Exception {
	
	Mongo mongo = new Mongo();
	
	Platform platform = Platform.getInstance();
	platform.setMongo(mongo);
	platform.initialize();
	
	SchedulingInformation job = new SchedulingInformation();
	job.setWorkerIdentifier("Test");
	job.setHeartbeat(System.currentTimeMillis());
	job.getCurrentLoad().add("hi");
	
	platform.getProjectRepositoryManager().getProjectRepository().getSchedulingInformation().add(job);
	platform.getProjectRepositoryManager().getProjectRepository().getSchedulingInformation().sync();
	
	job = platform.getProjectRepositoryManager().getProjectRepository().getSchedulingInformation().findOneByWorkerIdentifier("Test");
	assertNotNull(job);
	assertEquals(1, job.getCurrentLoad().size());

	job.getCurrentLoad().add("Test2");
	platform.getProjectRepositoryManager().getProjectRepository().getSchedulingInformation().sync();
	
	job = platform.getProjectRepositoryManager().getProjectRepository().getSchedulingInformation().findOneByWorkerIdentifier("Test");
	assertNotNull(job);
	assertEquals(2, job.getCurrentLoad().size());
	
	platform.getProjectRepositoryManager().getProjectRepository().getSchedulingInformation().remove(job);
	platform.getProjectRepositoryManager().getProjectRepository().getSchedulingInformation().sync();
	
	mongo.close();
}
 
Example 4
Source File: TestProjectResource.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testPostInsert() throws Exception {
	Request request = new Request(Method.POST, "http://localhost:8182/projects/");

	ObjectMapper mapper = new ObjectMapper();
	
	ObjectNode p = mapper.createObjectNode();
	p.put("name", "test");
	p.put("shortName", "test-short");
	p.put("description", "this is a description");

	request.setEntity(p.toString(), MediaType.APPLICATION_JSON);
	
	Client client = new Client(Protocol.HTTP);
	Response response = client.handle(request);
	
	System.out.println(response.getEntity().getText() + " " + response.isEntityAvailable());
	
	validateResponse(response, 201);
	
	// Now try again, it should fail
	response = client.handle(request);
	validateResponse(response, 409);
	
	// Clean up
	Mongo mongo = new Mongo();
	DB db = mongo.getDB("scava");
	DBCollection col = db.getCollection("projects");
	BasicDBObject query = new BasicDBObject("name", "test");
	col.remove(query);

	mongo.close();
}
 
Example 5
Source File: MongoDbGridFSIO.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public List<? extends BoundedSource<ObjectId>> split(
    long desiredBundleSizeBytes, PipelineOptions options) throws Exception {
  Mongo mongo = spec.connectionConfiguration().setupMongo();
  try {
    GridFS gridfs = spec.connectionConfiguration().setupGridFS(mongo);
    DBCursor cursor = createCursor(gridfs);
    long size = 0;
    List<BoundedGridFSSource> list = new ArrayList<>();
    List<ObjectId> objects = new ArrayList<>();
    while (cursor.hasNext()) {
      GridFSDBFile file = (GridFSDBFile) cursor.next();
      long len = file.getLength();
      if ((size + len) > desiredBundleSizeBytes && !objects.isEmpty()) {
        list.add(new BoundedGridFSSource(spec, objects));
        size = 0;
        objects = new ArrayList<>();
      }
      objects.add((ObjectId) file.getId());
      size += len;
    }
    if (!objects.isEmpty() || list.isEmpty()) {
      list.add(new BoundedGridFSSource(spec, objects));
    }
    return list;
  } finally {
    mongo.close();
  }
}
 
Example 6
Source File: MongoDBDriver.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
public static void close()
{
    synchronized( MongoDBDriver.class )
    {
        if( sm_mongoServerNodes == null )
            return;

        for( Mongo node : sm_mongoServerNodes.values() )
        {
            node.close();
        }
        sm_mongoServerNodes.clear();
        sm_mongoServerNodes = null;
    }
}