Asynchronous HTTP client with Netty

后端 未结 1 403
心在旅途
心在旅途 2021-02-02 02:17

I\'m new to netty and still strugling to find my way. I\'m looking to create an http client that works asynchronously. The netty examples of http only show how to wait for IO op

1条回答
  •  再見小時候
    2021-02-02 02:39

    You are using a ChannelFutureListener to do all operations in the channel (which is bad), and the future listener will be executed right after calling those channel operations.

    The problem is, After sending the message, channel is disconnected immediately and the handler can not receive the response message which comes later.

            ........
        case Sending:
            this.state = State.Disconnecting;
            future.getChannel().disconnect().addListener(this);
            break;
            ........
    

    you should not block the channel future thread at all. The best approach is extend the SimpleChannelUpstreamHandler's

        channelConnected(..) {} 
        messageReceived(..) {} 
        channelDisconnected(..) {} 
    

    methods and react to those events. you can keep the state in that handler too.

    0 讨论(0)
提交回复
热议问题