diff --git a/pom.xml b/pom.xml index f0c76a2b0..cadf36b4b 100644 --- a/pom.xml +++ b/pom.xml @@ -25,6 +25,7 @@ shedlock-spring shedlock-springboot-test shedlock-provider-mongo + shedlock-provider-redis-jedis shedlock-provider-jedis shedlock-test-support shedlock-test-support-jdbc diff --git a/shedlock-provider-jedis/pom.xml b/shedlock-provider-jedis/pom.xml index 7a44253bf..6f07f7465 100644 --- a/shedlock-provider-jedis/pom.xml +++ b/shedlock-provider-jedis/pom.xml @@ -13,24 +13,15 @@ net.javacrumbs.shedlock - shedlock-core + shedlock-provider-redis-jedis ${project.version} - - - redis.clients - jedis - 2.9.0 - - net.javacrumbs.shedlock shedlock-test-support ${project.version} test - - com.github.kstyrc embedded-redis diff --git a/shedlock-provider-jedis/src/main/java/net/javacrumbs/shedlock/provider/jedis/JedisLockProvider.java b/shedlock-provider-jedis/src/main/java/net/javacrumbs/shedlock/provider/jedis/JedisLockProvider.java index 43db5696a..d1bef5078 100644 --- a/shedlock-provider-jedis/src/main/java/net/javacrumbs/shedlock/provider/jedis/JedisLockProvider.java +++ b/shedlock-provider-jedis/src/main/java/net/javacrumbs/shedlock/provider/jedis/JedisLockProvider.java @@ -15,118 +15,26 @@ */ package net.javacrumbs.shedlock.provider.jedis; -import net.javacrumbs.shedlock.core.LockConfiguration; -import net.javacrumbs.shedlock.core.LockProvider; -import net.javacrumbs.shedlock.core.SimpleLock; -import net.javacrumbs.shedlock.support.LockException; import redis.clients.jedis.Jedis; import redis.clients.util.Pool; -import java.net.InetAddress; -import java.net.UnknownHostException; -import java.time.Duration; -import java.time.Instant; -import java.util.Optional; - /** - * Uses Redis's `SET resource-name anystring NX PX max-lock-ms-time` as locking mechanism. - * - * See https://redis.io/commands/set - * + * @deprecated Please use net.javacrumbs.shedlock.provider.redis.jedis.JedisLockProvider in artifact shedlock-provider-redis-jedis */ -public class JedisLockProvider implements LockProvider { +@Deprecated +public class JedisLockProvider extends net.javacrumbs.shedlock.provider.redis.jedis.JedisLockProvider { private static final String KEY_PREFIX = "job-lock"; - private static final String ENV_DEFAULT = "default"; - - // Redis Flags - private static final String SET_IF_NOT_EXIST = "NX"; - private static final String SET_IF_EXIST = "XX"; - private static final String SET_EXPIRE_TIME_IN_MS = "PX"; - - private Pool jedisPool; - private String environment; public JedisLockProvider(Pool jedisPool) { - this(jedisPool, ENV_DEFAULT); + super(jedisPool); } public JedisLockProvider(Pool jedisPool, String environment) { - this.jedisPool = jedisPool; - this.environment = environment; - } - - @Override - public Optional lock(LockConfiguration lockConfiguration) { - long expireTime = getMsUntil(lockConfiguration.getLockAtMostUntil()); - - String key = buildKey(lockConfiguration.getName(), this.environment); - - try (Jedis jedis = jedisPool.getResource()) { - String rez = jedis.set(key, - buildValue(), - SET_IF_NOT_EXIST, - SET_EXPIRE_TIME_IN_MS, - expireTime); - - if (rez != null && "OK".equals(rez)) { - return Optional.of(new RedisLock(key, jedisPool, lockConfiguration)); - } - } - return Optional.empty(); - } - - private static final class RedisLock implements SimpleLock { - private final String key; - private final Pool jedisPool; - private final LockConfiguration lockConfiguration; - - private RedisLock(String key, Pool jedisPool, LockConfiguration lockConfiguration) { - this.key = key; - this.jedisPool = jedisPool; - this.lockConfiguration = lockConfiguration; - } - - @Override - public void unlock() { - long keepLockFor = getMsUntil(lockConfiguration.getLockAtLeastUntil()); - - // lock at least until is in the past - if (keepLockFor <= 0) { - try (Jedis jedis = jedisPool.getResource()) { - jedis.del(key); - } catch (Exception e) { - throw new LockException("Can not remove node", e); - } - } else { - try (Jedis jedis = jedisPool.getResource()) { - jedis.set(key, - buildValue(), - SET_IF_EXIST, - SET_EXPIRE_TIME_IN_MS, - keepLockFor); - } - } - } - } - - private static long getMsUntil(Instant instant) { - return Duration.between(Instant.now(), instant).toMillis(); - } - - private static String getHostname() { - try { - return InetAddress.getLocalHost().getHostName(); - } catch (UnknownHostException e) { - return "unknown host"; - } + super(jedisPool, environment); } static String buildKey(String lockName, String env) { return String.format("%s:%s:%s", KEY_PREFIX, env, lockName); } - - private static String buildValue() { - return String.format("ADDED:%s@%s", Instant.now().toString(),getHostname()); - } }