DLock is a library to get a distributed 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.
Feedback and pull-requests are welcome!
- Java 8
- slf4j-api
Note that, there should be only one global lock.
<dependency>
<groupId>com.yusufaytas</groupId>
<artifactId>dlock-spring</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>com.yusufaytas</groupId>
<artifactId>dlock-jdbc</artifactId>
<version>0.1</version>
</dependency>or you can import all
<dependency>
<groupId>com.yusufaytas</groupId>
<artifactId>dlock-all</artifactId>
<version>0.1</version>
</dependency>compile 'com.yusufaytas:dlock-spring:0.1'
compile 'com.yusufaytas:dlock-jdbc:0.1'or you can import all
compile 'com.yusufaytas:dlock-all:0.1'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.IntervalLockRegistrar"/>@Scheduled(cron = "*/1 * * * * *")
@TryLock(name = "example", owner = "dlock", lockFor = ONE_MINUTE)
public void exampleLock() {
System.out.println("lock works");
}You need to execute the DDL at the target database with appropriate permissions to make lock work.
We insert into postgres if there doesn't exist a lock. Please checkout the Postgres DDL.
We get an exclusive lock on the lock table and insert a new lock if it a conflicting lock doesn't exit. Please checkout the MySQL DDL.
mvn clean install -Dgpg.skip