|
3 | 3 | [](https://coveralls.io/github/coditory/sherlock-distributed-lock)
|
4 | 4 | [](https://search.maven.org/search?q=com.coditory.sherlock)
|
5 | 5 | [](http://www.javadoc.io/doc/com.coditory.sherlock/sherlock-api-sync)
|
6 |
| -[](https://gitter.im/coditory/sherlock-distributed-lock?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) |
7 | 6 |
|
8 |
| -## Distributed lock JVM library |
| 7 | +> Distributed lock library for JVM projects |
9 | 8 |
|
10 |
| -Sherlock Distributed Lock is a JVM library that provides distributed locking mechanism. |
11 |
| -It uses a database to store locks and exposes both synchronous and reactive API. |
12 |
| -It was created as a simple solution to manage distributed locks among multiple microservices. |
| 9 | +Look for details in the [documentation](https://coditory.github.io/sherlock-distributed-lock/). |
| 10 | + |
| 11 | +- **minimal dependency** - the main dependency is database driver that is probably already used in your project |
| 12 | +- **provides different [types of locks](https://coditory.github.io/sherlock-distributed-lock/locs)** - small but flexible |
| 13 | +- **provides [migration capabilities](https://coditory.github.io/sherlock-distributed-lock/migrator)** - no need for another library for database migration process |
| 14 | +- **exposes [synchronous and reactive API](https://coditory.github.io/sherlock-distributed-lock/api)** - changing API should not be a revolution |
| 15 | + |
| 16 | +## Sample usage |
| 17 | + |
| 18 | +Add dependency to `build.gradle`: |
| 19 | +```groovy |
| 20 | +dependencies { |
| 21 | + compile "com.coditory.sherlock:sherlock-mongo-sync:1.4.10" |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +Create synchronous MongoDB backed lock: |
| 26 | +```java |
| 27 | +// Get mongo locks collection |
| 28 | +MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017/sherlock"); |
| 29 | +MongoCollection<Document> collection = mongoClient |
| 30 | + .getDatabase("sherlock") |
| 31 | + .getCollection("locks"); |
| 32 | + |
| 33 | +// Create sherlock |
| 34 | +Sherlock sherlock = MongoSherlock.builder() |
| 35 | + .withLocksCollection(collection) |
| 36 | + .build(); |
| 37 | + |
| 38 | +// Create a lock |
| 39 | +DistributedLock lock = sherlock.createLock("sample-lock"); |
| 40 | + |
| 41 | +// Acquire a lock |
| 42 | +lock.acquireAndExecute(() -> { |
| 43 | + System.out.println("Lock granted!"); |
| 44 | +}); |
| 45 | +``` |
13 | 46 |
|
14 |
| -For details go to the [documentation](https://coditory.github.io/sherlock-distributed-lock/). |
|
0 commit comments