Search in sources :

Example 1 with HttpClientHandler

use of pro.tools.http.netty.handler.HttpClientHandler in project protools by SeanDragon.

the class DefaultClientPool method request.

public HttpReceive request(HttpSend httpSend, long timeout, TimeUnit timeUnit) {
    final HttpReceive httpReceive = new HttpReceive();
    Future<Channel> fch = channelPool.acquire();
    Channel channel = null;
    try {
        channel = fch.get(timeout, timeUnit);
        ChannelPipeline p = channel.pipeline();
        p.addLast(new HttpClientHandler(httpSend, httpReceive));
        final FullHttpRequest fullHttpRequest = convertRequest(httpSend);
        p.writeAndFlush(fullHttpRequest);
        channel.closeFuture().await(timeout, timeUnit);
        if (!httpReceive.getIsDone()) {
            httpReceive.setHaveError(true);
            httpReceive.setErrMsg("请求已经超时");
        }
    } catch (Exception e) {
        if (log.isWarnEnabled()) {
            log.warn(e.getMessage(), e);
        }
        httpReceive.setHaveError(true).setErrMsg(e.getMessage()).setThrowable(e).setIsDone(true);
    } finally {
        if (channel != null) {
            channelPool.release(channel);
        }
    }
    return httpReceive;
}
Also used : HttpReceive(pro.tools.http.pojo.HttpReceive) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) HttpClientHandler(pro.tools.http.netty.handler.HttpClientHandler) ChannelPipeline(io.netty.channel.ChannelPipeline) HttpException(pro.tools.http.pojo.HttpException) URISyntaxException(java.net.URISyntaxException) SSLException(javax.net.ssl.SSLException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

Channel (io.netty.channel.Channel)1 ChannelPipeline (io.netty.channel.ChannelPipeline)1 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)1 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)1 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URISyntaxException (java.net.URISyntaxException)1 SSLException (javax.net.ssl.SSLException)1 HttpClientHandler (pro.tools.http.netty.handler.HttpClientHandler)1 HttpException (pro.tools.http.pojo.HttpException)1 HttpReceive (pro.tools.http.pojo.HttpReceive)1