Skip to content

Instantly share code, notes, and snippets.

View Udith's full-sized avatar

Udith Gunaratna Udith

View GitHub Profile
let AWS = require('aws-sdk');
const s3 = new AWS.S3();
exports.handler = function (event, context, callback) {
console.log(`Batch process triggered at ${event.time}`);
s3.listObjects({
'Bucket': 'batch-process-bucket-udith',
'MaxKeys': 100,
'Prefix': ''
let AWS = require('aws-sdk');
const sns = new AWS.SNS();
const s3 = new AWS.S3();
const S3_BUCKET_NAME = 'batch-process-bucket-udith';
const SNS_TOPIC_ARN = 'arn:aws:sns:us-east-1:<YOUR_ACCOUNT_ID>:BatchProcessNotifications';
/*
This is the main function of the lambda
*/
@Override
public boolean add(Runnable runnable) {
return super.offer(runnable);
}
public class ReEnqueuePolicy implements RejectedExecutionHandler {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
if (!executor.getQueue().add(r)) {
rejectedExecutionHandler.rejectedExecution(r, executor);
}
}
}
@Override
public Runnable poll(long timeout, TimeUnit unit) throws InterruptedException {
currentIdleThreadCount.incrementAndGet();
Runnable poll = super.poll(timeout, unit);
currentIdleThreadCount.decrementAndGet();
return poll;
}
@Override
public Runnable take() throws InterruptedException {
currentIdleThreadCount.incrementAndGet();
Runnable take = super.take();
currentIdleThreadCount.decrementAndGet();
return take;
}
@Udith
Udith / ThreadPoolExecutor_2.java
Created October 9, 2017 07:48
An extract from getTask() method of Java's java.util.concurrent.ThreadPoolExecutor class
Runnable r = timed ?
workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) : workQueue.take();
@Override
public boolean offer(Runnable e) {
return currentIdleThreadCount.get() > 0 && super.offer(e);
}
@Udith
Udith / ThreadPoolExecutor_1.java
Created October 8, 2017 20:33
An extract from execute() method of Java's java.util.concurrent.ThreadPoolExecutor class
int c = ctl.get();
if (workerCountOf(c) < corePoolSize) {
if (addWorker(command, true))
return;
c = ctl.get();
}
if (isRunning(c) && workQueue.offer(command)) {
int recheck = ctl.get();
if (! isRunning(recheck) && remove(command))
reject(command);
private void optimizeIndex() {
OptimizeResponse optimizeResponse = elasticsearch.admin().indices()
.prepareOptimize(ES_INDEX)
.setFlush(true)
.setOnlyExpungeDeletes(false)
.execute().actionGet();
System.out.println("Elasticsearch index optimization finished with " +
optimizeResponse.getSuccessfulShards() + " successful and " +
optimizeResponse.getFailedShards() + " failed shards out of " +