`
zhaohaolin
  • 浏览: 985408 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

netty心跳代码【转】

 
阅读更多

类:ServerNIOHandler 别看名字是server它其实是客户端的handler,这个我是用来做反向连接的。所有handler的超类

import java.net.ConnectException;
import java.util.concurrent.TimeUnit;

import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.jboss.netty.channel.ChannelHandler.Sharable;
import org.jboss.netty.util.Timeout;
import org.jboss.netty.util.Timer;
import org.jboss.netty.util.TimerTask;

import com.zrat.server.nio.factory.ServerPipelineFactory;
@Sharable
public abstract class ServerNIOHandler extends SimpleChannelUpstreamHandler{
    final ClientBootstrap bootstrap;
    private final Timer timer;
    private long startTime = -1;

    public ServerNIOHandler(ClientBootstrap bootstrap, Timer timer) {
        this.bootstrap = bootstrap;
        this.timer = timer;
    }
   
    @Override
    public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) {
        timer.newTimeout(new TimerTask() {
            public void run(Timeout timeout) throws Exception {
                bootstrap.connect();
            }
        }, ServerPipelineFactory.RECONNECT_DELAY, TimeUnit.SECONDS);
    }

    @Override
    public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {
        if (startTime < 0) {
            startTime = System.currentTimeMillis();
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
        Throwable cause = e.getCause();
        if (cause instanceof ConnectException) {
            startTime = -1;
            ctx.getChannel().close();
        }
        e.getCause().printStackTrace();
       
    }

}

类:ServerPipelineFactory 管道工厂类

import static org.jboss.netty.channel.Channels.pipeline;
import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.handler.codec.serialization.ObjectDecoder;
import org.jboss.netty.handler.codec.serialization.ObjectEncoder;
import org.jboss.netty.util.Timer;

import com.zrat.server.nio.handler.CommandHandler;
import com.zrat.server.nio.handler.FileHandler;

public class ServerPipelineFactory implements ChannelPipelineFactory {

    private Timer timer;
    private ClientBootstrap bootstrap;
   
    public static final int RECONNECT_DELAY = 5;
   
    public ServerPipelineFactory(ClientBootstrap bootstrap,Timer timer){
        super();
        this.timer = timer;
        this.bootstrap = bootstrap;
    }
   
    public ChannelPipeline getPipeline() throws Exception {
        ChannelPipeline pipeline = pipeline();

        pipeline.addLast("decoder", new ObjectDecoder());
        pipeline.addLast("encoder", new ObjectEncoder());

        pipeline.addLast("fileHandler", new FileHandler(bootstrap,timer));
        pipeline.addLast("commandHandler", new CommandHandler(bootstrap,timer));
        return pipeline;
    }
}

调用方法:

public void start(){
        final Timer timer = new HashedWheelTimer();
        final ClientBootstrap bootstrap = new ClientBootstrap(
                new NioClientSocketChannelFactory(
                        Executors.newCachedThreadPool(),
                        Executors.newCachedThreadPool()));
        bootstrap.setPipelineFactory(new ServerPipelineFactory(bootstrap,timer));

        bootstrap.setOption(
                "remoteAddress", new InetSocketAddress(host, port));
        bootstrap.connect();
    }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics