├── NioTest10.txt
├── gen-py
├── __init__.py
└── py
│ ├── __init__.py
│ └── thrift
│ ├── __init__.py
│ └── generated
│ ├── __init__.py
│ ├── constants.py
│ ├── PersonService-remote
│ ├── ttypes.py
│ └── PersonService.py
├── NioTest2.txt
├── NioTest9.txt
├── NioTest3.txt
├── input2.txt
├── output2.txt
├── .gitignore
├── settings.gradle
├── input.txt
├── output.txt
├── NioTest13_In.txt
├── NioTest13_Out.txt
├── README.md
└── src
├── main
├── java
│ └── com
│ │ ├── shengsiyuan
│ │ ├── decorator
│ │ │ ├── Component.java
│ │ │ ├── ConcreteComponent.java
│ │ │ ├── Client.java
│ │ │ ├── Decorator.java
│ │ │ ├── ConcreteDecorator1.java
│ │ │ └── ConcreteDecorator2.java
│ │ ├── proto
│ │ │ ├── StudentRequestOrBuilder.java
│ │ │ ├── MyRequestOrBuilder.java
│ │ │ ├── MyResponseOrBuilder.java
│ │ │ ├── StreamRequestOrBuilder.java
│ │ │ ├── StreamResponseOrBuilder.java
│ │ │ ├── StudentResponseOrBuilder.java
│ │ │ ├── StudentResponseListOrBuilder.java
│ │ │ ├── StudentProto.java
│ │ │ ├── StudentRequest.java
│ │ │ ├── StudentServiceGrpc.java
│ │ │ └── MyRequest.java
│ │ ├── netty
│ │ │ ├── test
│ │ │ │ └── Test.java
│ │ │ ├── thirdexample
│ │ │ │ ├── MyChatClientHandler.java
│ │ │ │ ├── MyChatClientInitializer.java
│ │ │ │ ├── MyChatServerInitializer.java
│ │ │ │ ├── MyChatServer.java
│ │ │ │ ├── MyChatClient.java
│ │ │ │ └── MyChatServerHandler.java
│ │ │ ├── handler2
│ │ │ │ ├── MyClientInitializer.java
│ │ │ │ ├── MyServerInitializer.java
│ │ │ │ ├── MyClient.java
│ │ │ │ ├── MyServer.java
│ │ │ │ ├── MyServerHandler.java
│ │ │ │ └── MyClientHandler.java
│ │ │ ├── handler3
│ │ │ │ ├── PersonProtocol.java
│ │ │ │ ├── MyPersonEncoder.java
│ │ │ │ ├── MyServerInitializer.java
│ │ │ │ ├── MyPersonDecoder.java
│ │ │ │ ├── MyServer.java
│ │ │ │ ├── MyServerHandler.java
│ │ │ │ ├── MyClient.java
│ │ │ │ └── MyClientHandler.java
│ │ │ ├── handler
│ │ │ │ ├── MyLongToStringDecoder.java
│ │ │ │ ├── MyLongToByteEncoder.java
│ │ │ │ ├── MyByteToLongDecoder2.java
│ │ │ │ ├── MyClientInitializer.java
│ │ │ │ ├── MyByteToLongDecoder.java
│ │ │ │ ├── MyServerInitializer.java
│ │ │ │ ├── MyServerHandler.java
│ │ │ │ ├── MyClient.java
│ │ │ │ ├── MyClientHandler.java
│ │ │ │ └── MyServer.java
│ │ │ ├── bytebuf
│ │ │ │ ├── ByteBufTest0.java
│ │ │ │ ├── ByteBufTest2.java
│ │ │ │ ├── ByteBufTest1.java
│ │ │ │ └── AtomicUpdaterTest.java
│ │ │ ├── firstexample
│ │ │ │ ├── TestServerInitializer.java
│ │ │ │ ├── TestServer.java
│ │ │ │ └── TestHttpServerHandler.java
│ │ │ ├── fourthexample
│ │ │ │ ├── MyServerInitializer.java
│ │ │ │ ├── MyServerHandler.java
│ │ │ │ └── MyServer.java
│ │ │ ├── secondexample
│ │ │ │ ├── MyServerHandler.java
│ │ │ │ ├── MyClient.java
│ │ │ │ ├── MyClientHandler.java
│ │ │ │ ├── MyClientInitializer.java
│ │ │ │ ├── MyServerInitializer.java
│ │ │ │ └── MyServer.java
│ │ │ ├── sixthexample
│ │ │ │ ├── TestClient.java
│ │ │ │ ├── TestClientInitializer.java
│ │ │ │ ├── TestServerInitializer.java
│ │ │ │ ├── TestServerHandler.java
│ │ │ │ ├── TestServer.java
│ │ │ │ └── TestClientHandler.java
│ │ │ └── fifthexample
│ │ │ │ ├── WebSocketChannelInitializer.java
│ │ │ │ ├── TextWebSocketFrameHandler.java
│ │ │ │ └── MyServer.java
│ │ ├── protobuf
│ │ │ └── ProtoBufTest.java
│ │ ├── nio
│ │ │ ├── NioTest9.java
│ │ │ ├── NioTest10.java
│ │ │ ├── NioTest7.java
│ │ │ ├── NioTest2.java
│ │ │ ├── NioTest3.java
│ │ │ ├── NioTest5.java
│ │ │ ├── NioTest6.java
│ │ │ ├── NioTest1.java
│ │ │ ├── NioTest8.java
│ │ │ ├── NioTest4.java
│ │ │ ├── NioTest13.java
│ │ │ ├── NioTest11.java
│ │ │ ├── NioTest12.java
│ │ │ ├── NioClient.java
│ │ │ └── NioServer.java
│ │ ├── thrift
│ │ │ ├── PersonServiceImpl.java
│ │ │ ├── ThriftServer.java
│ │ │ └── ThriftClient.java
│ │ └── grpc
│ │ │ ├── GrpcServer.java
│ │ │ ├── StudentServiceImpl.java
│ │ │ └── GrpcClient.java
│ │ └── shensiyuan
│ │ └── zerocopy
│ │ ├── OldIOServer.java
│ │ ├── NewIOClient.java
│ │ ├── OldIOClient.java
│ │ └── NewIOServer.java
├── resources
│ └── log4j.properties
└── proto
│ └── Student.proto
├── thrift
└── data.thrift
├── protobuf
└── Person.proto
└── webapp
└── test.html
/NioTest10.txt:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/gen-py/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/gen-py/py/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/gen-py/py/thrift/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/NioTest2.txt:
--------------------------------------------------------------------------------
1 | hello world welcome
--------------------------------------------------------------------------------
/NioTest9.txt:
--------------------------------------------------------------------------------
1 | aelboworld
2 | welcome
--------------------------------------------------------------------------------
/NioTest3.txt:
--------------------------------------------------------------------------------
1 | hello world welcome, nihao
2 |
--------------------------------------------------------------------------------
/input2.txt:
--------------------------------------------------------------------------------
1 | hello world
2 | welcome
3 | zhangsan
--------------------------------------------------------------------------------
/output2.txt:
--------------------------------------------------------------------------------
1 | hello world
2 | welcome
3 | zhangsan
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .idea/
2 | build/
3 | .gradle/
4 | out/
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | rootProject.name = 'netty_lecture'
2 |
3 |
--------------------------------------------------------------------------------
/input.txt:
--------------------------------------------------------------------------------
1 | hello world
2 | welcome
3 | zhangsan
4 | lisi
5 | 王五
6 | 赵六
--------------------------------------------------------------------------------
/output.txt:
--------------------------------------------------------------------------------
1 | hello world
2 | welcome
3 | zhangsan
4 | lisi
5 | 王五
6 | 赵六
--------------------------------------------------------------------------------
/NioTest13_In.txt:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shengsiyuan/netty_lecture/HEAD/NioTest13_In.txt
--------------------------------------------------------------------------------
/gen-py/py/thrift/generated/__init__.py:
--------------------------------------------------------------------------------
1 | __all__ = ['ttypes', 'constants', 'PersonService']
2 |
--------------------------------------------------------------------------------
/NioTest13_Out.txt:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shengsiyuan/netty_lecture/HEAD/NioTest13_Out.txt
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 精通并发与Netty 课程源码
2 |
3 | 课程源码位于src目录下,请将代码clone至本地,使用Intellij IDEA打开即可(代码构建使用了Gradle,请确保提前在本机安装好了Gradle)
4 |
5 | #### 版权所有@北京圣思园
--------------------------------------------------------------------------------
/src/main/java/com/shengsiyuan/decorator/Component.java:
--------------------------------------------------------------------------------
1 | package com.shengsiyuan.decorator;
2 |
3 | public interface Component {
4 |
5 | void doSomething();
6 | }
7 |
--------------------------------------------------------------------------------
/src/main/java/com/shengsiyuan/decorator/ConcreteComponent.java:
--------------------------------------------------------------------------------
1 | package com.shengsiyuan.decorator;
2 |
3 | public class ConcreteComponent implements Component {
4 |
5 | public void doSomething() {
6 | System.out.println("功能A");
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/src/main/resources/log4j.properties:
--------------------------------------------------------------------------------
1 | #log4j.rootLogger=DEBUG, stdout
2 | #
3 | #log4j.appender.stdout=org.apache.log4j.ConsoleAppender
4 | #log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
5 | #log4j.appender.stdout.layout.ConversionPattern=[%p] %C{1} - %m%n
--------------------------------------------------------------------------------
/src/main/java/com/shengsiyuan/decorator/Client.java:
--------------------------------------------------------------------------------
1 | package com.shengsiyuan.decorator;
2 |
3 | public class Client {
4 |
5 | public static void main(String[] args) {
6 |
7 | Component component = new ConcreteDecorator2(new ConcreteDecorator1(
8 | new ConcreteComponent()));
9 |
10 | component.doSomething();
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/src/main/java/com/shengsiyuan/decorator/Decorator.java:
--------------------------------------------------------------------------------
1 | package com.shengsiyuan.decorator;
2 |
3 | public class Decorator implements Component {
4 |
5 | private Component component;
6 |
7 | public Decorator(Component component) {
8 | this.component = component;
9 | }
10 |
11 | public void doSomething() {
12 | component.doSomething();
13 | }
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/gen-py/py/thrift/generated/constants.py:
--------------------------------------------------------------------------------
1 | #
2 | # Autogenerated by Thrift Compiler (0.10.0)
3 | #
4 | # DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
5 | #
6 | # options string: py
7 | #
8 |
9 | from thrift.Thrift import TType, TMessageType, TFrozenDict, TException, TApplicationException
10 | from thrift.protocol.TProtocol import TProtocolException
11 | import sys
12 | from .ttypes import *
13 |
--------------------------------------------------------------------------------
/src/main/java/com/shengsiyuan/proto/StudentRequestOrBuilder.java:
--------------------------------------------------------------------------------
1 | // Generated by the protocol buffer compiler. DO NOT EDIT!
2 | // source: Student.proto
3 |
4 | package com.shengsiyuan.proto;
5 |
6 | public interface StudentRequestOrBuilder extends
7 | // @@protoc_insertion_point(interface_extends:com.shengsiyuan.proto.StudentRequest)
8 | com.google.protobuf.MessageOrBuilder {
9 |
10 | /**
11 | * int32 age = 1;
12 | */
13 | int getAge();
14 | }
15 |
--------------------------------------------------------------------------------
/src/main/java/com/shengsiyuan/netty/test/Test.java:
--------------------------------------------------------------------------------
1 | package com.shengsiyuan.netty.test;
2 |
3 | import io.netty.util.NettyRuntime;
4 | import io.netty.util.internal.SystemPropertyUtil;
5 |
6 | public class Test {
7 |
8 | public static void main(String[] args) {
9 | int result = Math.max(1, SystemPropertyUtil.getInt(
10 | "io.netty.eventLoopThreads", NettyRuntime.availableProcessors() * 2));
11 |
12 | System.out.println(result);
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/main/java/com/shengsiyuan/netty/thirdexample/MyChatClientHandler.java:
--------------------------------------------------------------------------------
1 | package com.shengsiyuan.netty.thirdexample;
2 |
3 |
4 | import io.netty.channel.ChannelHandlerContext;
5 | import io.netty.channel.SimpleChannelInboundHandler;
6 |
7 | public class MyChatClientHandler extends SimpleChannelInboundHandler{
8 |
9 | @Override
10 | protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
11 | System.out.println(msg);
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/main/java/com/shengsiyuan/decorator/ConcreteDecorator1.java:
--------------------------------------------------------------------------------
1 | package com.shengsiyuan.decorator;
2 |
3 | public class ConcreteDecorator1 extends Decorator {
4 |
5 | public ConcreteDecorator1(Component component) {
6 | super(component);
7 | }
8 |
9 | @Override
10 | public void doSomething() {
11 | super.doSomething();
12 | this.doAnotherThing();
13 | }
14 |
15 | private void doAnotherThing() {
16 | System.out.println("功能B");
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/com/shengsiyuan/decorator/ConcreteDecorator2.java:
--------------------------------------------------------------------------------
1 | package com.shengsiyuan.decorator;
2 |
3 | public class ConcreteDecorator2 extends Decorator {
4 |
5 | public ConcreteDecorator2(Component component) {
6 | super(component);
7 | }
8 |
9 | @Override
10 | public void doSomething() {
11 | super.doSomething();
12 | this.doAnotherThing();
13 | }
14 |
15 | private void doAnotherThing() {
16 | System.out.println("功能C");
17 | }
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/shengsiyuan/netty/handler2/MyClientInitializer.java:
--------------------------------------------------------------------------------
1 | package com.shengsiyuan.netty.handler2;
2 |
3 |
4 | import io.netty.channel.ChannelInitializer;
5 | import io.netty.channel.ChannelPipeline;
6 | import io.netty.channel.socket.SocketChannel;
7 |
8 |
9 | public class MyClientInitializer extends ChannelInitializer {
10 |
11 | @Override
12 | protected void initChannel(SocketChannel ch) throws Exception {
13 | ChannelPipeline pipeline = ch.pipeline();
14 |
15 | pipeline.addLast(new MyClientHandler());
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/main/java/com/shengsiyuan/netty/handler3/PersonProtocol.java:
--------------------------------------------------------------------------------
1 | package com.shengsiyuan.netty.handler3;
2 |
3 | public class PersonProtocol {
4 |
5 | private int length;
6 |
7 | private byte[] content;
8 |
9 | public int getLength() {
10 | return length;
11 | }
12 |
13 | public void setLength(int length) {
14 | this.length = length;
15 | }
16 |
17 | public byte[] getContent() {
18 | return content;
19 | }
20 |
21 | public void setContent(byte[] content) {
22 | this.content = content;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/com/shengsiyuan/netty/handler/MyLongToStringDecoder.java:
--------------------------------------------------------------------------------
1 | package com.shengsiyuan.netty.handler;
2 |
3 | import io.netty.channel.ChannelHandlerContext;
4 | import io.netty.handler.codec.MessageToMessageDecoder;
5 |
6 | import java.util.List;
7 |
8 | public class MyLongToStringDecoder extends MessageToMessageDecoder {
9 |
10 | @Override
11 | protected void decode(ChannelHandlerContext ctx, Long msg, List