Netty权威指南 第2版学习笔记10——实现HTTP服务
来源:互联网 发布:手机淘宝app流量软件 编辑:程序博客网 时间:2024/05/19 09:37
HttpServer
import org.apache.log4j.Logger;import io.netty.bootstrap.ServerBootstrap;import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel;import io.netty.handler.codec.http.HttpRequestDecoder; import io.netty.handler.codec.http.HttpResponseEncoder; public class HttpServer { private static Logger log = Logger.getLogger(HttpServer.class); public void start(int port) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { // server端发送的是httpResponse,所以要使用HttpResponseEncoder进行编码 ch.pipeline().addLast(new HttpResponseEncoder()); // server端接收到的是httpRequest,所以要使用HttpRequestDecoder进行解码 ch.pipeline().addLast(new HttpRequestDecoder()); ch.pipeline().addLast(new HttpServerInboundHandler()); } }).option(ChannelOption.SO_BACKLOG, 128) .childOption(ChannelOption.SO_KEEPALIVE, true); ChannelFuture f = b.bind(port).sync(); f.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } } public static void main(String[] args) throws Exception { HttpServer server = new HttpServer(); log.info("Http Server listening on 8844 ..."); server.start(8844); }}
HttpServerInboundHandler
import static io.netty.handler.codec.http.HttpHeaders.Names.CONNECTION;import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_LENGTH;import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_TYPE;import static io.netty.handler.codec.http.HttpResponseStatus.OK;import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;import org.apache.log4j.Logger;import io.netty.buffer.ByteBuf;import io.netty.buffer.Unpooled;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.ChannelInboundHandlerAdapter;import io.netty.handler.codec.http.DefaultFullHttpResponse;import io.netty.handler.codec.http.FullHttpResponse;import io.netty.handler.codec.http.HttpContent;import io.netty.handler.codec.http.HttpHeaders;import io.netty.handler.codec.http.HttpHeaders.Values;import io.netty.handler.codec.http.HttpRequest;@SuppressWarnings("deprecation")public class HttpServerInboundHandler extends ChannelInboundHandlerAdapter { private static Logger log = Logger.getLogger(HttpServerInboundHandler.class); private HttpRequest request; @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (msg instanceof HttpRequest) { request = (HttpRequest) msg; String uri = request.getUri(); System.out.println("Uri:" + uri); } if (msg instanceof HttpContent) { HttpContent content = (HttpContent) msg; ByteBuf buf = content.content(); System.out.println(buf.toString(io.netty.util.CharsetUtil.UTF_8)); buf.release(); String res = "Hello World"; FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1,OK, Unpooled.wrappedBuffer(res.getBytes("UTF-8"))); response.headers().set(CONTENT_TYPE, "text/plain"); response.headers().set(CONTENT_LENGTH,response.content().readableBytes()); if (HttpHeaders.isKeepAlive(request)) { response.headers().set(CONNECTION, Values.KEEP_ALIVE); } ctx.write(response); ctx.flush(); } } @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { ctx.flush(); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { log.error(cause.getMessage()); ctx.close(); }}
0 0
- Netty权威指南 第2版学习笔记10——实现HTTP服务
- Netty权威指南 第2版学习笔记3——Netty NIO开发指南
- Netty权威指南 第2版学习笔记11——实现WebSocket
- Netty权威指南 第2版学习笔记2——NIO入门
- Netty权威指南 第2版学习笔记7——MessagePack编解码及LengthFieldBasedFrameDecoder
- Netty权威指南 第2版学习笔记9——Channel,Unsafe和 AttributeMap
- netty权威指南 学习笔记http
- Netty权威指南 第2版学习笔记1——Java的I/O演进之路
- Netty权威指南 第2版学习笔记4——TCP粘包/拆包问题的解决之道
- Netty权威指南 第2版学习笔记5——分隔符和定长解码器的应用
- Netty权威指南 第2版学习笔记6——常见序列化与反序列化框架
- Netty权威指南 第2版学习笔记8——Google Protobuf编解码(未写)
- 《netty权威指南》学习笔记2
- Netty权威指南(第2版)
- 《HTTP权威指南》学习笔记(一)—HTTP概述
- 《HTTP权威指南》学习笔记(三)—HTTP报文
- Android编程权威指南(第二版)学习笔记(二十六)—— 第26章 后台服务
- Netty权威指南学习笔记1
- ScrollView嵌套recyclerView出现的滑动问题
- MSF初体验—入侵安卓手机
- html对齐
- java九九乘法表
- Linux内核分析课程--构造一个简单的Linux系统MenuOS
- Netty权威指南 第2版学习笔记10——实现HTTP服务
- 数据库逆向框架代码生成工具:MyBatis Generator的使用
- npm Webpack
- 接口和抽象类的区别
- 第8章 多态
- caffe之绘制网络模型
- 水陆距离 搜索BFS
- 同时安装python2和python3的pip使用问题
- OpenGL——粒子系统