java异步io netty_基于netty的异步http请求

packagecom.pt.utils;importio.netty.bootstrap.Bootstrap;importio.netty.channel.ChannelFuture;importio.netty.channel.ChannelInitializer;importio.netty.channel.ChannelOption;importio.netty.channel.EventLoopGroup;importio.netty.channel.nio.NioEventLoopGroup;importio.netty.channel.socket.SocketChannel;importio.netty.channel.socket.nio.NioSocketChannel;import io.netty.handler.codec.http.*;importio.netty.util.concurrent.DefaultEventExecutorGroup;importio.netty.util.concurrent.EventExecutorGroup;importjava.net.URI;importjava.util.Map;/***@authorpanteng

* @description

* @date 17-3-20.*/

public classNonBlockHttpClient {public static EventLoopGroup workerGroup = new NioEventLoopGroup(1);public static Bootstrap b = newBootstrap();public static final EventExecutorGroup executor = new DefaultEventExecutorGroup(2);static{

b.group(workerGroup);

b.channel(NioSocketChannel.class);

b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS,1000);

}public static Object lock = newObject();/*** 异步GET请求

*

*@paramurl

*@paramhead

*@paramhandler

*@return

*/

public static Boolean get(String url, Map head, finalHttpHandler handler) {try{

URI uri= newURI(url);

String domain=uri.getHost();

Integer port= uri.getPort() < 0 ? 80: uri.getPort();

DefaultFullHttpRequest request= newDefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri.toASCIIString());if (head == null) {

request.headers().add("Host", domain);

request.headers().add("User-Agent", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.0");

request.headers().add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");

request.headers().add("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");

request.headers().add("Connection", "keep-alive");

request.headers().add("Cache-Control", "max-age=0");

}else{for(Map.Entry entry : head.entrySet()) {

request.headers().add((String) entry.getKey(), entry.getValue());

}

}

ChannelInitializer channelInitializer= new ChannelInitializer() {

@Overrideprotected void initChannel(SocketChannel socketChannel) throwsException {//客户端接收到的是httpResponse响应,所以要使用HttpResponseDecoder进行解码

socketChannel.pipeline().addLast(newHttpResponseDecoder());//客户端发送的是httprequest,所以要使用HttpRequestEncoder进行编码

socketChannel.pipeline().addLast(newHttpRequestEncoder());

socketChannel.pipeline().addLast(executor,newGeneralHandler(handler));

}

};

ChannelFuture f;synchronized(lock) {

b.handler(channelInitializer);

f=b.connect(domain, port).sync();

}

f.channel().writeAndFlush(request);

}catch(Exception e) {

e.printStackTrace();

}return false;

}public static voidclose() {try{

executor.shutdownGracefully();

workerGroup.shutdownGracefully();

}catch(Exception e) {

e.printStackTrace();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值