org.redisson.api.RSemaphore Java Examples

The following examples show how to use org.redisson.api.RSemaphore. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RedissonSemaphoreTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testConcurrency_MultiInstance_1_permits() throws InterruptedException {
    int iterations = 30;
    final AtomicInteger lockedCounter = new AtomicInteger();

    RSemaphore s = redisson.getSemaphore("test");
    s.trySetPermits(1);

    testMultiInstanceConcurrency(iterations, r -> {
        RSemaphore s1 = r.getSemaphore("test");
        try {
            s1.acquire();
        }catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int value = lockedCounter.get();
        lockedCounter.set(value + 1);
        s1.release();
    });

    assertThat(lockedCounter.get()).isEqualTo(iterations);
}
 
Example #2
Source File: RedissonSemaphoreTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testConcurrency_SingleInstance() throws InterruptedException {
    final AtomicInteger lockedCounter = new AtomicInteger();

    RSemaphore s = redisson.getSemaphore("test");
    s.trySetPermits(1);

    int iterations = 15;
    testSingleInstanceConcurrency(iterations, r -> {
        RSemaphore s1 = r.getSemaphore("test");
        try {
            s1.acquire();
        }catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int value = lockedCounter.get();
        lockedCounter.set(value + 1);
        s1.release();
    });

    assertThat(lockedCounter.get()).isEqualTo(iterations);
}
 
Example #3
Source File: RedissonSemaphoreTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testConcurrencyLoop_MultiInstance() throws InterruptedException {
    final int iterations = 100;
    final AtomicInteger lockedCounter = new AtomicInteger();

    RSemaphore s = redisson.getSemaphore("test");
    s.trySetPermits(1);

    testMultiInstanceConcurrency(16, r -> {
        for (int i = 0; i < iterations; i++) {
            try {
                r.getSemaphore("test").acquire();
            }catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            int value = lockedCounter.get();
            lockedCounter.set(value + 1);
            r.getSemaphore("test").release();
        }
    });

    assertThat(lockedCounter.get()).isEqualTo(16 * iterations);
}
 
Example #4
Source File: RedissonSemaphoreTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testReleaseAcquire() throws InterruptedException {
    RSemaphore s = redisson.getSemaphore("test");
    s.trySetPermits(10);
    s.acquire();
    assertThat(s.availablePermits()).isEqualTo(9);
    s.release();
    assertThat(s.availablePermits()).isEqualTo(10);
    s.acquire(5);
    assertThat(s.availablePermits()).isEqualTo(5);
    s.release(5);
    assertThat(s.availablePermits()).isEqualTo(10);
}
 
Example #5
Source File: RedissonSemaphoreTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testDrainPermits() throws InterruptedException {
    RSemaphore s = redisson.getSemaphore("test");
    s.trySetPermits(10);
    s.acquire(3);

    assertThat(s.drainPermits()).isEqualTo(7);
    assertThat(s.availablePermits()).isEqualTo(0);
}
 
Example #6
Source File: RedissonSemaphoreTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testReleaseWithoutPermits() {
    RSemaphore s = redisson.getSemaphore("test");
    s.release();

    assertThat(s.availablePermits()).isEqualTo(1);
}
 
Example #7
Source File: RedissonSemaphoreTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testReducePermits() throws InterruptedException {
    RSemaphore s = redisson.getSemaphore("test");
    s.trySetPermits(10);
    
    s.acquire(10);
    s.reducePermits(5);
    assertThat(s.availablePermits()).isEqualTo(-5);
    s.release(10);
    assertThat(s.availablePermits()).isEqualTo(5);
    s.acquire(5);
    assertThat(s.availablePermits()).isEqualTo(0);
}
 
Example #8
Source File: RedissonSemaphoreTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testTrySetPermits() {
    RSemaphore s = redisson.getSemaphore("test");
    assertThat(s.trySetPermits(10)).isTrue();
    assertThat(s.availablePermits()).isEqualTo(10);
    assertThat(s.trySetPermits(15)).isFalse();
    assertThat(s.availablePermits()).isEqualTo(10);
}
 
Example #9
Source File: RedissonSemaphoreTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testAcquireWithoutSetPermits() throws InterruptedException {
    RSemaphore s = redisson.getSemaphore("test");
    s.release();
    s.release();
    s.acquire(2);
}
 
Example #10
Source File: RedissonSemaphoreTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testZero() throws InterruptedException {
    RSemaphore s = redisson.getSemaphore("test");
    assertThat(s.tryAcquire(0, 10, TimeUnit.MINUTES)).isTrue();
    s.release(0);
    assertThat(s.availablePermits()).isZero();
}
 
Example #11
Source File: RedissonLockHeavyTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void lockUnlockRLock() throws Exception {
    for (int i = 0; i < threads; i++) {

        Runnable worker = new Runnable() {

            @Override
            public void run() {
                for (int j = 0; j < loops; j++) {
                    RLock lock = redisson.getLock("RLOCK_" + j);
                    lock.lock();
                    try {
                        RBucket<String> bucket = redisson.getBucket("RBUCKET_" + j);
                        bucket.set("TEST", 30, TimeUnit.SECONDS);
                        RSemaphore semaphore = redisson.getSemaphore("SEMAPHORE_" + j);
                        semaphore.release();
                        try {
                            semaphore.acquire();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    } finally {
                        lock.unlock();
                    }
                }
            }
        };
        executor.execute(worker);
    }
    executor.shutdown();
    executor.awaitTermination(threads * loops, TimeUnit.SECONDS);

}
 
Example #12
Source File: RedisJobRepositoryImpl.java    From earth-frost with Apache License 2.0 5 votes vote down vote up
private void waitFor(String uuid, int subscribers) {
  RSemaphore semaphore = redissonClient.getSemaphore(String.format(Container.WORKER_REQ, uuid));
  try {
    semaphore.tryAcquire(subscribers, 10L, TimeUnit.SECONDS);
  } catch (InterruptedException e) {
    Thread.currentThread().interrupt();
  }
  redissonClient.getKeys().delete(semaphore);
}
 
Example #13
Source File: RedissonSet.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
public RSemaphore getSemaphore(V value) {
    String lockName = getLockByValue(value, "semaphore");
    return new RedissonSemaphore(commandExecutor, lockName);
}
 
Example #14
Source File: RedissonMap.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
public RSemaphore getSemaphore(K key) {
    String lockName = getLockByMapKey(key, "semaphore");
    return new RedissonSemaphore(commandExecutor, lockName);
}
 
Example #15
Source File: RedissonSetMultimapValues.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
public RSemaphore getSemaphore(V value) {
    return set.getSemaphore(value);
}
 
Example #16
Source File: RedissonSetCache.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
public RSemaphore getSemaphore(V value) {
    String lockName = getLockByValue(value, "semaphore");
    return new RedissonSemaphore(commandExecutor, lockName);
}
 
Example #17
Source File: RedissonTransactionalMap.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
public RSemaphore getSemaphore(K key) {
    throw new UnsupportedOperationException("getSemaphore method is not supported in transaction");
}
 
Example #18
Source File: RedissonTransactionalSet.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
public RSemaphore getSemaphore(V value) {
    throw new UnsupportedOperationException("getSemaphore method is not supported in transaction");
}
 
Example #19
Source File: RedissonTransactionalMapCache.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
public RSemaphore getSemaphore(K key) {
    throw new UnsupportedOperationException("getSemaphore method is not supported in transaction");
}
 
Example #20
Source File: TracingRMultimap.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public RSemaphore getSemaphore(K key) {
  return new TracingRSemaphore(map.getSemaphore(key), tracingRedissonHelper);
}
 
Example #21
Source File: TracingRMap.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public RSemaphore getSemaphore(K key) {
  return new TracingRSemaphore(map.getSemaphore(key), tracingRedissonHelper);
}
 
Example #22
Source File: TracingRSet.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public RSemaphore getSemaphore(V value) {
  return new TracingRSemaphore(set.getSemaphore(value), tracingRedissonHelper);
}
 
Example #23
Source File: TracingRedissonClient.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public RSemaphore getSemaphore(String name) {
  return new TracingRSemaphore(redissonClient.getSemaphore(name), tracingRedissonHelper);
}
 
Example #24
Source File: TracingRSemaphore.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
public TracingRSemaphore(RSemaphore semaphore, TracingRedissonHelper tracingRedissonHelper) {
  super(semaphore, tracingRedissonHelper);
  this.semaphore = semaphore;
  this.tracingRedissonHelper = tracingRedissonHelper;
}
 
Example #25
Source File: TracingRSetCache.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public RSemaphore getSemaphore(V value) {
  return new TracingRSemaphore(cache.getSemaphore(value), tracingRedissonHelper);
}
 
Example #26
Source File: RedisPeakLimiter.java    From Limiter with Apache License 2.0 4 votes vote down vote up
@Override
public void release(Object key, int max) {
    RSemaphore rSemaphore = redisson.getSemaphore(key.toString());
    rSemaphore.release();
}
 
Example #27
Source File: RedisPeakLimiter.java    From Limiter with Apache License 2.0 4 votes vote down vote up
@Override
public boolean acquire(Object key, int max) {
    RSemaphore rSemaphore = redisson.getSemaphore(key.toString());
    return rSemaphore.tryAcquire();
}