MongoDB - Connection from Remote Database
There are multiple ways of doing this things, but I found this one is most suitable.1. From Standalone
2. From Web Application
Make sure you have added correct maven dependency in your pom.xml like below -
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.13.2</version>
</dependency>
Web Application -
Before to use below code, please add property file having credentials and all other required details in it. Read that property file in spring-config.xml. You can use below code to read the property file -<context:property-placeholder location='classpath:/config/configTest.properties'/>
configTest.properties
mongodb.dbname=
mongodb.host=
mongodb.port=
mongodb.username=
mongodb.password=
mongodb.authenticationdatabase=
mongodb.host=
mongodb.port=
mongodb.username=
mongodb.password=
mongodb.authenticationdatabase=
Now below code will take the responsibility to get connected from mongodb host.
@Configuration
public class MongoConfiguration extends AbstractMongoConfiguration{
@Value("${mongodb.dbname}")
private String dbName;
@Value("${mongodb.host}")
private String host;
@Value("${mongodb.port}")
private Integer port;
@Value("${mongodb.username}")
private String userName;
@Value("${mongodb.password}")
private String password;
@Value("${mongodb.authenticationdatabase}")
private String authenticationDatabase;
@Override
protected String getDatabaseName() {
return this.dbName;
}
@Override
public MongoClient mongo() throws Exception {
List<ServerAddress> serverAddresses = new ArrayList<ServerAddress>();
ServerAddress address = new ServerAddress(host, port);
serverAddresses.add(address);
List<MongoCredential> credentials = new ArrayList<MongoCredential>();
MongoCredential credential = MongoCredential.createPlainCredential(userName, authenticationDatabase, password.toCharArray());
credentials.add(credential);
return new MongoClient(serverAddresses, credentials);
}
@Override
@Bean
public SimpleMongoDbFactory mongoDbFactory() throws Exception {
return new SimpleMongoDbFactory(mongo(), getDatabaseName());
}
@Override
@Bean
public MongoTemplate mongoTemplate() throws Exception {
final MongoTemplate mongoTemplate = new MongoTemplate(mongo(), getDatabaseName());
mongoTemplate.setWriteConcern(WriteConcern.SAFE);
return mongoTemplate;
}
Standalone Java Program -
A standalone Java Program which tells about how to connect with remote MongoDB server from Java Code.String database = "TestDev";String username = "user@test.COM";String pass = "XXXXX";char[] password = pass.toChogram arArray();try {List<ServerAddress> serverAddresses = new ArrayList<ServerAddress>();ServerAddress address = new ServerAddress("hostname", portnumber);serverAddresses.add(address);List<MongoCredential> credentials = new ArrayList<MongoCredential>();MongoCredential credential = MongoCredential.createPlainCredential(username, "$external", password);credentials.add(credential);MongoClient mongoClient1 = new MongoClient(serverAddresses, credentials);DB db = mongoClient1.getDB(database);System.out.println(db.getCollectionNames());System.out.println("Done");} catch (UnknownHostException e) {e.printStackTrace();}
No comments:
Post a Comment