Search in sources :

Example 1 with ClientMode

use of org.apache.pulsar.io.redis.RedisAbstractConfig.ClientMode in project pulsar by apache.

the class RedisSession method create.

public static RedisSession create(RedisSinkConfig config) {
    RedisSession redisSession;
    final RedisCodec<byte[], byte[]> codec = new ByteArrayCodec();
    final SocketOptions socketOptions = SocketOptions.builder().tcpNoDelay(config.isTcpNoDelay()).connectTimeout(Duration.ofMillis(config.getConnectTimeout())).keepAlive(config.isKeepAlive()).build();
    final ClientMode clientMode;
    try {
        clientMode = ClientMode.valueOf(config.getClientMode().toUpperCase());
    } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException("Illegal Redis client mode, valid values are: " + Arrays.asList(ClientMode.values()));
    }
    List<RedisURI> redisURIs = redisURIs(config.getHostAndPorts(), config);
    if (clientMode == ClientMode.STANDALONE) {
        ClientOptions.Builder clientOptions = ClientOptions.builder().socketOptions(socketOptions).requestQueueSize(config.getRequestQueue()).autoReconnect(config.isAutoReconnect());
        final RedisClient client = RedisClient.create(redisURIs.get(0));
        client.setOptions(clientOptions.build());
        final StatefulRedisConnection<byte[], byte[]> connection = client.connect(codec);
        redisSession = new RedisSession(client, connection, connection.async());
    } else if (clientMode == ClientMode.CLUSTER) {
        ClusterClientOptions.Builder clientOptions = ClusterClientOptions.builder().requestQueueSize(config.getRequestQueue()).autoReconnect(config.isAutoReconnect());
        final RedisClusterClient client = RedisClusterClient.create(redisURIs);
        client.setOptions(clientOptions.build());
        final StatefulRedisClusterConnection<byte[], byte[]> connection = client.connect(codec);
        redisSession = new RedisSession(client, connection, connection.async());
    } else {
        throw new UnsupportedOperationException(String.format("%s is not supported", config.getClientMode()));
    }
    return redisSession;
}
Also used : ClientOptions(io.lettuce.core.ClientOptions) ClusterClientOptions(io.lettuce.core.cluster.ClusterClientOptions) RedisURI(io.lettuce.core.RedisURI) SocketOptions(io.lettuce.core.SocketOptions) ClientMode(org.apache.pulsar.io.redis.RedisAbstractConfig.ClientMode) ByteArrayCodec(io.lettuce.core.codec.ByteArrayCodec) RedisClient(io.lettuce.core.RedisClient) AbstractRedisClient(io.lettuce.core.AbstractRedisClient) StatefulRedisClusterConnection(io.lettuce.core.cluster.api.StatefulRedisClusterConnection) RedisClusterClient(io.lettuce.core.cluster.RedisClusterClient)

Aggregations

AbstractRedisClient (io.lettuce.core.AbstractRedisClient)1 ClientOptions (io.lettuce.core.ClientOptions)1 RedisClient (io.lettuce.core.RedisClient)1 RedisURI (io.lettuce.core.RedisURI)1 SocketOptions (io.lettuce.core.SocketOptions)1 ClusterClientOptions (io.lettuce.core.cluster.ClusterClientOptions)1 RedisClusterClient (io.lettuce.core.cluster.RedisClusterClient)1 StatefulRedisClusterConnection (io.lettuce.core.cluster.api.StatefulRedisClusterConnection)1 ByteArrayCodec (io.lettuce.core.codec.ByteArrayCodec)1 ClientMode (org.apache.pulsar.io.redis.RedisAbstractConfig.ClientMode)1