netty client 連接超時設置


public class Client4 {

    public static void main(String[] args) {

        //worker負責讀寫數據
        EventLoopGroup worker = new NioEventLoopGroup();
        long st = System.currentTimeMillis();

        try {
            //輔助啟動類
            Bootstrap bootstrap = new Bootstrap();

            //設置線程池
            bootstrap.group(worker);

            //設置socket工廠
            bootstrap.channel(NioSocketChannel.class);

            /**
             * *******************************************************************
             * 如果不設置超時,連接會一直占用本地線程,端口,連接客戶端一多,會導致本地端口用盡及CPU壓力
             */
            bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000);

            //設置管道
            bootstrap.handler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel socketChannel) throws Exception {
                    //獲取管道
                    ChannelPipeline pipeline = socketChannel.pipeline();
                    pipeline.addLast(new LengthFieldBasedFrameDecoder(10000, 0, 4, 0, 4));
                    pipeline.addLast(new ProtobufDecoder(MyBaseProto.BaseProto.getDefaultInstance()));
                    pipeline.addLast(new LengthFieldPrepender(4));
                    pipeline.addLast(new ProtobufEncoder());

                    pipeline.addLast(new IdleStateHandler(61, 30, 0, TimeUnit.SECONDS));
                    pipeline.addLast(new ClientHeartbeatHandler());

                    //處理類
                    pipeline.addLast(new ClientHandler4Heart());
                }
            });

            //發起異步連接操作(
// 如果是127.0.0.1,則會直接拋連接異常,由於192。*網絡不通,故到時拋超時異常

ChannelFuture futrue = bootstrap.connect(new InetSocketAddress("192.168.1.1",8866)).sync(); //等待客戶端鏈路關閉 futrue.channel().closeFuture().sync(); } catch (Exception e) { e.printStackTrace(); long ed = System.currentTimeMillis(); System.out.println((ed-st)/1000); } finally { //優雅的退出,釋放NIO線程組 worker.shutdownGracefully(); } } }

 

 

/**
* *******************************************************************
* 如果不設置超時,連接會一直占用本地線程,端口,連接客戶端一多,阻塞在那里,會導致本地端口用盡及CPU壓力
*/
bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000);


未設置超時:

30
io.netty.channel.ConnectTimeoutException: connection timed out: /192.168.1.1:8866
at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe$1.run(AbstractNioChannel.java:206)
at io.netty.util.concurrent.PromiseTask$RunnableAdapter.call(PromiseTask.java:38)
at io.netty.util.concurrent.ScheduledFutureTask.run(ScheduledFutureTask.java:123)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:354)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:353)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:101)
at java.lang.Thread.run(Thread.java:745)

Process finished with exit code 0

設置后:

5
io.netty.channel.ConnectTimeoutException: connection timed out: /192.168.1.1:8866
at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe$1.run(AbstractNioChannel.java:206)
at io.netty.util.concurrent.PromiseTask$RunnableAdapter.call(PromiseTask.java:38)
at io.netty.util.concurrent.ScheduledFutureTask.run(ScheduledFutureTask.java:123)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:354)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:353)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:101)
at java.lang.Thread.run(Thread.java:745)

Process finished with exit code 0

 

 

192.*這個ip由於網絡不通,觸發的是超時異常,如果是網絡通的,但是服務不通,則會

java.net.ConnectException: Connection refused: /127.0.0.1:8866
0
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:191)
at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:279)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:511)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:461)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:378)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:350)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:101)
at java.lang.Thread.run(Thread.java:745)

Process finished with exit code 0

立馬拋出異常,ConnectException非超時異常


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM