
com.wandoulabs.nedis.NedisClientBuilder Maven / Gradle / Ivy
/**
* Copyright (c) 2015 Wandoujia Inc.
*
* 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 com.wandoulabs.nedis;
import static com.wandoulabs.nedis.util.NedisUtils.DEFAULT_REDIS_PORT;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.Promise;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.concurrent.TimeUnit;
import com.wandoulabs.nedis.handler.RedisDuplexHandler;
import com.wandoulabs.nedis.handler.RedisResponseDecoder;
import com.wandoulabs.nedis.util.AbstractNedisBuilder;
/**
* @author Apache9
*/
public class NedisClientBuilder extends AbstractNedisBuilder {
private NedisClientPool pool;
@Override
public NedisClientBuilder group(EventLoopGroup group) {
super.group(group);
return this;
}
@Override
public NedisClientBuilder channel(Class extends Channel> channelClass) {
super.channel(channelClass);
return this;
}
@Override
public NedisClientBuilder timeoutMs(long timeoutMs) {
super.timeoutMs(timeoutMs);
return this;
}
public NedisClientBuilder belongTo(NedisClientPool pool) {
this.pool = pool;
return this;
}
public Future connect(String host) {
return connect(host, DEFAULT_REDIS_PORT);
}
public Future connect(String inetHost, int inetPort) {
return connect(new InetSocketAddress(inetHost, inetPort));
}
public Future connect(SocketAddress remoteAddress) {
validateGroupConfig();
Bootstrap b = new Bootstrap().group(group).channel(channelClass)
.handler(new ChannelInitializer() {
@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast(new RedisResponseDecoder(),
new RedisDuplexHandler(TimeUnit.MILLISECONDS.toNanos(timeoutMs)));
}
});
if (timeoutMs > 0) {
b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS,
(int) Math.min(Integer.MAX_VALUE, timeoutMs));
}
ChannelFuture f = b.connect(remoteAddress);
final Promise promise = f.channel().eventLoop().newPromise();
f.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
promise.trySuccess(new NedisClientImpl(future.channel(), pool));
} else {
promise.tryFailure(future.cause());
}
}
});
return promise;
}
private NedisClientBuilder() {}
public static NedisClientBuilder create() {
return new NedisClientBuilder();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy