use of org.redisson.misc.CompletableFutureWrapper in project redisson by redisson.
the class RedissonSpinLock method tryLockAsync.
@Override
public RFuture<Boolean> tryLockAsync(long waitTime, long leaseTime, TimeUnit unit, long currentThreadId) {
CompletableFuture<Boolean> result = new CompletableFuture<>();
AtomicLong time = new AtomicLong(unit.toMillis(waitTime));
LockOptions.BackOffPolicy backOffPolicy = backOff.create();
tryLock(leaseTime, unit, currentThreadId, result, time, backOffPolicy);
return new CompletableFutureWrapper<>(result);
}
use of org.redisson.misc.CompletableFutureWrapper in project redisson by redisson.
the class CommandAsyncService method evalAsync.
private <T, R> RFuture<R> evalAsync(NodeSource nodeSource, boolean readOnlyMode, Codec codec, RedisCommand<T> evalCommandType, String script, List<Object> keys, boolean noRetry, Object... params) {
if (isEvalCacheActive() && evalCommandType.getName().equals("EVAL")) {
CompletableFuture<R> mainPromise = new CompletableFuture<>();
Object[] pps = copy(params);
CompletableFuture<R> promise = new CompletableFuture<>();
String sha1 = calcSHA(script);
RedisCommand cmd;
if (readOnlyMode && evalShaROSupported.get()) {
cmd = new RedisCommand(evalCommandType, "EVALSHA_RO");
} else {
cmd = new RedisCommand(evalCommandType, "EVALSHA");
}
List<Object> args = new ArrayList<Object>(2 + keys.size() + params.length);
args.add(sha1);
args.add(keys.size());
args.addAll(keys);
args.addAll(Arrays.asList(params));
RedisExecutor<T, R> executor = new RedisExecutor<>(readOnlyMode, nodeSource, codec, cmd, args.toArray(), promise, false, connectionManager, objectBuilder, referenceType, noRetry);
executor.execute();
promise.whenComplete((res, e) -> {
if (e != null) {
if (e.getMessage().startsWith("ERR unknown command")) {
evalShaROSupported.set(false);
free(pps);
RFuture<R> future = evalAsync(nodeSource, readOnlyMode, codec, evalCommandType, script, keys, noRetry, params);
transfer(future.toCompletableFuture(), mainPromise);
} else if (e.getMessage().startsWith("NOSCRIPT")) {
RFuture<String> loadFuture = loadScript(executor.getRedisClient(), script);
loadFuture.whenComplete((r, ex) -> {
if (ex != null) {
free(pps);
mainPromise.completeExceptionally(ex);
return;
}
List<Object> newargs = new ArrayList<Object>(2 + keys.size() + params.length);
newargs.add(sha1);
newargs.add(keys.size());
newargs.addAll(keys);
newargs.addAll(Arrays.asList(pps));
NodeSource ns = nodeSource;
if (ns.getRedisClient() == null) {
ns = new NodeSource(nodeSource, executor.getRedisClient());
}
RFuture<R> future = async(readOnlyMode, ns, codec, cmd, newargs.toArray(), false, noRetry);
transfer(future.toCompletableFuture(), mainPromise);
});
} else {
free(pps);
mainPromise.completeExceptionally(e);
}
return;
}
free(pps);
mainPromise.complete(res);
});
return new CompletableFutureWrapper<>(mainPromise);
}
List<Object> args = new ArrayList<Object>(2 + keys.size() + params.length);
args.add(script);
args.add(keys.size());
args.addAll(keys);
args.addAll(Arrays.asList(params));
return async(readOnlyMode, nodeSource, codec, evalCommandType, args.toArray(), false, noRetry);
}
use of org.redisson.misc.CompletableFutureWrapper in project redisson by redisson.
the class CommandAsyncService method async.
public <V, R> RFuture<R> async(boolean readOnlyMode, NodeSource source, Codec codec, RedisCommand<V> command, Object[] params, boolean ignoreRedirect, boolean noRetry) {
if (readOnlyMode && command.getName().equals("SORT") && !sortRoSupported.get()) {
readOnlyMode = false;
} else if (readOnlyMode && command.getName().equals("SORT") && sortRoSupported.get()) {
RedisCommand cmd = new RedisCommand("SORT_RO", command.getReplayMultiDecoder());
CompletableFuture<R> mainPromise = createPromise();
RedisExecutor<V, R> executor = new RedisExecutor<>(readOnlyMode, source, codec, cmd, params, mainPromise, ignoreRedirect, connectionManager, objectBuilder, referenceType, noRetry);
executor.execute();
CompletableFuture<R> result = new CompletableFuture<>();
mainPromise.whenComplete((r, e) -> {
if (e != null && e.getMessage().startsWith("ERR unknown command")) {
sortRoSupported.set(false);
RFuture<R> future = async(false, source, codec, command, params, ignoreRedirect, noRetry);
transfer(future.toCompletableFuture(), result);
return;
}
transfer(mainPromise, result);
});
return new CompletableFutureWrapper<>(result);
}
CompletableFuture<R> mainPromise = createPromise();
RedisExecutor<V, R> executor = new RedisExecutor<>(readOnlyMode, source, codec, command, params, mainPromise, ignoreRedirect, connectionManager, objectBuilder, referenceType, noRetry);
executor.execute();
return new CompletableFutureWrapper<>(mainPromise);
}
Aggregations