netty client 连接超时设置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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。*网络不通,故到时抛超时异常)<br>
            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非超时异常

posted on   silyvin  阅读(23884)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示