lock-provider

Distributed locks made easy

License

License

Categories

Categories

IDE Development Tools
GroupId

GroupId

com.inomera.telco.commons
ArtifactId

ArtifactId

lock-provider
Last Version

Last Version

1.4.0
Release Date

Release Date

Type

Type

jar
Description

Description

lock-provider
Distributed locks made easy
Project URL

Project URL

https://github.com/inomera/lock-provider
Project Organization

Project Organization

Inomera Research
Source Code Management

Source Code Management

https://github.com/inomera/lock-provider

Download lock-provider

How to add to project

<!-- https://jarcasting.com/artifacts/com.inomera.telco.commons/lock-provider/ -->
<dependency>
    <groupId>com.inomera.telco.commons</groupId>
    <artifactId>lock-provider</artifactId>
    <version>1.4.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.inomera.telco.commons/lock-provider/
implementation 'com.inomera.telco.commons:lock-provider:1.4.0'
// https://jarcasting.com/artifacts/com.inomera.telco.commons/lock-provider/
implementation ("com.inomera.telco.commons:lock-provider:1.4.0")
'com.inomera.telco.commons:lock-provider:jar:1.4.0'
<dependency org="com.inomera.telco.commons" name="lock-provider" rev="1.4.0">
  <artifact name="lock-provider" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.inomera.telco.commons', module='lock-provider', version='1.4.0')
)
libraryDependencies += "com.inomera.telco.commons" % "lock-provider" % "1.4.0"
[com.inomera.telco.commons/lock-provider "1.4.0"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

Lock Provider

Build Maven Central

Usage

With Maven

<dependency>
  <groupId>com.inomera.telco.commons</groupId>
  <artifactId>lock-provider</artifactId>
  <version>1.4.0</version>
</dependency>

With Gradle

implementation 'com.inomera.telco.commons:lock-provider:1.4.0'

Create an Instance

With Hazelcast

final LockProvider lockProvider = new HazelcastLockProvider(hazelcastInstance);

With Redis

final LockProvider lockProvider = new RedisLockProvider(redissonClient);

With Zookeeper

final LockProvider lockProvider = new ZookeeperLockProvider(curatorClient);

Non-distributed Lock Provider

final LockProvider lockProvider = new LocalReentrantLockProvider();

Optimistic Lock

Default Lock Map - Manual Unlock

Returns empty optional if lock is not acquired.

final Optional<Locked> maybeLocked = lockProvider.tryLock("lockKey");
maybeLocked.ifPresent(locked -> {
  try {
    // Do things in lock
  } finally {
    locked.unlock();
  }
});

Custom Lock Map - Manual Unlock

Returns empty optional if lock is not acquired.

final Optional<Locked> maybeLocked = lockProvider.tryLock("lockMapName", "lockKey");
maybeLocked.ifPresent(locked -> {
  try {
    // Do things in lock
  } finally {
    locked.unlock();
  }
});

Default Lock Map - Auto Unlock

lockProvider.executeInTryLock("lockKey", () -> {
  // Do stuff in lock
});

Custom Lock Map - Auto Unlock

lockProvider.executeInTryLock("lockMapName", "lockKey", () -> {
  // Do stuff in lock
});

Default Lock Map - Auto Unlock - Return Value

Returns null if lock is not acquired.

final String result = lockProvider.executeInTryLock("lockKey", () -> {
  // Do stuff in lock
  return "result";
});

Custom Lock Map - Auto Unlock - Return Value

Returns null if lock is not acquired.

final String result = lockProvider.executeInTryLock("lockMapName", "lockKey", () -> {
  // Do stuff in lock
  return "result";
});

Pesimistic Lock

Default Lock Map - Manual Unlock

final Locked locked = lockProvider.lock("lockKey");
try {
  // Do stuff in lock
} finally {
  locked.unlock();
}

Custom Lock Map - Manual Unlock

final Locked locked = lockProvider.lock("lockMapName", "lockKey");
try {
  // Do stuff in lock
} finally {
  locked.unlock();
}

Default Lock Map - Auto Unlock

lockProvider.executeInLock("lockKey", () -> {
  // Do stuff in lock
});

Custom Lock Map - Auto Unlock

lockProvider.executeInLock("lockMapName", "lockKey", () -> {
  // Do stuff in lock
});

Default Lock Map - Auto Unlock - Return Value

final String result = lockProvider.executeInLock("lockKey", () -> {
  // Do stuff in lock
  return "result";
});

Custom Lock Map - Auto Unlock - Return Value

final String result = lockProvider.executeInLock("lockMapName", "lockKey", () -> {
  // Do stuff in lock
  return "result";
});

Publishing

To publish a version to maven repository, you should create a gradle.properties file in the root directory of this project.

The file is: /path-to-project/gradle.properties

This file is included in .gitignore file. You should not commit it since it contains sensitive information.

Add credentials for maven repository to gradle.properties file.

Example gradle.properties file:

mavenReleaseUrl=https://oss.sonatype.org/service/local/staging/deploy/maven2/
mavenSnapshotUrl=https://oss.sonatype.org/content/repositories/snapshots/
mavenUsername=************************
mavenPassword=************************
mavenPackageGroup=com.inomera

signing.keyId=******
signing.password=******
signing.secretKeyRingFile=******.gpg

Then you need to invoke release.sh script in the project root directory.

# When the latest VERSION is 1.1.1
./release.sh patch
# New version is 1.1.2

./release.sh minor
# New version is 1.2.0

./release.sh major
# New version is 2.0.0

To publish a snapshot release, use --snapshot flag as follows:

./release.sh latest --snapshot

Please change the version wisely.

Redisson client, why we choose?

Redisson is more appropriate for our interface and also more mature according to the Jedis.

redisson github : https://github.com/redisson

CuratorFramework client, why we choose?

CuratorFramework is more appropriate for our interface and also more mature according to the Zookeper Java client.

curator github : https://github.com/apache/curator

com.inomera.telco.commons

Inomera Research

Versions

Version
1.4.0
1.3.0
1.2.0