Do I need to shutdown a client? What are the repercussions of keeping a client open forever?
Earlier, I'd client as static member of the class and I was creating a client in constructor. The application run absolutely fine on HP Stackato Cloud Foundry. I was not calling shutdown on the client in this case.
However, as soon as I made it a member of method, and run it 7-8 times consecutively, I see tomcat shuts down and restarts the application.
Here is my newer code that runs into issues (after 7-8 consecutive runs). Hope you can let me know?
public void insertInBatchAsync(Map<String, Object> dbRecords) throws CustomException {
RedisURI redisUri = RedisURI.Builder.redis(ConfigPropertiesReader.getRedisEndpoint(), ConfigPropertiesReader.getRedisPort())
.withSsl(true).withPassword(ConfigPropertiesReader.getRedisCredentials()).build();
RedisClient client = RedisClient.create(redisUri);
StatefulRedisConnection<String, Object> connection = client.connect(new KryoCodec());
try {
RedisAsyncCommands<String, Object> asyncCommands = connection.async();
// Disable auto-flushing
connection.setAutoFlushCommands(false);
// Perform a series of independent calls
List<RedisFuture<?>> futures = new ArrayList<RedisFuture<?>>();
for (Map.Entry<String, Object> entry : dbRecords.entrySet()) {
if (null != entry) {
futures.add(asyncCommands.set(entry.getKey(), entry.getValue()));
}
}
// Write all commands to the transport layer
connection.flushCommands();
// Wait until all futures complete
boolean success = LettuceFutures.awaitAll(10, TimeUnit.SECONDS,
futures.toArray(new RedisFuture[futures.size()]));
if (!success) {
RedisFuture [] rf = (RedisFuture[]) futures.toArray(new RedisFuture[futures.size()]);
StringBuilder fails = new StringBuilder();
List<String> keys = new ArrayList<String>(dbRecords.keySet());
for (int i=0; i<rf.length; i++) {
AsyncCommand asyncCommand = (AsyncCommand)rf[i];
if (null != asyncCommand) {
StatusOutput so = (StatusOutput) asyncCommand.getOutput();
if (null != so) {
if (!"OK".equals(so.get())) {
String key = keys.get(i);
fails.append(key);
fails.append(", ");
}
}
}
}
// Check if there is any failures and throw an exception in case there are any.
if (fails.length() > 0) {
logger.error("++++++++ Failed to insert some or all objects into Redis cache ++++++++");
throw new CustomException(500, "Objects with following keys not inserted = " + fails.toString());
}
}
logger.info("======== Inserting Object objects in batch - Success = " + success + " ========");
} finally {
connection.close();
client.shutdown();
}
}