Thursday, November 22, 2018

MongoClient with SocketTimeOut

 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;
    } 

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);




Thursday, November 8, 2018

Track changes in Word

Track changes in Word

Turning on Track Changes gives you and your coworkers a way to make changes that are easy to spot. The changes are like suggestions that you can review, and then remove them or make them permanent.
Turn Track Changes on and off by going to Review > Track Changes.
Track changes
  • When it's turned on, deletions are marked with a strikethrough, and additions are marked with an underline. Different authors' changes are indicated with different colors.
  • When it's turned off, Word stops marking changes, but the colored underlines and strikethrough are still in the document.

View suggested changes

To review the changes in your document, go to Review Tracking Display for Review.

Load JS File dynamically from JSP file.

function loadJSfile(language) {
var fileref = document.createElement('script')
fileref.setAttribute("type", "text/javascript");
var theURL = window.location.href;
var arr = theURL.split("/");
var result = arr[0] + "//" + arr[2] + "/" + arr[3];
var thePath ="/resources/json_messages/messageJson_"+language+".js";
var theMessageFileURL = result + thePath;
$.ajax({
url : theMessageFileURL,
type : 'HEAD',
async : false,
error : function() {
//language message file not exists
loadJSfile("en");
},
success : function() {
//language message file exists
fileref.setAttribute("src", theMessageFileURL)
if (typeof fileref != "undefined")
document.getElementsByTagName("head")[0]
.appendChild(fileref)
}
});
}

loadJSfile("${pageContext.response.locale}"); //dynamically load and add this .js file

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...