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

org.redisson.client.handler.BaseConnectionHandler Maven / Gradle / Ivy

There is a newer version: 3.45.1
Show newest version
/**
 * Copyright 2016 Nikita Koksharov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.redisson.client.handler;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import org.redisson.api.RFuture;
import org.redisson.client.RedisClient;
import org.redisson.client.RedisClientConfig;
import org.redisson.client.RedisConnection;
import org.redisson.client.RedisLoadingException;
import org.redisson.client.protocol.RedisCommands;
import org.redisson.misc.RPromise;
import org.redisson.misc.RedissonPromise;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.FutureListener;

/**
 * 
 * @author Nikita Koksharov
 *
 */
public abstract class BaseConnectionHandler extends ChannelInboundHandlerAdapter {

    final RedisClient redisClient;
    final RPromise connectionPromise = new RedissonPromise();
    C connection;
    
    public BaseConnectionHandler(RedisClient redisClient) {
        super();
        this.redisClient = redisClient;
    }

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        if (connection == null) {
            connection = createConnection(ctx);
        }
        super.channelRegistered(ctx);
    }

    abstract C createConnection(ChannelHandlerContext ctx);
    
    @Override
    public void channelActive(final ChannelHandlerContext ctx) {
        List> futures = new ArrayList>();

        RedisClientConfig config = redisClient.getConfig();
        if (config.getPassword() != null) {
            RFuture future = connection.async(RedisCommands.AUTH, config.getPassword());
            futures.add(future);
        }
        if (config.getDatabase() != 0) {
            RFuture future = connection.async(RedisCommands.SELECT, config.getDatabase());
            futures.add(future);
        }
        if (config.getClientName() != null) {
            RFuture future = connection.async(RedisCommands.CLIENT_SETNAME, config.getClientName());
            futures.add(future);
        }
        if (config.isReadOnly()) {
            RFuture future = connection.async(RedisCommands.READONLY);
            futures.add(future);
        }
        if (config.getPingConnectionInterval() > 0) {
            RFuture future = connection.async(RedisCommands.PING);
            futures.add(future);
        }
        
        if (futures.isEmpty()) {
            ctx.fireChannelActive();
            connectionPromise.trySuccess(connection);
            return;
        }
        
        final AtomicBoolean retry = new AtomicBoolean();
        final AtomicInteger commandsCounter = new AtomicInteger(futures.size());
        for (RFuture future : futures) {
            future.addListener(new FutureListener() {
                @Override
                public void operationComplete(Future future) throws Exception {
                    if (!future.isSuccess()) {
                        if (future.cause() instanceof RedisLoadingException) {
                            if (retry.compareAndSet(false, true)) {
                                ctx.executor().schedule(new Runnable() {
                                    @Override
                                    public void run() {
                                        channelActive(ctx);
                                    }
                                }, 1, TimeUnit.SECONDS);
                            }
                            return;
                        }
                        connection.closeAsync();
                        connectionPromise.tryFailure(future.cause());
                        return;
                    }
                    if (commandsCounter.decrementAndGet() == 0) {
                        ctx.fireChannelActive();
                        connectionPromise.trySuccess(connection);
                    }
                }
            });
        }
    }
    
}