use of org.redisson.misc.CompletableFutureWrapper in project redisson by redisson.
the class RedissonScript method evalShaAsync.
@Override
public <R> RFuture<R> evalShaAsync(String key, Mode mode, String shaDigest, ReturnType returnType, List<Object> keys, Object... values) {
RedisCommand command = new RedisCommand(returnType.getCommand(), "EVALSHA");
if (mode == Mode.READ_ONLY && commandExecutor.isEvalShaROSupported()) {
RedisCommand cmd = new RedisCommand(returnType.getCommand(), "EVALSHA_RO");
RFuture<R> f = commandExecutor.evalReadAsync(key, codec, cmd, shaDigest, keys, encode(Arrays.asList(values), codec).toArray());
CompletableFuture<R> result = new CompletableFuture<>();
f.whenComplete((r, e) -> {
if (e != null && e.getMessage().startsWith("ERR unknown command")) {
commandExecutor.setEvalShaROSupported(false);
RFuture<R> s = evalShaAsync(key, mode, shaDigest, returnType, keys, values);
commandExecutor.transfer(s.toCompletableFuture(), result);
return;
}
commandExecutor.transfer(f.toCompletableFuture(), result);
});
return new CompletableFutureWrapper<>(result);
}
return commandExecutor.evalWriteAsync(key, codec, command, shaDigest, keys, encode(Arrays.asList(values), codec).toArray());
}
use of org.redisson.misc.CompletableFutureWrapper in project redisson by redisson.
the class RedisConnection method async.
public <T, R> RFuture<R> async(long timeout, Codec encoder, RedisCommand<T> command, Object... params) {
CompletableFuture<R> promise = new CompletableFuture<>();
if (timeout == -1) {
timeout = redisClient.getCommandTimeout();
}
if (redisClient.getEventLoopGroup().isShuttingDown()) {
RedissonShutdownException cause = new RedissonShutdownException("Redisson is shutdown");
return RedissonPromise.newFailedFuture(cause);
}
Timeout scheduledFuture = redisClient.getTimer().newTimeout(t -> {
RedisTimeoutException ex = new RedisTimeoutException("Command execution timeout for command: " + LogHelper.toString(command, params) + ", Redis client: " + redisClient);
promise.completeExceptionally(ex);
}, timeout, TimeUnit.MILLISECONDS);
promise.whenComplete((res, e) -> {
scheduledFuture.cancel();
});
ChannelFuture writeFuture = send(new CommandData<>(promise, encoder, command, params));
writeFuture.addListener((ChannelFutureListener) future -> {
if (!future.isSuccess()) {
promise.completeExceptionally(future.cause());
}
});
return new CompletableFutureWrapper<>(promise);
}
use of org.redisson.misc.CompletableFutureWrapper in project redisson by redisson.
the class RedisClient method shutdownAsync.
public RFuture<Void> shutdownAsync() {
shutdown = true;
CompletableFuture<Void> result = new CompletableFuture<>();
if (channels.isEmpty() || config.getGroup().isShuttingDown()) {
shutdown(result);
return new CompletableFutureWrapper<>(result);
}
ChannelGroupFuture channelsFuture = channels.newCloseFuture();
channelsFuture.addListener(new FutureListener<Void>() {
@Override
public void operationComplete(Future<Void> future) throws Exception {
if (!future.isSuccess()) {
result.completeExceptionally(future.cause());
return;
}
shutdown(result);
}
});
for (Channel channel : channels) {
RedisConnection connection = RedisConnection.getFrom(channel);
if (connection != null) {
connection.closeAsync();
}
}
return new CompletableFutureWrapper<>(result);
}
use of org.redisson.misc.CompletableFutureWrapper in project redisson by redisson.
the class CommandAsyncService method pollFromAnyAsync.
@Override
public <V> RFuture<V> pollFromAnyAsync(String name, Codec codec, RedisCommand<Object> command, long secondsTimeout, String... queueNames) {
if (connectionManager.isClusterMode() && queueNames.length > 0) {
AtomicReference<Iterator<String>> ref = new AtomicReference<>();
List<String> names = new ArrayList<>();
names.add(name);
names.addAll(Arrays.asList(queueNames));
ref.set(names.iterator());
AtomicLong counter = new AtomicLong(secondsTimeout);
CompletionStage<V> result = poll(codec, ref, names, counter, command);
return new CompletableFutureWrapper<>(result);
} else {
List<Object> params = new ArrayList<Object>(queueNames.length + 1);
params.add(name);
params.addAll(Arrays.asList(queueNames));
params.add(secondsTimeout);
return writeAsync(name, codec, command, params.toArray());
}
}
use of org.redisson.misc.CompletableFutureWrapper in project redisson by redisson.
the class CommandAsyncService method readAllAsync.
@Override
public <T, R> RFuture<Collection<R>> readAllAsync(Codec codec, RedisCommand<T> command, Object... params) {
Collection<MasterSlaveEntry> nodes = connectionManager.getEntrySet();
List<CompletableFuture<?>> futures = new ArrayList<>();
for (MasterSlaveEntry entry : nodes) {
RFuture<Object> f = async(true, new NodeSource(entry), codec, command, params, true, false);
futures.add(f.toCompletableFuture());
}
CompletableFuture<Void> future = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
CompletableFuture<Collection<R>> resFuture = future.thenApply(r -> {
List<R> results = new ArrayList<>();
for (CompletableFuture<?> f : futures) {
Object res = f.getNow(null);
if (res instanceof Collection) {
results.addAll((Collection) res);
} else {
results.add((R) res);
}
}
return results;
});
return new CompletableFutureWrapper<>(resFuture);
}
Aggregations