Java Code Examples for org.redisson.api.RFuture#await()

The following examples show how to use org.redisson.api.RFuture#await() . 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: RedissonCountDownLatch.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
public boolean await(long time, TimeUnit unit) throws InterruptedException {
    long remainTime = unit.toMillis(time);
    long current = System.currentTimeMillis();
    if (getCount() == 0) {
        return true;
    }
    RFuture<RedissonCountDownLatchEntry> promise = subscribe();
    if (!promise.await(time, unit)) {
        return false;
    }

    try {
        remainTime -= System.currentTimeMillis() - current;
        if (remainTime <= 0) {
            return false;
        }

        while (getCount() > 0) {
            if (remainTime <= 0) {
                return false;
            }
            current = System.currentTimeMillis();
            // waiting for open state
            promise.getNow().getLatch().await(remainTime, TimeUnit.MILLISECONDS);

            remainTime -= System.currentTimeMillis() - current;
        }

        return true;
    } finally {
        unsubscribe(promise);
    }
}
 
Example 2
Source File: RedissonPriorityBlockingQueueTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testPollAsyncReattach() throws InterruptedException, IOException, ExecutionException, TimeoutException {
    RedisProcess runner = new RedisRunner()
            .nosave()
            .randomDir()
            .randomPort()
            .run();
    
    Config config = new Config();
    config.useSingleServer().setAddress(runner.getRedisServerAddressAndPort());
    RedissonClient redisson = Redisson.create(config);
    
    RBlockingQueue<Integer> queue1 = getQueue(redisson);
    RFuture<Integer> f = queue1.pollAsync(10, TimeUnit.SECONDS);
    f.await(1, TimeUnit.SECONDS);
    runner.stop();

    runner = new RedisRunner()
            .port(runner.getRedisServerPort())
            .nosave()
            .randomDir()
            .run();
    queue1.put(123);
    
    // check connection rotation
    for (int i = 0; i < 10; i++) {
        queue1.put(i + 1000);
    }
    Integer result = f.get();
    assertThat(queue1.size()).isEqualTo(10);
    
    assertThat(result).isEqualTo(123);
    
    redisson.shutdown();
    runner.stop();
}
 
Example 3
Source File: RedissonPriorityBlockingQueueTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testTakeReattach() throws Exception {
    RedisProcess runner = new RedisRunner()
            .nosave()
            .randomDir()
            .randomPort()
            .run();
    
    Config config = new Config();
    config.useSingleServer().setAddress(runner.getRedisServerAddressAndPort());
    RedissonClient redisson = Redisson.create(config);
    RBlockingQueue<Integer> queue1 = getQueue(redisson);
    RFuture<Integer> f = queue1.takeAsync();
    f.await(1, TimeUnit.SECONDS);
    runner.stop();

    runner = new RedisRunner()
            .port(runner.getRedisServerPort())
            .nosave()
            .randomDir()
            .run();
    queue1.put(123);
    
    // check connection rotation
    for (int i = 0; i < 10; i++) {
        queue1.put(i + 10000);
    }

    Integer result = f.get();
    assertThat(result).isEqualTo(123);
    assertThat(queue1.size()).isEqualTo(10);
    runner.stop();
    
    redisson.shutdown();
}
 
Example 4
Source File: RedissonBlockingQueueTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testPollAsyncReattach() throws InterruptedException, IOException, ExecutionException, TimeoutException {
    RedisProcess runner = new RedisRunner()
            .nosave()
            .randomDir()
            .randomPort()
            .run();
    
    Config config = new Config();
    config.useSingleServer().setAddress(runner.getRedisServerAddressAndPort());
    RedissonClient redisson = Redisson.create(config);
    
    RBlockingQueue<Integer> queue1 = getQueue(redisson);
    RFuture<Integer> f = queue1.pollAsync(10, TimeUnit.SECONDS);
    f.await(1, TimeUnit.SECONDS);
    runner.stop();

    runner = new RedisRunner()
            .port(runner.getRedisServerPort())
            .nosave()
            .randomDir()
            .run();
    queue1.put(123);
    
    // check connection rotation
    for (int i = 0; i < 10; i++) {
        queue1.put(i);
    }
    assertThat(queue1.size()).isEqualTo(10);
    
    Integer result = f.get(1, TimeUnit.SECONDS);
    assertThat(result).isEqualTo(123);
    
    redisson.shutdown();
    runner.stop();
}
 
Example 5
Source File: RedissonBlockingQueueTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testTakeReattach() throws Exception {
    RedisProcess runner = new RedisRunner()
            .nosave()
            .randomDir()
            .randomPort()
            .run();
    
    Config config = new Config();
    config.useSingleServer().setAddress(runner.getRedisServerAddressAndPort());
    RedissonClient redisson = Redisson.create(config);

    RBlockingQueue<Integer> queue1 = getQueue(redisson);
    RFuture<Integer> f = queue1.takeAsync();
    f.await(1, TimeUnit.SECONDS);
    runner.stop();

    runner = new RedisRunner()
            .port(runner.getRedisServerPort())
            .nosave()
            .randomDir()
            .run();
    queue1.put(123);
    
    // check connection rotation
    for (int i = 0; i < 10; i++) {
        queue1.put(i + 10000);
    }
    assertThat(queue1.size()).isEqualTo(10);
    
    Integer result = f.get(1, TimeUnit.SECONDS);
    assertThat(result).isEqualTo(123);
    runner.stop();
    
    redisson.shutdown();
}
 
Example 6
Source File: RedissonBoundedBlockingQueueTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testPollAsyncReattach() throws InterruptedException, IOException, ExecutionException, TimeoutException {
    RedisProcess runner = new RedisRunner()
            .nosave()
            .randomDir()
            .randomPort()
            .run();
    
    Config config = new Config();
    config.useSingleServer().setAddress(runner.getRedisServerAddressAndPort());
    RedissonClient redisson = Redisson.create(config);
    
    RBoundedBlockingQueue<Integer> queue1 = redisson.getBoundedBlockingQueue("queue:pollany");
    RFuture<Integer> f = queue1.pollAsync(10, TimeUnit.SECONDS);
    f.await(1, TimeUnit.SECONDS);
    runner.stop();

    runner = new RedisRunner()
            .port(runner.getRedisServerPort())
            .nosave()
            .randomDir()
            .run();
    assertThat(queue1.trySetCapacity(15)).isTrue();
    queue1.put(123);
    
    // check connection rotation
    for (int i = 0; i < 10; i++) {
        queue1.put(i);
    }
    assertThat(queue1.size()).isEqualTo(10);
    
    Integer result = f.get(1, TimeUnit.SECONDS);
    assertThat(result).isEqualTo(123);
    
    redisson.shutdown();
    runner.stop();
}
 
Example 7
Source File: RedissonBoundedBlockingQueueTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testTakeReattach() throws InterruptedException, IOException, ExecutionException, TimeoutException {
    RedisProcess runner = new RedisRunner()
            .nosave()
            .randomDir()
            .randomPort()
            .run();
    
    Config config = new Config();
    config.useSingleServer().setAddress(runner.getRedisServerAddressAndPort());
    RedissonClient redisson = Redisson.create(config);
    redisson.getKeys().flushall();
    
    RBoundedBlockingQueue<Integer> queue1 = redisson.getBoundedBlockingQueue("testTakeReattach");
    assertThat(queue1.trySetCapacity(15)).isTrue();
    RFuture<Integer> f = queue1.takeAsync();
    f.await(1, TimeUnit.SECONDS);
    runner.stop();

    runner = new RedisRunner()
            .port(runner.getRedisServerPort())
            .nosave()
            .randomDir()
            .run();
    assertThat(queue1.trySetCapacity(15)).isTrue();
    queue1.put(123);
    
    // check connection rotation
    for (int i = 0; i < 10; i++) {
        queue1.put(i);
    }
    assertThat(queue1.size()).isEqualTo(10);
    
    Integer result = f.get(1, TimeUnit.SECONDS);
    assertThat(result).isEqualTo(123);
    runner.stop();
    
    redisson.shutdown();
}
 
Example 8
Source File: RedissonSemaphore.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
    public boolean tryAcquire(int permits, long waitTime, TimeUnit unit) throws InterruptedException {
        long time = unit.toMillis(waitTime);
        long current = System.currentTimeMillis();

        if (tryAcquire(permits)) {
            return true;
        }

        time -= System.currentTimeMillis() - current;
        if (time <= 0) {
            return false;
        }

        current = System.currentTimeMillis();
        RFuture<RedissonLockEntry> future = subscribe();
        if (!future.await(time, TimeUnit.MILLISECONDS)) {
            return false;
        }

        try {
            time -= System.currentTimeMillis() - current;
            if (time <= 0) {
                return false;
            }
            
            while (true) {
                current = System.currentTimeMillis();
                if (tryAcquire(permits)) {
                    return true;
                }

                time -= System.currentTimeMillis() - current;
                if (time <= 0) {
                    return false;
                }

                // waiting for message
                current = System.currentTimeMillis();

                future.getNow().getLatch().tryAcquire(permits, time, TimeUnit.MILLISECONDS);

                time -= System.currentTimeMillis() - current;
                if (time <= 0) {
                    return false;
                }
            }
        } finally {
            unsubscribe(future);
        }
//        return get(tryAcquireAsync(permits, waitTime, unit));
    }
 
Example 9
Source File: RedissonPermitExpirableSemaphore.java    From redisson with Apache License 2.0 4 votes vote down vote up
private String tryAcquire(int permits, long waitTime, long ttl, TimeUnit unit) throws InterruptedException {
        long time = unit.toMillis(waitTime);
        long current = System.currentTimeMillis();

        String permitId = tryAcquire(permits, ttl, unit);
        if (permitId != null && !permitId.startsWith(":")) {
            return permitId;
        }

        time -= System.currentTimeMillis() - current;
        if (time <= 0) {
            return null;
        }
        
        current = System.currentTimeMillis();
        RFuture<RedissonLockEntry> future = subscribe();
        if (!future.await(time, TimeUnit.MILLISECONDS)) {
            return null;
        }

        try {
            time -= System.currentTimeMillis() - current;
            if (time <= 0) {
                return null;
            }
            
            while (true) {
                current = System.currentTimeMillis();
                Long nearestTimeout;
                permitId = tryAcquire(permits, ttl, unit);
                if (permitId != null) {
                    if (!permitId.startsWith(":")) {
                        return permitId;
                    } else {
                        nearestTimeout = Long.valueOf(permitId.substring(1)) - System.currentTimeMillis();
                    }
                } else {
                    nearestTimeout = null;
                }
                
                time -= System.currentTimeMillis() - current;
                if (time <= 0) {
                    return null;
                }

                // waiting for message
                current = System.currentTimeMillis();

                if (nearestTimeout != null) {
                    future.getNow().getLatch().tryAcquire(permits, Math.min(time, nearestTimeout), TimeUnit.MILLISECONDS);
                } else {
                    future.getNow().getLatch().tryAcquire(permits, time, TimeUnit.MILLISECONDS);
                }
                
                long elapsed = System.currentTimeMillis() - current;
                time -= elapsed;
                if (time <= 0) {
                    return null;
                }
            }
        } finally {
            unsubscribe(future);
        }
//        return get(tryAcquireAsync(permits, waitTime, ttl, unit));
    }
 
Example 10
Source File: CoordinatorTask.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
public Object call() throws Exception {
    long timeSpent = System.currentTimeMillis() - startTime;
    if (isTimeoutExpired(timeSpent)) {
        throw new MapReduceTimeoutException();
    }
    
    this.codec = (Codec) objectCodecClass.getConstructor().newInstance();
    
    RScheduledExecutorService executor = redisson.getExecutorService(RExecutorService.MAPREDUCE_NAME);
    int workersAmount = executor.countActiveWorkers();
    
    UUID id = UUID.randomUUID();
    String collectorMapName = objectName + ":collector:" + id;

    mapperTask.setCollectorMapName(collectorMapName);
    mapperTask.setWorkersAmount(workersAmount);
    timeSpent = System.currentTimeMillis() - startTime;
    if (isTimeoutExpired(timeSpent)) {
        throw new MapReduceTimeoutException();
    }
    if (timeout > 0) {
        mapperTask.setTimeout(timeout - timeSpent);
    }

    mapperTask.addObjectName(objectName);
    RFuture<?> mapperFuture = executor.submitAsync(mapperTask);
    try {
        if (timeout > 0 && !mapperFuture.await(timeout - timeSpent)) {
            mapperFuture.cancel(true);
            throw new MapReduceTimeoutException();
        }
        if (timeout == 0) {
            mapperFuture.await();
        }
    } catch (InterruptedException e) {
        mapperFuture.cancel(true);
        return null;
    }

    SubTasksExecutor reduceExecutor = new SubTasksExecutor(executor, workersAmount, startTime, timeout);
    for (int i = 0; i < workersAmount; i++) {
        String name = collectorMapName + ":" + i;
        Runnable runnable = new ReducerTask<KOut, VOut>(name, reducer, objectCodecClass, resultMapName, timeout - timeSpent);
        reduceExecutor.submit(runnable);
    }

    if (!reduceExecutor.await()) {
        return null;
    }
    
    return executeCollator();
}
 
Example 11
Source File: RedissonLock.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
    public boolean tryLock(long waitTime, long leaseTime, TimeUnit unit) throws InterruptedException {
        long time = unit.toMillis(waitTime);
        long current = System.currentTimeMillis();
        long threadId = Thread.currentThread().getId();
        Long ttl = tryAcquire(leaseTime, unit, threadId);
        // lock acquired
        if (ttl == null) {
            return true;
        }
        
        time -= System.currentTimeMillis() - current;
        if (time <= 0) {
            acquireFailed(threadId);
            return false;
        }
        
        current = System.currentTimeMillis();
        RFuture<RedissonLockEntry> subscribeFuture = subscribe(threadId);
        if (!subscribeFuture.await(time, TimeUnit.MILLISECONDS)) {
            if (!subscribeFuture.cancel(false)) {
                subscribeFuture.onComplete((res, e) -> {
                    if (e == null) {
                        unsubscribe(subscribeFuture, threadId);
                    }
                });
            }
            acquireFailed(threadId);
            return false;
        }

        try {
            time -= System.currentTimeMillis() - current;
            if (time <= 0) {
                acquireFailed(threadId);
                return false;
            }
        
            while (true) {
                long currentTime = System.currentTimeMillis();
                ttl = tryAcquire(leaseTime, unit, threadId);
                // lock acquired
                if (ttl == null) {
                    return true;
                }

                time -= System.currentTimeMillis() - currentTime;
                if (time <= 0) {
                    acquireFailed(threadId);
                    return false;
                }

                // waiting for message
                currentTime = System.currentTimeMillis();
                if (ttl >= 0 && ttl < time) {
                    subscribeFuture.getNow().getLatch().tryAcquire(ttl, TimeUnit.MILLISECONDS);
                } else {
                    subscribeFuture.getNow().getLatch().tryAcquire(time, TimeUnit.MILLISECONDS);
                }

                time -= System.currentTimeMillis() - currentTime;
                if (time <= 0) {
                    acquireFailed(threadId);
                    return false;
                }
            }
        } finally {
            unsubscribe(subscribeFuture, threadId);
        }
//        return get(tryLockAsync(waitTime, leaseTime, unit));
    }
 
Example 12
Source File: RedissonBlockingQueueTest.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Test
public void testTakeReattachSentinel() throws IOException, InterruptedException, TimeoutException, ExecutionException {
    RedisRunner.RedisProcess master = new RedisRunner()
            .nosave()
            .randomDir()
            .run();
    RedisRunner.RedisProcess slave1 = new RedisRunner()
            .port(6380)
            .nosave()
            .randomDir()
            .slaveof("127.0.0.1", 6379)
            .run();
    RedisRunner.RedisProcess slave2 = new RedisRunner()
            .port(6381)
            .nosave()
            .randomDir()
            .slaveof("127.0.0.1", 6379)
            .run();
    RedisRunner.RedisProcess sentinel1 = new RedisRunner()
            .nosave()
            .randomDir()
            .port(26379)
            .sentinel()
            .sentinelMonitor("myMaster", "127.0.0.1", 6379, 2)
            .run();
    RedisRunner.RedisProcess sentinel2 = new RedisRunner()
            .nosave()
            .randomDir()
            .port(26380)
            .sentinel()
            .sentinelMonitor("myMaster", "127.0.0.1", 6379, 2)
            .run();
    RedisRunner.RedisProcess sentinel3 = new RedisRunner()
            .nosave()
            .randomDir()
            .port(26381)
            .sentinel()
            .sentinelMonitor("myMaster", "127.0.0.1", 6379, 2)
            .run();

    Thread.sleep(1000);

    Config config = new Config();
    config.useSentinelServers()
        .setLoadBalancer(new RandomLoadBalancer())
        .addSentinelAddress(sentinel3.getRedisServerAddressAndPort()).setMasterName("myMaster");
    RedissonClient redisson = Redisson.create(config);

    RBlockingQueue<Integer> queue1 = getQueue(redisson);
    RFuture<Integer> f = queue1.takeAsync();
    f.await(1, TimeUnit.SECONDS);

    master.stop();

    Thread.sleep(TimeUnit.SECONDS.toMillis(60));

    queue1.put(123);

    // check connection rotation
    for (int i = 0; i < 10; i++) {
        queue1.put(i + 10000);
    }
    assertThat(queue1.size()).isEqualTo(10);

    Integer result = f.get(80, TimeUnit.SECONDS);
    assertThat(result).isEqualTo(123);

    redisson.shutdown();
    sentinel1.stop();
    sentinel2.stop();
    sentinel3.stop();
    master.stop();
    slave1.stop();
    slave2.stop();

}