https://github.com/snower/jaslock
slock java client
https://github.com/snower/jaslock
Last synced: about 1 month ago
JSON representation
slock java client
- Host: GitHub
- URL: https://github.com/snower/jaslock
- Owner: snower
- License: mit
- Created: 2021-12-02T11:10:06.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2025-09-08T08:44:45.000Z (9 months ago)
- Last Synced: 2025-09-08T10:33:32.563Z (9 months ago)
- Language: Java
- Size: 223 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# jaslock
[](https://github.com/snower/jaslock/actions/workflows/build-test.yml)
[](https://github.com/snower/jaslock/stargazers)
High-performance distributed sync service and atomic DB. Provides good multi-core support through lock queues, high-performance asynchronous binary network protocols. Can be used for spikes, synchronization, event notification, concurrency control. https://github.com/snower/slock
# Install
```xml
io.github.snower
jaslock
1.1.3
```
# Client Lock
```java
package main;
import io.github.snower.jaslock.SlockClient;
import io.github.snower.jaslock.Event;
import io.github.snower.jaslock.Lock;
import io.github.snower.jaslock.SlockReplsetClient;
import io.github.snower.jaslock.SlockClient;
import io.github.snower.jaslock.exceptions.SlockException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class App {
public static void main(String[] args) {
SlockClient client = new SlockClient("127.0.0.1", 5658);
try {
client.open();
Lock lock = client.newLock("test", 5, 5);
lock.acquire();
lock.release();
} catch (IOException | SlockException e) {
e.printStackTrace();
} finally {
client.close();
}
}
}
```
# Replset Client Lock
```java
package main;
import io.github.snower.jaslock.SlockClient;
import io.github.snower.jaslock.Event;
import io.github.snower.jaslock.Lock;
import io.github.snower.jaslock.SlockReplsetClient;
import io.github.snower.jaslock.exceptions.SlockException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class App {
public static void main(String[] args) {
SlockReplsetClient replsetClient = new SlockReplsetClient(new String[]{"127.0.0.1:5658"});
try {
replsetClient.open();
Lock lock = replsetClient.newLock("test", 5, 5);
lock.acquire();
lock.release();
} catch (SlockException e) {
e.printStackTrace();
} finally {
replsetClient.close();
}
}
}
```
# Async Callback Lock
```java
package main;
import io.github.snower.jaslock.SlockClient;
import io.github.snower.jaslock.Event;
import io.github.snower.jaslock.Lock;
import io.github.snower.jaslock.SlockReplsetClient;
import io.github.snower.jaslock.SlockClient;
import io.github.snower.jaslock.exceptions.SlockException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class App {
public static void main(String[] args) {
SlockClient client = new SlockClient("127.0.0.1", 5658);
client.enableAsyncCallback();
try {
client.open();
Lock lock = client.newLock("test", 5, 5);
lock.acquire(callbackFuture -> {
try {
callbackFuture.getResult();
lock.release(callbackFuture1 -> {
try {
callbackFuture1.getResult();
System.out.println("succed");
} catch (IOException | SlockException e) {
e.printStackTrace();
} finally {
client.close();
}
});
} catch (IOException | SlockException e) {
e.printStackTrace();
client.close();
}
});
} catch (IOException | SlockException e) {
e.printStackTrace();
client.close();
}
try {
Thread.sleep(2000);
} catch (InterruptedException ignored1) {}
}
}
```
# Async Future Lock
```java
package main;
import io.github.snower.jaslock.SlockClient;
import io.github.snower.jaslock.Event;
import io.github.snower.jaslock.Lock;
import io.github.snower.jaslock.SlockReplsetClient;
import io.github.snower.jaslock.SlockClient;
import io.github.snower.jaslock.exceptions.SlockException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.ExecutionException;
public class App {
public static void main(String[] args) {
SlockReplsetClient replsetClient = new SlockReplsetClient(new String[]{"127.0.0.1:5658"});
replsetClient.enableAsyncCallback();
try {
replsetClient.open();
Lock lock = replsetClient.newLock("test", 5, 5);
CallbackFuture callbackFuture = lock.acquire(null);
callbackFuture.get();
callbackFuture = lock.release(null);
callbackFuture.get();
} catch (IOException | SlockException | ExecutionException | InterruptedException e) {
e.printStackTrace();
} finally {
client.close();
}
}
}
```
# Event
```java
package main;
import io.github.snower.jaslock.SlockClient;
import io.github.snower.jaslock.Event;
import io.github.snower.jaslock.Lock;
import io.github.snower.jaslock.SlockReplsetClient;
import io.github.snower.jaslock.SlockReplsetClient;
import io.github.snower.jaslock.exceptions.SlockException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class App {
public static void main(String[] args) {
SlockReplsetClient replsetClient = new SlockReplsetClient(new String[]{"127.0.0.1:5658"});
try {
replsetClient.open();
Event event1 = replsetClient.newEvent("test", 5, 5, true);
event1.clear();
Event event2 = replsetClient.newEvent("test", 5, 5, true);
event2.set("{\"value\": 10}");
event1.wait(10);
System.out.println(event1.getCurrentLockDataAsString());
} catch (SlockException e) {
e.printStackTrace();
} finally {
replsetClient.close();
}
}
}
```
# License
slock uses the MIT license, see LICENSE file for the details.