reactor模型了解

reactor模型 特点 事件驱动 可以处理一个或多个输入源 通过多路复用将请求的事件分发给对应的处理器处理 根据大神Doug Lea 在 《Scalable IO in Java 》中的介绍,Reacotr模型主要分为三个角色 ...

February 10, 2022 · 3 min

netty利用websocket协议开发服务端

介绍websocket协议 WebSocket是HTML5开始提供的一种浏览器与服务器间进行全双工通信的网络技术,WebSocket通信协议于2011年被IETF定为 标准RFC6455,WebSocket API被W3C定为标准。 ...

February 9, 2022 · 4 min

使用netty作为http文件服务

代码 HttpFileServer package http; import echo.server.EchoServer; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; 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.channel.socket.nio.NioSocketChannel; import io.netty.handler.codec.http.*; import io.netty.handler.stream.ChunkedWriteHandler; /** * @author ggBall * @version 1.0.0 * @ClassName HttpFileServer.java * @Description TODO * @createTime 2022年02月08日 09:41:00 */ public class HttpFileServer { private static final String default_url = "/src/main/java/http/file/netty/"; public void run(final int port , final String url) throws InterruptedException { EventLoopGroup boss = new NioEventLoopGroup(); EventLoopGroup worker = new NioEventLoopGroup(); try { ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(boss,worker) .channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { /** * This method will be called once the {@link Channel} was registered. After the method returns this instance * will be removed from the {@link ChannelPipeline} of the {@link Channel}. * * @param ch the {@link Channel} which was registered. * @throws Exception is thrown if an error occurs. In that case the {@link Channel} will be closed. */ protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast("http-decoder", new HttpRequestDecoder()); // 请求消息解码器 ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(65536));// 目的是将多个消息转换为单一的request或者response对象 ch.pipeline().addLast("http-encoder", new HttpResponseEncoder());//响应解码器 ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler());//目的是支持异步大文件传输() ch.pipeline().addLast("fileServerHandler", new HttpFileServerHandler(url));// 业务逻辑 } }); ChannelFuture future = bootstrap.bind("127.0.0.1", port).sync(); System.out.println("http文件服务器,地址:http://127.0.0.1:" + port + url); future.channel().closeFuture().sync(); } finally { boss.shutdownGracefully(); worker.shutdownGracefully(); } } public static void main(String[] args) throws Exception { int port = 8080; if (args.length > 0) { try { port = Integer.parseInt(args[0]); } catch (NumberFormatException e) { e.printStackTrace(); } } String url = default_url; if (args.length > 1) url = args[1]; new HttpFileServer().run(port, url); } } HttpFileServerHandler ...

February 8, 2022 · 3 min

io入门了解

Java1.4之前的早期版本,Java对I/O的支持并不完善,开发人员在开发高性能I/O程序的时候,会面临一些巨大的挑战和困难,主要问题如下: ...

January 15, 2022 · 1 min

javaNIO了解

流与块 I/O 与 NIO 最重要的区别是数据打包和传输的方式,I/O 以流的方式处理数据,而 NIO 以块的方式处理数据。 面向流的 I/O 一次处理一个字节数据: 一个输入流产生一个字节数据,一个输出流消费一个字节数据。为流式数据创建过滤器非常容易,链接几个过滤器,以便每个过滤器只负责复杂处理机制的一部分。不利的一面是,面向流的 I/O 通常相当慢。 面向块的 I/O 一次处理一个数据块,按块处理数据比按流处理数据要快得多。但是面向块的 I/O 缺少一些面向流的 I/O 所具有的优雅性和简单性。 I/O 包和 NIO 已经很好地集成了,java.io.* 已经以 NIO 为基础重新实现了,所以现在它可以利用 NIO 的一些特性。例如,java.io.* 包中的一些类包含以块的形式读写数据的方法,这使得即使在面向流的系统中,处理速度也会更快。 ...

January 15, 2022 · 3 min
总访问量 总访客数