Thursday, November 22, 2018

Stoping MongoTemplate after completion of Operations

Step 1 : 

import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;

public class CustomMongoTemplate extends MongoTemplate {

public CustomMongoTemplate(MongoDbFactory mongoDbFactory) {
super(mongoDbFactory);
}

@Override
protected void finalize() throws Throwable {
this.getDb().getMongo().close();
}
}



Step 2:

  public static MongoClient getMongoInstance() {
        if (_mongoClient == null) {
            try {
            MongoCredential credential = MongoCredential.createCredential(getMongoUser(), getAuthDatabase(), MONGO_PWD.toCharArray());
                ServerAddress serverAddress = new ServerAddress(getMongoDBServerName(), getMongoDBServerPort());
                MongoClientOptions.Builder options = new MongoClientOptions.Builder();
            _mongoClient = new MongoClient(serverAddress, credential, options.build());
            } catch (Exception e) {
                log.error("MongoDBClient::getMongoInstance Exception --> " + e.getLocalizedMessage());
                log.info("Exception : ", e);
            }
        }

        return _mongoClient;
    }
======================================================
 public static MongoClient getMongoInstance(int timeOut) {
    MongoClient myMongoClient = null;
    if(timeOut==-1) {
        return getMongoInstance();
        }else {
                try {
                MongoCredential credential = MongoCredential.createCredential(getMongoUser(), getAuthDatabase(), MONGO_PWD.toCharArray());
                MongoClientOptions.Builder options = new MongoClientOptions.Builder();
                options.socketTimeout(timeOut).connectTimeout(timeOut).maxWaitTime(timeOut);
               
                    ServerAddress serverAddress = new ServerAddress(getMongoDBServerName(), getMongoDBServerPort());
                    myMongoClient = new MongoClient(serverAddress, Arrays.asList(credential),options.build());
                } catch (Exception e) {
                  log.error("MongoDBClient::getMongoInstance Exception --> " + e.getLocalizedMessage());
                      log.info("Exception : ", e);
                }
        }
        return myMongoClient;
    } 
======================================================
public MongoDbFactory mongoDbFactory(String dbName, int timeOut) {
return new SimpleMongoDbFactory(MongoDBClient.getMongoInstance(timeOut), "my_" + dbName);
}
========================================================
public MongoTemplate mongoTemplate(String dbName, int tomeOut) {
if (dbName == null || dbName.isEmpty())
return null;
return new CustomMongoTemplate(mongoDbFactory(dbName.replace(" ", "_"), tomeOut));
}
=====================================================
Step 3 : Usage -->

someClass.mongoTemplate("dbName", 23);




No comments:

Post a Comment

Recent Post

Databricks Delta table merge Example

here's some sample code that demonstrates a merge operation on a Delta table using PySpark:   from pyspark.sql import SparkSession # cre...