Skip to content

Instantly share code, notes, and snippets.

@suresk
Last active April 10, 2016 05:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suresk/19f16576ed7cecec6576db261f4659cc to your computer and use it in GitHub Desktop.
Save suresk/19f16576ed7cecec6576db261f4659cc to your computer and use it in GitHub Desktop.
Elasticsearch get distributed lock
private boolean getLock(String aliasName)
{
synchronized (aliasName)
{
IndexRequestBuilder indexRequest = elasticClient.prepareIndex(LOCK_DIRECTORY, LOCK_TYPE, aliasName).setSource("{}").setOpType(OpType.CREATE);
boolean successful = false;
try
{
IndexResponse response = indexRequest.get();
successful = response.isCreated();
}
catch(Exception e)
{
// This means we couldn't get the lock.
e.printStackTrace();
}
return successful;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment