Search in sources :

Example 21 with CompletableFutureWrapper

use of org.redisson.misc.CompletableFutureWrapper in project redisson by redisson.

the class RedisClientEntry method pingAsync.

@Override
public RFuture<Boolean> pingAsync(long timeout, TimeUnit timeUnit) {
    RFuture<Boolean> f = commandExecutor.readAsync(client, null, RedisCommands.PING_BOOL);
    CompletableFuture<Boolean> s = f.toCompletableFuture().handle((res, e) -> {
        if (e != null) {
            return false;
        }
        return res;
    });
    commandExecutor.getConnectionManager().newTimeout(t -> {
        RedisTimeoutException ex = new RedisTimeoutException("Command execution timeout for command: PING, Redis client: " + client);
        s.completeExceptionally(ex);
    }, timeout, timeUnit);
    return new CompletableFutureWrapper<>(s);
}
Also used : RedisTimeoutException(org.redisson.client.RedisTimeoutException) CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper)

Example 22 with CompletableFutureWrapper

use of org.redisson.misc.CompletableFutureWrapper in project redisson by redisson.

the class RedisNode method pingAsync.

@Override
public RFuture<Boolean> pingAsync(long timeout, TimeUnit timeUnit) {
    RFuture<Boolean> f = commandExecutor.readAsync(client, null, RedisCommands.PING_BOOL);
    CompletionStage<Boolean> s = f.exceptionally(e -> false);
    commandExecutor.getConnectionManager().newTimeout(t -> {
        RedisTimeoutException ex = new RedisTimeoutException("Command execution timeout (" + timeUnit.toMillis(timeout) + "ms) for command: PING, Redis client: " + client);
        s.toCompletableFuture().completeExceptionally(ex);
    }, timeout, timeUnit);
    return new CompletableFutureWrapper<>(s);
}
Also used : RedisTimeoutException(org.redisson.client.RedisTimeoutException) CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper)

Example 23 with CompletableFutureWrapper

use of org.redisson.misc.CompletableFutureWrapper in project redisson by redisson.

the class RedissonCountDownLatch method awaitAsync.

@Override
public RFuture<Boolean> awaitAsync(long waitTime, TimeUnit unit) {
    CompletableFuture<Boolean> result = new CompletableFuture<>();
    AtomicLong time = new AtomicLong(unit.toMillis(waitTime));
    long currentTime = System.currentTimeMillis();
    CompletableFuture<Long> countFuture = getCountAsync().toCompletableFuture();
    CompletableFuture<Boolean> f = countFuture.thenCompose(r -> {
        long el = System.currentTimeMillis() - currentTime;
        time.addAndGet(-el);
        if (time.get() <= 0) {
            return CompletableFuture.completedFuture(false);
        }
        long current = System.currentTimeMillis();
        CompletableFuture<RedissonCountDownLatchEntry> subscribeFuture = subscribe();
        return subscribeFuture.thenCompose(entry -> {
            long elapsed = System.currentTimeMillis() - current;
            time.addAndGet(-elapsed);
            return await(time, entry);
        });
    });
    return new CompletableFutureWrapper<>(f);
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 24 with CompletableFutureWrapper

use of org.redisson.misc.CompletableFutureWrapper in project redisson by redisson.

the class RedissonBaseAdder method resetAsync.

public RFuture<Void> resetAsync() {
    String id = generateId();
    RFuture<Long> future = topic.publishAsync(CLEAR_MSG + ":" + id);
    RSemaphore semaphore = getSemaphore(id);
    CompletionStage<Void> f = future.thenCompose(r -> semaphore.acquireAsync(r.intValue())).thenCompose(r -> semaphore.deleteAsync().thenApply(res -> null));
    return new CompletableFutureWrapper<>(f);
}
Also used : RFuture(org.redisson.api.RFuture) CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) ByteBufUtil(io.netty.buffer.ByteBufUtil) CommandAsyncExecutor(org.redisson.command.CommandAsyncExecutor) Logger(org.slf4j.Logger) StringCodec(org.redisson.client.codec.StringCodec) java.util.concurrent(java.util.concurrent) RTopic(org.redisson.api.RTopic) LoggerFactory(org.slf4j.LoggerFactory) RSemaphore(org.redisson.api.RSemaphore) RedissonClient(org.redisson.api.RedissonClient) CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) RSemaphore(org.redisson.api.RSemaphore)

Example 25 with CompletableFutureWrapper

use of org.redisson.misc.CompletableFutureWrapper in project redisson by redisson.

the class RedissonBaseAdder method resetAsync.

public RFuture<Void> resetAsync(long timeout, TimeUnit timeUnit) {
    String id = generateId();
    RFuture<Long> future = topic.publishAsync(CLEAR_MSG + ":" + id);
    RSemaphore semaphore = getSemaphore(id);
    CompletionStage<Void> f = future.thenCompose(r -> tryAcquire(semaphore, timeout, timeUnit, r.intValue())).thenCompose(r -> semaphore.deleteAsync().thenApply(res -> null));
    return new CompletableFutureWrapper<>(f);
}
Also used : RFuture(org.redisson.api.RFuture) CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) ByteBufUtil(io.netty.buffer.ByteBufUtil) CommandAsyncExecutor(org.redisson.command.CommandAsyncExecutor) Logger(org.slf4j.Logger) StringCodec(org.redisson.client.codec.StringCodec) java.util.concurrent(java.util.concurrent) RTopic(org.redisson.api.RTopic) LoggerFactory(org.slf4j.LoggerFactory) RSemaphore(org.redisson.api.RSemaphore) RedissonClient(org.redisson.api.RedissonClient) CompletableFutureWrapper(org.redisson.misc.CompletableFutureWrapper) RSemaphore(org.redisson.api.RSemaphore)

Aggregations

CompletableFutureWrapper (org.redisson.misc.CompletableFutureWrapper)38 ByteBuf (io.netty.buffer.ByteBuf)16 AtomicLong (java.util.concurrent.atomic.AtomicLong)9 RFuture (org.redisson.api.RFuture)8 CompletableFuture (java.util.concurrent.CompletableFuture)7 StringCodec (org.redisson.client.codec.StringCodec)7 Logger (org.slf4j.Logger)7 LoggerFactory (org.slf4j.LoggerFactory)7 ByteBufUtil (io.netty.buffer.ByteBufUtil)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 CommandAsyncExecutor (org.redisson.command.CommandAsyncExecutor)5 Timeout (io.netty.util.Timeout)4 java.util.concurrent (java.util.concurrent)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 RSemaphore (org.redisson.api.RSemaphore)4 RTopic (org.redisson.api.RTopic)4 RedissonClient (org.redisson.api.RedissonClient)4 RedisCommand (org.redisson.client.protocol.RedisCommand)4 TimerTask (io.netty.util.TimerTask)3