Skip to content

Commit

Permalink
#约约跨城 #2.3.1 #增加代码注释说明
Browse files Browse the repository at this point in the history
  • Loading branch information
JMCuixy committed Sep 5, 2018
1 parent 6eef986 commit c3a3ea7
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 33 deletions.
8 changes: 4 additions & 4 deletions src/main/java/org/netty/demo/eventloop/EventLoopSchedule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/netty/demo/eventloop/ScheduleExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,6 +19,7 @@ public void run() {
}
}, 10, TimeUnit.SECONDS);
Thread.sleep(15000);
// 关闭 ScheduledExecutorService ,释放资源
executorService.shutdown();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 2 additions & 10 deletions src/main/java/org/netty/demo/protocol/FileRegionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class FileRegionHandler extends ChannelHandlerAdapter {
* 这个示例只适用于文件内容的直接传输,不包括应用程序对数据的任何处理。在需要将数据
* 从文件系统复制到用户内存中时,可以使用 ChunkedWriteHandler, 它支持异步写大型数据
* 流,而又不会导致大量的内存消耗
*
* @param ctx
* @param msg
* @throws Exception
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public MarshallingInitializer(MarshallerProvider marshallerProvider, Unmarshalle
this.unmarshallerProvider = unmarshallerProvider;
}


@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast(
Expand All @@ -34,10 +33,7 @@ protected void initChannel(Channel ch) throws Exception {
}

public static final class ObjectHandler extends SimpleChannelInboundHandler<Serializable> {

@Override
protected void messageReceived(ChannelHandlerContext ctx, Serializable msg) throws Exception {

}
protected void messageReceived(ChannelHandlerContext ctx, Serializable msg) throws Exception { }
}
}

0 comments on commit c3a3ea7

Please sign in to comment.