lock feature

Lock Feature

Version: 17.07

Supported Since: 17.01

Lock Feature

Lock Feature provides the ability to get a lock or a sequence number for given key. This feature has separate APIs to get a distributed lock as well as a ReentrantLock which must be acquire and release using the same thread within the JVM as per the java-docs of the ReentrantLock

In order to get a better understanding about each API, let’s go through all the APIs in the lock feature in detail.

APIs Java Docs

tryLock

Parameters
String lockPath

The lock path to be associated with the lock which is trying to acquire.

Returns
Boolean

This API will return "true" if it’s able to acquire a lock for the given "lockPath", otherwise returns "false".

This API will try to get a lock for the given "lockpath". If the clustering is enabled this API will try to get a distributed lock through out the cluster using the Cluster Feature and it can be used to achieve required synchronization and mutual exclusion between multiple nodes in a clustered deployment.

If clustering is disabled, this API will assume this is a single node deployment and it will try to get a Semaphore based lock for the given path. As per the java-docs of the Semaphore, it’s not a distributed lock and it’s only applicable to a single JVM which owns the lock.

releaseLock

Parameters
String lockPath

The lock path associated to the lock which is requested to release.

This API will release the lock which is acquired for the given "lockpath". Same as in the tryLock API, if it’s a clustered deployment with clustering enabled, this will release the acquired cluster lock. Otherwise, this will release the pre-acquired Semaphore based lock for the given "lockpath".

getNextSequenceNumber

Parameters
String path

The path to be associated to the sequence number which is requested to generate.

Returns
Optional<Integer>

This API will return an optional integer. Users have to check whether a value exists. If the optional has no value, that means that the sequence number generation has failed.

Generates a sequence number which is the next in line of a sequence associated to the given "lockpath". If it’s a clustered deployment with clustering enabled, Cluster Feature will be used to generate a distributed sequence number which is the next in line of a distributed sequence between all the nodes in the cluster.

If clustering is disabled, this API will assume this is a single node deployment and it will generate the sequence number which is only applicable to a single JVM which owns the sequence number.

tryReentrantLock

Parameters
String lockPath

The lock path to be associated with the ReentrantLock which is trying to acquire.

Returns
Boolean

This API will return "true" if it’s able to acquire a ReentrantLock for the given "lockPath", otherwise returns "false".

This API will try to get a ReentrantLock for the given "lockpath". If it’s able to get a ReentrantLock for the given path, it will returns true, otherwise it will return false.

As per the java-docs of the ReentrantLock, it’s expected to acquire and release the lock from the same thread. Because of that, tryReentrantLock API and releaseReentrantLock API should call from the same thread for a given "lockpath". Otherwise it will throw a IllegalMonitorStateException.

releaseReentrantLock

Parameters
String lockPath

The lock path associated to the ReentrantLock which is requested to release.

This API will release the ReentrantLock which is acquired for the given "lockpath". This method is expected to call from the same thread which is acquired the ReentrantLock for the given "lockpath".

In this topic
In this topic
Contact Us