Example usage for io.netty.handler.timeout ReadTimeoutHandler ReadTimeoutHandler

List of usage examples for io.netty.handler.timeout ReadTimeoutHandler ReadTimeoutHandler

Introduction

In this page you can find the example usage for io.netty.handler.timeout ReadTimeoutHandler ReadTimeoutHandler.

Prototype

public ReadTimeoutHandler(long timeout, TimeUnit unit) 

Source Link

Document

Creates a new instance.

Usage

From source file:socketServer.client.SocketClient.java

License:Apache License

public void connect(final String host, final int port) throws Exception {
    // ?NIO//  www . j  ava  2 s.  co  m
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new ChargeStationMessageDecoder(1024 * 1024, 1, 2, -3, 0));
                        ch.pipeline().addLast("MessageEncoder", new ChargeStationMessageEncoder());
                        ch.pipeline().addLast("readTimeoutHandler",
                                new ReadTimeoutHandler(100, TimeUnit.SECONDS));
                        ch.pipeline().addLast("ChargeStationEventHandler", new ChargeStationEventHandler());
                    }
                });
        // ??
        b.bind(new InetSocketAddress("127.0.0.1", 30302));
        ChannelFuture future = b.connect(new InetSocketAddress(host, port)).sync();
        future.channel().closeFuture().sync();

    } finally {
        // ?????
        try {
            TimeUnit.SECONDS.sleep(10);
            try {
                connect(host, port); // ???
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}