修改启动es集群启动
This commit is contained in:
@@ -94,9 +94,10 @@ public class ESJobSearchImpl implements IESJobSearchService
|
|||||||
private boolean acquireDistributedLock() throws InterruptedException {
|
private boolean acquireDistributedLock() throws InterruptedException {
|
||||||
long start = System.currentTimeMillis();
|
long start = System.currentTimeMillis();
|
||||||
while (System.currentTimeMillis() - start < WAIT_LOCK_SECONDS * 1000) {
|
while (System.currentTimeMillis() - start < WAIT_LOCK_SECONDS * 1000) {
|
||||||
boolean lockExists = redisCache.hasKey(ES_INIT_LOCK_KEY);
|
boolean success = redisCache.setIfAbsent(ES_INIT_LOCK_KEY,"es_init_locked",
|
||||||
if (!lockExists) {
|
LOCK_EXPIRE_SECONDS,TimeUnit.SECONDS
|
||||||
redisCache.setCacheObject(ES_INIT_LOCK_KEY, "es_init_locked", LOCK_EXPIRE_SECONDS, TimeUnit.SECONDS);
|
);
|
||||||
|
if (success) {
|
||||||
logger.info("成功获取ES初始化分布式锁,key:{}", ES_INIT_LOCK_KEY);
|
logger.info("成功获取ES初始化分布式锁,key:{}", ES_INIT_LOCK_KEY);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -265,4 +265,17 @@ public class RedisCache
|
|||||||
{
|
{
|
||||||
return redisTemplate.keys(pattern);
|
return redisTemplate.keys(pattern);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分布式锁:不存在则设置缓存(原子操作)
|
||||||
|
* @param key 缓存键
|
||||||
|
* @param value 缓存值
|
||||||
|
* @param timeout 过期时间
|
||||||
|
* @param unit 时间单位
|
||||||
|
* @return true=设置成功(获取锁),false=已存在(未获取锁)
|
||||||
|
*/
|
||||||
|
public boolean setIfAbsent(String key, Object value, long timeout, TimeUnit unit) {
|
||||||
|
Boolean result = redisTemplate.opsForValue().setIfAbsent(key, value, timeout, unit);
|
||||||
|
return Boolean.TRUE.equals(result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user