GithubHelp home page GithubHelp logo

dlock's Introduction

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

dlock's People

Contributors

dependabot[bot] avatar riggs333 avatar yusufaytas avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

dlock's Issues

LockRegistry never returns Optional#empty

Hi,
thanks for providing this library.

Looking at the source I wonder why the methods in com.yusufaytas.dlock.core.LockRegistry (setLock and getLock) have Optional as return type.

If setLock is never called then the call to getLock will throw a NullPointerException:

java.lang.NullPointerException
	at java.base/java.util.Objects.requireNonNull(Objects.java:221)
	at java.base/java.util.Optional.<init>(Optional.java:107)
	at java.base/java.util.Optional.of(Optional.java:120)
	at com.yusufaytas.dlock.core.LockRegistry.getLock(LockRegistry.java:37)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.