From c3a3ea7ade35a4c46166fd1fefec34b4a834aba8 Mon Sep 17 00:00:00 2001 From: cuixiuyin <1099442418@qq.com> Date: Wed, 5 Sep 2018 09:27:37 +0800 Subject: [PATCH] =?UTF-8?q?#=E7=BA=A6=E7=BA=A6=E8=B7=A8=E5=9F=8E=20#2.3.1?= =?UTF-8?q?=20#=E5=A2=9E=E5=8A=A0=E4=BB=A3=E7=A0=81=E6=B3=A8=E9=87=8A?= =?UTF-8?q?=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../netty/demo/eventloop/EventLoopSchedule.java | 8 ++++---- .../netty/demo/eventloop/ScheduleExecutor.java | 2 ++ .../protocol/ChunkedWriteHandlerInitializer.java | 16 ++-------------- .../netty/demo/protocol/FileRegionHandler.java | 12 ++---------- .../demo/protocol/MarshallingInitializer.java | 6 +----- 5 files changed, 11 insertions(+), 33 deletions(-) diff --git a/src/main/java/org/netty/demo/eventloop/EventLoopSchedule.java b/src/main/java/org/netty/demo/eventloop/EventLoopSchedule.java index d71d491..1f7de1c 100644 --- a/src/main/java/org/netty/demo/eventloop/EventLoopSchedule.java +++ b/src/main/java/org/netty/demo/eventloop/EventLoopSchedule.java @@ -13,21 +13,21 @@ public class EventLoopSchedule { public static void main(String[] args) { Channel channel = null; - //60 秒后延迟执行 + //10 秒后延迟执行 channel.eventLoop().schedule(new Runnable() { @Override public void run() { System.out.println("10 seconds later"); } - }, 10 , TimeUnit.SECONDS); + }, 10, TimeUnit.SECONDS); //每隔3秒执行一次 - ScheduledFuture schedule = channel.eventLoop().schedule(new Runnable() { + ScheduledFuture schedule = channel.eventLoop().scheduleAtFixedRate(new Runnable() { @Override public void run() { System.out.println("Run every 3 seconds"); } - }, 3, TimeUnit.SECONDS); + }, 0, 3, TimeUnit.SECONDS); //取消该任务 schedule.cancel(false); diff --git a/src/main/java/org/netty/demo/eventloop/ScheduleExecutor.java b/src/main/java/org/netty/demo/eventloop/ScheduleExecutor.java index 9b84f34..e112b5f 100644 --- a/src/main/java/org/netty/demo/eventloop/ScheduleExecutor.java +++ b/src/main/java/org/netty/demo/eventloop/ScheduleExecutor.java @@ -10,6 +10,7 @@ public class ScheduleExecutor { public static void main(String[] args) throws InterruptedException { + // 创建一个线程池,数量10 ScheduledExecutorService executorService = Executors.newScheduledThreadPool(10); executorService.schedule(new Runnable() { @Override @@ -18,6 +19,7 @@ public void run() { } }, 10, TimeUnit.SECONDS); Thread.sleep(15000); + // 关闭 ScheduledExecutorService ,释放资源 executorService.shutdown(); } diff --git a/src/main/java/org/netty/demo/protocol/ChunkedWriteHandlerInitializer.java b/src/main/java/org/netty/demo/protocol/ChunkedWriteHandlerInitializer.java index c0d5621..55245c2 100644 --- a/src/main/java/org/netty/demo/protocol/ChunkedWriteHandlerInitializer.java +++ b/src/main/java/org/netty/demo/protocol/ChunkedWriteHandlerInitializer.java @@ -23,29 +23,17 @@ public ChunkedWriteHandlerInitializer(File file, SslContext sslCtx) { this.file = file; this.sslCtx = sslCtx; } - - @Override protected void initChannel(Channel ch) throws Exception { ch.pipeline().addLast( new SslHandler(sslCtx.newEngine(ch.alloc())), - //1、要使用你自己的 ChunkedInput 实现,请在 ChannelPipeline 中安装一个 ChunkedWriteHandler - //2、添加 ChunkedWriteHandler 以处理作为 ChunkedInput 传入的数据 + // 添加 ChunkedWriteHandler 以处理作为 ChunkedInput 传入的数据 new ChunkedWriteHandler(), new WriteStreamHandler() ); - } - - private final class WriteStreamHandler extends ChannelHandlerAdapter { - - /** - * 当连接建立时,channelActive() 方法将使用 ChunkedInput 写文件数据 - * - * @param ctx - * @throws Exception - */ + //当连接建立时,channelActive() 方法将使用 ChunkedInput 写文件数据 @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { super.channelActive(ctx); diff --git a/src/main/java/org/netty/demo/protocol/FileRegionHandler.java b/src/main/java/org/netty/demo/protocol/FileRegionHandler.java index e9ab5cc..8be782b 100644 --- a/src/main/java/org/netty/demo/protocol/FileRegionHandler.java +++ b/src/main/java/org/netty/demo/protocol/FileRegionHandler.java @@ -15,6 +15,7 @@ public class FileRegionHandler extends ChannelHandlerAdapter { * 这个示例只适用于文件内容的直接传输,不包括应用程序对数据的任何处理。在需要将数据 * 从文件系统复制到用户内存中时,可以使用 ChunkedWriteHandler, 它支持异步写大型数据 * 流,而又不会导致大量的内存消耗 + * * @param ctx * @param msg * @throws Exception @@ -27,15 +28,6 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception //以该文件的完整长度创建一个新的 DefaultFileRegion DefaultFileRegion defaultFileRegion = new DefaultFileRegion(channel, 0, file.length()); //发送该 DefaultFileRegion, 并注册一个 ChannelFutureListener - ctx.writeAndFlush(defaultFileRegion).addListener( - new ChannelFutureListener() { - @Override - public void operationComplete(ChannelFuture future) throws Exception { - if (!future.isSuccess()) { - Throwable cause = future.cause(); - } - } - } - ); + ctx.writeAndFlush(defaultFileRegion).addListener(ChannelFutureListener.CLOSE_ON_FAILURE); } } diff --git a/src/main/java/org/netty/demo/protocol/MarshallingInitializer.java b/src/main/java/org/netty/demo/protocol/MarshallingInitializer.java index e828f4a..4d12c43 100644 --- a/src/main/java/org/netty/demo/protocol/MarshallingInitializer.java +++ b/src/main/java/org/netty/demo/protocol/MarshallingInitializer.java @@ -23,7 +23,6 @@ public MarshallingInitializer(MarshallerProvider marshallerProvider, Unmarshalle this.unmarshallerProvider = unmarshallerProvider; } - @Override protected void initChannel(Channel ch) throws Exception { ch.pipeline().addLast( @@ -34,10 +33,7 @@ protected void initChannel(Channel ch) throws Exception { } public static final class ObjectHandler extends SimpleChannelInboundHandler { - @Override - protected void messageReceived(ChannelHandlerContext ctx, Serializable msg) throws Exception { - - } + protected void messageReceived(ChannelHandlerContext ctx, Serializable msg) throws Exception { } } }