Giter VIP home page Giter VIP logo

daggerok / distributed-lock-mongodb-spring-boot-starter Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 229 KB

This repository demonstrates how to implement custom written Distributed Lock starter based on Spring Boot and MongoTemplate with Testcontainers integration testing and fabric8 docker-maven-plugin

License: MIT License

Java 85.75% Shell 14.25%
github-actions java maven mongo mongodb mongodb-database mongotemplate spring-boot spring-boot-2 testcontainers

distributed-lock-mongodb-spring-boot-starter's Introduction

MongoDB distributed lock tests integration tests

A distributed-lock-mongodb-spring-boot-starter repository project contains custom written Distributed Lock starter based on Spring Boot and MongoTemplate with Testcontainers integration testing and fabric8 docker-maven-plugin maven module to help run example showcase application uses mongo docker container

Maven Central

Versions convention

We support next version convention:

Distributed Lock version Spring Boot version GitHub branch
3.0.6 3.0.6 master
2.7.11 2.7.11 spring-boot-2.7.x
2.6.14-4 2.6.14 spring-boot-2.6.x
2.5.14-4 2.5.14 spring-boot-2.5.x
2.4.13-4 2.4.13 spring-boot-2.4.x
2.3.12-4 2.3.12.RELEASE spring-boot-2.3.x
2.2.13-4 2.2.13.RELEASE spring-boot-2.2.x
2.1.18-4 2.1.18.RELEASE spring-boot-2.1.x

Installation

<dependency>
    <groupId>io.github.daggerok</groupId>
    <artifactId>distributed-lock-mongodb-spring-boot-starter</artifactId>
    <version>RELEASE</version>
</dependency>

Or:

<dependency>
    <groupId>io.github.daggerok</groupId>
    <artifactId>distributed-lock-mongodb-spring-boot-starter</artifactId>
    <version>LATEST</version>
</dependency>

...if you need SNAPSHOT

Supported operations

  • Acquire lock (try lock)
  • Release lock (try to unlock)
  • Acquire lock and consume (only if lock was acquired)
  • Acquire lock and supply value (only if lock was acquired)

Usage

Inject distributed lock

A DistributedLockAutoConfiguration will provide DistributedLock you can use as locking implementation.

Feel free to simply inject it by defined single constructor in your spring Bean:

@Service
public class MyService {

    @Autowired
    private MyService myService;

    // skipped...
}

Or you can inject it using @Auqtowired annotation by field injection (not recommended):

@Service
public class MyService {

    private final MyService myService;

    public MyService(MyService myService) {
        this.myService = myService;
    }

    // skipped...
}

Or query it from ApplicationContext directly:

@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) {
        var context = SpringApplication.run(ExampleApplication.class, args);
        var distributedLock = context.getBean(DistributedLock.class);
        // skipped...
    }
}

acquire(Lock config)

Acquire lock (try lock)

Optional<Lock> maybeLock = distributedLock.acquire(Lock.of(identifier));
if (maybeLock.isPresent()) log.debug("Lock was acquired.");
else log.warn("Lock can't be acquired...");

release(String lockId)

Release lock (try to unlock)

Optional<Lock> maybeUnlock = distributedLock.release("642b52e873d0ec7cd4463f05")
if (maybeUnlock.isPresent()) log.debug("Lock was released.");
else log.warn("Can't release lock...");

acquireAndRun

Acquire lock and consume (only if lock was acquired)

Optional<Boolean> maybeSync = distributedLock.acquireAndGet(Lock.of("sync"), () -> leaderElection.sync());
if (maybeSync.isPresent()) log.debug("Data synchronization was completed")
else log.warn("Data wasn't synced");

acquireAndGet

Acquire lock and supply value (only if lock was acquired)

Optional<SyncResult> syncResult = distributedLock.acquireAndGet(Lock.of("ETL"), () -> syncService.etl());
maybeResult.ifPresent(result -> log.debug("Synchronization complete with: {}", result));

distributed-lock-mongodb-spring-boot-starter's People

Stargazers

 avatar  avatar

Watchers

 avatar

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.