|
| 1 | +package java0.nio01.netty; |
| 2 | + |
| 3 | +import io.netty.buffer.Unpooled; |
| 4 | +import io.netty.channel.ChannelFutureListener; |
| 5 | +import io.netty.channel.ChannelHandlerContext; |
| 6 | +import io.netty.channel.ChannelInboundHandlerAdapter; |
| 7 | +import io.netty.handler.codec.http.DefaultFullHttpResponse; |
| 8 | +import io.netty.handler.codec.http.FullHttpRequest; |
| 9 | +import io.netty.handler.codec.http.FullHttpResponse; |
| 10 | +import io.netty.handler.codec.http.HttpUtil; |
| 11 | +import io.netty.util.ReferenceCountUtil; |
| 12 | +import okhttp3.OkHttpClient; |
| 13 | +import okhttp3.Request; |
| 14 | +import okhttp3.Response; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | + |
| 18 | +import static io.netty.handler.codec.http.HttpHeaderNames.CONNECTION; |
| 19 | +import static io.netty.handler.codec.http.HttpHeaderValues.KEEP_ALIVE; |
| 20 | +import static io.netty.handler.codec.http.HttpResponseStatus.NO_CONTENT; |
| 21 | +import static io.netty.handler.codec.http.HttpResponseStatus.OK; |
| 22 | +import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; |
| 23 | + |
| 24 | +public class HttpHandler extends ChannelInboundHandlerAdapter { |
| 25 | + |
| 26 | + final OkHttpClient CLIENT = new OkHttpClient(); |
| 27 | + final String TARGET_URL = "http://localhost:8801"; |
| 28 | + |
| 29 | + @Override |
| 30 | + public void channelReadComplete(ChannelHandlerContext ctx) { |
| 31 | + ctx.flush(); |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public void channelRead(ChannelHandlerContext ctx, Object msg) { |
| 36 | + try { |
| 37 | + //logger.info("channelRead流量接口请求开始,时间为{}", startTime); |
| 38 | + FullHttpRequest fullRequest = (FullHttpRequest) msg; |
| 39 | + |
| 40 | + handlerTest(fullRequest, ctx); |
| 41 | + |
| 42 | + } catch(Exception e) { |
| 43 | + e.printStackTrace(); |
| 44 | + } finally { |
| 45 | + ReferenceCountUtil.release(msg); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private void handlerTest(FullHttpRequest fullRequest, ChannelHandlerContext ctx) { |
| 50 | + FullHttpResponse response = null; |
| 51 | + try { |
| 52 | + String value = getUrl(TARGET_URL); // 对接上次作业的httpclient或者okhttp请求另一个url的响应数据 |
| 53 | + |
| 54 | +// httpGet ... http://localhost:8801 |
| 55 | +// 返回的响应,"hello,nio"; |
| 56 | +// value = reponse.... |
| 57 | + |
| 58 | + response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(value.getBytes("UTF-8"))); |
| 59 | + response.headers().set("Content-Type", "application/json"); |
| 60 | + response.headers().setInt("Content-Length", response.content().readableBytes()); |
| 61 | + |
| 62 | + } catch (Exception e) { |
| 63 | + System.out.println("处理出错:"+e.getMessage()); |
| 64 | + response = new DefaultFullHttpResponse(HTTP_1_1, NO_CONTENT); |
| 65 | + } finally { |
| 66 | + if (fullRequest != null) { |
| 67 | + if (!HttpUtil.isKeepAlive(fullRequest)) { |
| 68 | + ctx.write(response).addListener(ChannelFutureListener.CLOSE); |
| 69 | + } else { |
| 70 | + response.headers().set(CONNECTION, KEEP_ALIVE); |
| 71 | + ctx.write(response); |
| 72 | + } |
| 73 | + ctx.flush(); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { |
| 80 | + cause.printStackTrace(); |
| 81 | + ctx.close(); |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + String getUrl(String url) throws IOException { |
| 86 | + Request request = new Request.Builder() |
| 87 | + .url(url) |
| 88 | + .build(); |
| 89 | + try (Response response = CLIENT.newCall(request).execute()) { |
| 90 | + return response.body().string(); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments