Skip to content

Commit 3fca498

Browse files
committed
RedLock implementation in Java
0 parents  commit 3fca498

File tree

8 files changed

+549
-0
lines changed

8 files changed

+549
-0
lines changed

.github/workflows/maven.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Java CI with Maven
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
build:
11+
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- uses: actions/checkout@v2
16+
- name: Set up JDK 8
17+
uses: actions/setup-java@v2
18+
with:
19+
java-version: '8'
20+
distribution: 'adopt'
21+
- name: Redis Server in GitHub Actions
22+
uses: supercharge/redis-github-action@1.1.0
23+
- name: Build with Maven
24+
run: mvn -B package --file pom.xml

.gitignore

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Compiled class file
2+
*.class
3+
4+
# Log file
5+
*.log
6+
7+
# BlueJ files
8+
*.ctxt
9+
10+
# Mobile Tools for Java (J2ME)
11+
.mtj.tmp/
12+
13+
# Package Files #
14+
*.jar
15+
*.war
16+
*.nar
17+
*.ear
18+
*.zip
19+
*.tar.gz
20+
*.rar
21+
22+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23+
hs_err_pid*
24+
25+
# IntelliJ files and folders
26+
.idea
27+
*.iml
28+
29+
# Maven
30+
target

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Sathish
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# redlock-java
2+
RedLock in Java
3+
4+
Implementation based on [Redlock-rb](https://github.com/antirez/redlock-rb)
5+
6+
This Java library implements the Redis-based distributed lock manager algorithm [described in this blog post](http://antirez.com/news/77).
7+
8+
To create a lock manager:
9+
10+
```java
11+
RedLock redLock = new RedLock();
12+
```
13+
14+
To acquire a lock:
15+
16+
```java
17+
String lockResult = redLock.lock("redlock");
18+
```
19+
20+
To release a lock:
21+
22+
```java
23+
List<Object> releaseLockResult = redLock.release("redlock", lockResult);
24+
```

pom.xml

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>io.github.s-sathish</groupId>
6+
<artifactId>redlock-java</artifactId>
7+
<version>1.0</version>
8+
<name>RedLock-Java</name>
9+
<description>RedLock in Java</description>
10+
<url>https://github.com/s-sathish/redlock-java</url>
11+
12+
<developers>
13+
<developer>
14+
<id>s-sathish</id>
15+
<name>Sathish S</name>
16+
<email>sathish1293@gmail.com</email>
17+
</developer>
18+
</developers>
19+
20+
<licenses>
21+
<license>
22+
<name>MIT</name>
23+
<url>http://github.com/s-sathish/redlock-java/raw/master/LICENSE</url>
24+
<distribution>repo</distribution>
25+
</license>
26+
</licenses>
27+
28+
<scm>
29+
<connection>scm:git:git@github.com:s-sathish/redlock-java.git</connection>
30+
<url>scm:git:git@github.com:s-sathish/redlock-java.git</url>
31+
<developerConnection>scm:git:git@github.com:s-sathish/redlock-java.git</developerConnection>
32+
<tag>redlock-java-1.0</tag>
33+
</scm>
34+
35+
<properties>
36+
<jedis.version>3.6.0</jedis.version>
37+
<slf4j.version>1.7.30</slf4j.version>
38+
<junit.version>4.13.2</junit.version>
39+
<redlock.module.name>redis.lock.redlock</redlock.module.name>
40+
</properties>
41+
42+
<dependencies>
43+
<dependency>
44+
<groupId>redis.clients</groupId>
45+
<artifactId>jedis</artifactId>
46+
<version>${jedis.version}</version>
47+
<type>jar</type>
48+
<scope>compile</scope>
49+
</dependency>
50+
51+
<dependency>
52+
<groupId>org.slf4j</groupId>
53+
<artifactId>slf4j-simple</artifactId>
54+
<version>${slf4j.version}</version>
55+
<scope>test</scope>
56+
</dependency>
57+
58+
<dependency>
59+
<groupId>junit</groupId>
60+
<artifactId>junit</artifactId>
61+
<version>${junit.version}</version>
62+
<scope>test</scope>
63+
</dependency>
64+
</dependencies>
65+
66+
<distributionManagement>
67+
<snapshotRepository>
68+
<id>ossrh</id>
69+
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
70+
</snapshotRepository>
71+
<repository>
72+
<id>ossrh</id>
73+
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
74+
</repository>
75+
</distributionManagement>
76+
77+
<build>
78+
<plugins>
79+
<plugin>
80+
<groupId>org.apache.maven.plugins</groupId>
81+
<artifactId>maven-compiler-plugin</artifactId>
82+
<version>3.8.1</version>
83+
<configuration>
84+
<source>1.8</source>
85+
<target>1.8</target>
86+
</configuration>
87+
</plugin>
88+
<plugin>
89+
<groupId>org.apache.maven.plugins</groupId>
90+
<artifactId>maven-surefire-plugin</artifactId>
91+
<version>2.22.2</version>
92+
</plugin>
93+
<plugin>
94+
<groupId>org.apache.maven.plugins</groupId>
95+
<artifactId>maven-source-plugin</artifactId>
96+
<version>3.2.1</version>
97+
<configuration>
98+
<attach>true</attach>
99+
</configuration>
100+
<executions>
101+
<execution>
102+
<id>attach-sources</id>
103+
<goals>
104+
<goal>jar</goal>
105+
</goals>
106+
</execution>
107+
</executions>
108+
</plugin>
109+
<plugin>
110+
<groupId>org.apache.maven.plugins</groupId>
111+
<artifactId>maven-javadoc-plugin</artifactId>
112+
<version>3.2.0</version>
113+
<configuration>
114+
<doclint>none</doclint>
115+
</configuration>
116+
<executions>
117+
<execution>
118+
<id>attach-javadoc</id>
119+
<goals>
120+
<goal>aggregate</goal>
121+
<goal>jar</goal>
122+
</goals>
123+
</execution>
124+
</executions>
125+
</plugin>
126+
<plugin>
127+
<groupId>org.apache.maven.plugins</groupId>
128+
<artifactId>maven-release-plugin</artifactId>
129+
<version>2.5.3</version>
130+
</plugin>
131+
<plugin>
132+
<groupId>org.sonatype.plugins</groupId>
133+
<artifactId>nexus-staging-maven-plugin</artifactId>
134+
<version>1.6.8</version>
135+
<extensions>true</extensions>
136+
<configuration>
137+
<serverId>ossrh</serverId>
138+
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
139+
<autoReleaseAfterClose>true</autoReleaseAfterClose>
140+
</configuration>
141+
</plugin>
142+
<plugin>
143+
<groupId>org.apache.maven.plugins</groupId>
144+
<artifactId>maven-jar-plugin</artifactId>
145+
<version>3.2.0</version>
146+
<configuration>
147+
<archive>
148+
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
149+
<manifestEntries>
150+
<Automatic-Module-Name>${redlock.module.name}</Automatic-Module-Name>
151+
</manifestEntries>
152+
</archive>
153+
</configuration>
154+
</plugin>
155+
<plugin>
156+
<groupId>org.apache.felix</groupId>
157+
<artifactId>maven-bundle-plugin</artifactId>
158+
<version>4.2.1</version>
159+
<executions>
160+
<execution>
161+
<id>bundle-manifest</id>
162+
<phase>process-classes</phase>
163+
<goals>
164+
<goal>manifest</goal>
165+
</goals>
166+
</execution>
167+
</executions>
168+
</plugin>
169+
</plugins>
170+
</build>
171+
172+
<profiles>
173+
<profile>
174+
<id>release</id>
175+
<build>
176+
<plugins>
177+
<plugin>
178+
<groupId>org.apache.maven.plugins</groupId>
179+
<artifactId>maven-gpg-plugin</artifactId>
180+
<version>1.6</version>
181+
<executions>
182+
<execution>
183+
<id>sign-artifacts</id>
184+
<phase>verify</phase>
185+
<goals>
186+
<goal>sign</goal>
187+
</goals>
188+
</execution>
189+
</executions>
190+
</plugin>
191+
</plugins>
192+
</build>
193+
</profile>
194+
</profiles>
195+
196+
</project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package redis.lock.redlock;
2+
3+
public class LockResult {
4+
5+
private final String resource;
6+
private final String value;
7+
private final double validity;
8+
9+
public LockResult(String resource, String value, double validityTime) {
10+
this.resource = resource;
11+
this.value = value;
12+
this.validity = validityTime;
13+
}
14+
15+
public String getResource() {
16+
return resource;
17+
}
18+
19+
public String getValue() {
20+
return value;
21+
}
22+
23+
public double getValidity() {
24+
return validity;
25+
}
26+
27+
}

0 commit comments

Comments
 (0)