All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.lettuce.core.dynamic.AsyncExecutableCommand Maven / Gradle / Ivy

Go to download

Advanced and thread-safe Java Redis client for synchronous, asynchronous, and reactive usage. Supports Cluster, Sentinel, Pipelining, Auto-Reconnect, Codecs and much more.

The newest version!
package io.lettuce.core.dynamic;

import java.time.Duration;
import java.util.concurrent.ExecutionException;

import io.lettuce.core.api.StatefulConnection;
import io.lettuce.core.dynamic.domain.Timeout;
import io.lettuce.core.dynamic.parameter.ExecutionSpecificParameters;
import io.lettuce.core.internal.Futures;
import io.lettuce.core.protocol.AsyncCommand;
import io.lettuce.core.protocol.RedisCommand;

/**
 * An {@link ExecutableCommand} that is executed asynchronously or synchronously.
 *
 * @author Mark Paluch
 * @since 5.0
 */
class AsyncExecutableCommand implements ExecutableCommand {

    private final CommandMethod commandMethod;

    private final CommandFactory commandFactory;

    private final StatefulConnection connection;

    AsyncExecutableCommand(CommandMethod commandMethod, CommandFactory commandFactory,
            StatefulConnection connection) {

        this.commandMethod = commandMethod;
        this.commandFactory = commandFactory;
        this.connection = connection;
    }

    @Override
    public Object execute(Object[] parameters) throws ExecutionException, InterruptedException {

        RedisCommand command = commandFactory.createCommand(parameters);

        return dispatchCommand(parameters, command);
    }

    protected Object dispatchCommand(Object[] arguments, RedisCommand command)
            throws InterruptedException, java.util.concurrent.ExecutionException {

        AsyncCommand asyncCommand = new AsyncCommand<>(command);

        if (commandMethod.isFutureExecution()) {

            RedisCommand dispatched = connection.dispatch(asyncCommand);

            if (dispatched instanceof AsyncCommand) {
                return dispatched;
            }

            return asyncCommand;
        }

        connection.dispatch(asyncCommand);

        Duration timeout = connection.getTimeout();

        if (commandMethod.getParameters() instanceof ExecutionSpecificParameters) {
            ExecutionSpecificParameters executionSpecificParameters = (ExecutionSpecificParameters) commandMethod
                    .getParameters();

            if (executionSpecificParameters.hasTimeoutIndex()) {
                Timeout timeoutArg = (Timeout) arguments[executionSpecificParameters.getTimeoutIndex()];
                if (timeoutArg != null) {
                    timeout = timeoutArg.getTimeout();
                }
            }
        }

        Futures.await(timeout, asyncCommand);

        return asyncCommand.get();
    }

    @Override
    public CommandMethod getCommandMethod() {
        return commandMethod;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy