2

It can connect with database when we run the mongod server manually. But I want to run MongoDB server by Java code, so is there any way to do it?

Mongo mongo = new Mongo("localhost", 27017);
DB db = mongo.getDB("@NEW_DB");
1
  • There are ways, but there is probably a better approach. Please explain what you're trying to get done. Commented Sep 1, 2014 at 8:28

4 Answers 4

3

There's a platform independent wrapper, that will download and run the requested MongoDB binary: https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo

You'll need to make some changes to your test configuration, but otherwise it's a drop-in replacement. It has been working for us without any problems.

Sign up to request clarification or add additional context in comments.

1 Comment

Worked for me just fine in one (very big) project, and now starting to use this in the second project. I can really recommend this.
1

In this case you need an application that runs on the server and gets the command from your client application to launch the mongo db server.

If there's no some "command listener" application on the server side and mongodb server is down, you cannot launch it.

Make an application, that runs on the server and listens to a network port.

Your client application sends command via this port. Listener application receives this command and launches an external process (mongodb server). Also consider to add some security measures, to prevent getting such commands from an unauthorized source.

To launch an external process from your java code consider ProcessBuilder class.

I personally prefer to use Apache Commons Exec for this purpose.

Comments

1

When no mongod process is running on the local machine, you need to start one. There are different methods to start another program from a Java program.

The most crude way is to use Runtime.exec(String) which executes a command as if it were entered into the operating systems command shell. It returns a Process object which allows you to retrieve the output of the process you start and gives you some control over the process.

A more sophisticated but also more verbose way is to use the class ProcessBuilder. The main advantage is that the process builder allows you to write code which is more operating-system independent.

Keep in mind that either method only works when mongodb runs on the same server as your application. In a usual production-setup, mongodb runs on multiple dedicated servers which are physically separated from the application server. Starting a process on a different machine isn't that simple for obvious reasons. There are software solutions available which allow you to remotely manage multiple processes on many servers at once (the server admins at our company like Control-M), but this is in the realm of server administrations.

Comments

0

MongoDB is not written in Java, so you need to launch an external process, as indicated by the previous answers.

If you want to run and shutdown MongoDB for integration tests, there is a test framework that will do that: https://github.com/lordofthejars/nosql-unit/.

In your JUnit 4 test:

import static com.lordofthejars.nosqlunit.mongodb.ManagedMongoDb.MongoServerRuleBuilder.newManagedMongoDbRule;

@ClassRule
public static ManagedMongoDb managedMongoDb = newManagedMongoDbRule().build();

IIRC, It will download and launch a new MongoDB instance at localhost:27017 by default.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.