这是indexloc提供的服务,不要输入任何密码
Skip to content

yusufaytas/dlock

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.

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.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>

gradle

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'

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.IntervalLockRegistrar"/>

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

mvn clean install -Dgpg.skip

About

Interval Lock

Topics

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

Contributors 3

  •  
  •  
  •  

Languages