Created
October 9, 2019 10:00
-
-
Save bmaggi/e2e416cf342421f1c553c950d378fefa to your computer and use it in GitHub Desktop.
a simple way to lock key in Redis using Redisson
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.redisson.Redisson; | |
import org.redisson.api.RLock; | |
import org.redisson.api.RedissonClient; | |
import org.redisson.config.Config; | |
import java.util.concurrent.TimeUnit; | |
class RedissonManualLock { | |
// a simple way to lock key in Redis using Redisson | |
public static void main(String[] args) throws InterruptedException { | |
String lockName = "my-lock"; | |
final String redisAdress = "redis://my-redis.com:6379"; | |
Config config = new Config(); | |
config.useSingleServer().setAddress(redisAdress); | |
final RedissonClient redissonClient = Redisson.create(config); | |
final RLock lock = redissonClient.getLock(lockName); | |
final boolean locked = lock.tryLock(1, TimeUnit.SECONDS); | |
System.out.println("Locked? "+locked); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment