@Override public int countActiveWorkers() { String id = generateRequestId(); int subscribers = (int) workersTopic.publish(id); if (subscribers == 0) { return 0; } RSemaphore semaphore = redisson.getSemaphore(workersSemaphoreName + ":" + id); try { semaphore.tryAcquire(subscribers, 10, TimeUnit.MINUTES); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } RAtomicLong atomicLong = redisson.getAtomicLong(workersCounterName + ":" + id); long result = atomicLong.get(); redisson.getKeys().delete(semaphore, atomicLong); return (int) result; }
@Override public int countActiveWorkers() { String id = generateRequestId(); int subscribers = (int) workersTopic.publish(id); if (subscribers == 0) { return 0; } RSemaphore semaphore = redisson.getSemaphore(workersSemaphoreName + ":" + id); try { semaphore.tryAcquire(subscribers, 10, TimeUnit.MINUTES); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } RAtomicLong atomicLong = redisson.getAtomicLong(workersCounterName + ":" + id); long result = atomicLong.get(); redisson.getKeys().delete(semaphore, atomicLong); return (int) result; }
@Override public boolean acquire(Object key, int permits) { RSemaphore rSemaphore = redisson.getSemaphore(key.toString()); rSemaphore.trySetPermits(permits); return rSemaphore.tryAcquire(); }
private void waitFor(String uuid, int subscribers) { RSemaphore semaphore = redissonClient.getSemaphore(String.format(JobConfig.WORKER_REQ, uuid)); try { semaphore.tryAcquire(subscribers, 10L, TimeUnit.SECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } redissonClient.getKeys().delete(semaphore); }
@Override @SneakyThrows public Lock tryGetLock(String lockName, long timeout, TimeUnit timeUnit) { String id = IdUtils.newUUID(); RSemaphore semaphore = rSemaphoreMap.computeIfAbsent(lockName, key -> { RSemaphore rSemaphore = redissonClient.getSemaphore(key); rSemaphore.trySetPermits(1); return rSemaphore; }); log.debug("try lock {} permits:{},id={}", lockName, semaphore.availablePermits(), id); boolean success = semaphore.tryAcquire(timeout, timeUnit); if (!success) { throw new TimeoutException("try lock " + lockName + " timeout"); } return () -> { semaphore.release(); log.debug("unlock {},id={}", lockName, id); }; }
@Override public int countActiveWorkers() { String id = generateRequestId(); int subscribers = (int) workersTopic.publish(id); if (subscribers == 0) { return 0; } RSemaphore semaphore = redisson.getSemaphore(workersSemaphoreName + ":" + id); try { semaphore.tryAcquire(subscribers, 10, TimeUnit.MINUTES); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } RAtomicLong atomicLong = redisson.getAtomicLong(workersCounterName + ":" + id); long result = atomicLong.get(); redisson.getKeys().delete(semaphore, atomicLong); return (int) result; }