Skip to content
/ dlock-1 Public
forked from yusufaytas/dlock

Interval Lock

License

Apache-2.0, Unknown licenses found

Licenses found

Apache-2.0
LICENSE
Unknown
license-header.txt
Notifications You must be signed in to change notification settings

Viyond/dlock-1

 
 

Repository files navigation

DLock

Apache License 2 Build Status

DLock is a library to get an interval lock. An Interval Lock is a lock where the acquirer acquires the lock for a certain amount of time. Once the lock is acquired, it won't be released till the interval ends. Note that, each of the Interval Lock implementations guarantees atomicity over processes and threads. The lock can only be acquired by only one process or thread at a time as expected. The implementations differ based on the technology.

Feedback and pull-requests are welcome!

Usage

Requirements and dependencies

  • Java 8
  • slf4j-api

Note that, there should be only one global lock.

Import project

maven

<dependency>
    <groupId>com.yusufaytas</groupId>
    <artifactId>dlock-spring</artifactId>
    <version>0.2.1</version>
</dependency>
<dependency>
    <groupId>com.yusufaytas</groupId>
    <artifactId>dlock-jdbc</artifactId>
    <version>0.2.1</version>
</dependency>

or you can import all

<dependency>
    <groupId>com.yusufaytas</groupId>
    <artifactId>dlock-all</artifactId>
    <version>0.2.1</version>
</dependency>

gradle

compile 'com.yusufaytas:dlock-spring:0.2.1'
compile 'com.yusufaytas:dlock-jdbc:0.2.1'

or you can import all

compile 'com.yusufaytas:dlock-all:0.2.1'

Add an Interval Lock Support

Spring Bean Config

An example lock support for Postgres can be added as follows

<!-- A bean for the lock implementation. Note that there should be only one global implementation-->
<bean id="postgresLock" class="com.yusufaytas.dlock.jdbc.PostgresIntervalLock">
  <constructor-arg type="javax.sql.DataSource" ref="lockDataSource"/>
</bean>
<!-- The lock gets auto-registered to the registrar -->
<bean id="lockRegistrar" class="com.yusufaytas.dlock.spring.SpringLockRegistrar"/>

Java Code

@Scheduled(cron = "*/1 * * * * *")
@TryLock(name = "example", owner = "dlock", lockFor = ONE_MINUTE)
public void exampleLock() {
  System.out.println("lock works");
}

Lock Implementations

Jdbc

You need to execute the DDL at the target database with appropriate permissions to make the lock work.

Postgres

We insert into postgres if there doesn't exist a lock. Please checkout the Postgres DDL.

MySQL

We get an exclusive lock on the lock table and insert a new lock if a conflicting lock doesn't exit. Please checkout the MySQL DDL.

Contributing

Compiling project

./mvnw clean install -Dgpg.skip

License Header

./mvnw license:format -Dgpg.skip

About

Interval Lock

Resources

License

Apache-2.0, Unknown licenses found

Licenses found

Apache-2.0
LICENSE
Unknown
license-header.txt

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%