Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit fa8ab02

Browse filesBrowse files
committed
add netty channelPipeline demo
1 parent b81101d commit fa8ab02
Copy full SHA for fa8ab02

File tree

Expand file treeCollapse file tree

10 files changed

+172
-2
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

10 files changed

+172
-2
lines changed
Open diff view settings
Collapse file

‎Spring-Netty/learnnetty.iml‎

Copy file name to clipboardExpand all lines: Spring-Netty/learnnetty.iml
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,6 @@
9292
<orderEntry type="library" name="Maven: org.springframework:spring-jcl:5.2.1.RELEASE" level="project" />
9393
<orderEntry type="library" scope="TEST" name="Maven: org.springframework:spring-test:5.2.1.RELEASE" level="project" />
9494
<orderEntry type="library" scope="TEST" name="Maven: org.xmlunit:xmlunit-core:2.6.3" level="project" />
95-
<orderEntry type="library" name="Maven: io.netty:netty-all:4.1.42.Final" level="project" />
95+
<orderEntry type="library" name="Maven: io.netty:netty-all:4.1.6.Final" level="project" />
9696
</component>
9797
</module>
Collapse file

‎Spring-Netty/pom.xml‎

Copy file name to clipboardExpand all lines: Spring-Netty/pom.xml
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
<properties>
1818
<java.version>1.8</java.version>
19-
<netty-all.version>4.1.42.Final</netty-all.version>
19+
<netty-all.version>4.1.6.Final</netty-all.version>
2020
</properties>
2121

2222
<dependencies>
Collapse file
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.bruis.learnnetty.netty.channelpipeline;
2+
3+
public class BusinessException extends Exception {
4+
5+
public BusinessException(String message) {
6+
super(message);
7+
}
8+
}
Collapse file
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.bruis.learnnetty.netty.channelpipeline;
2+
3+
import io.netty.channel.ChannelHandlerContext;
4+
import io.netty.channel.ChannelInboundHandlerAdapter;
5+
6+
/**
7+
* @author
8+
*/
9+
public class InBoundHandlerA extends ChannelInboundHandlerAdapter {
10+
11+
@Override
12+
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
13+
System.out.println("InBoundHandlerA: " + msg);
14+
ctx.fireChannelRead(msg);
15+
}
16+
}
Collapse file
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.bruis.learnnetty.netty.channelpipeline;
2+
3+
import io.netty.channel.ChannelHandlerContext;
4+
import io.netty.channel.ChannelInboundHandlerAdapter;
5+
6+
/**
7+
* @author
8+
*/
9+
public class InBoundHandlerB extends ChannelInboundHandlerAdapter {
10+
@Override
11+
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
12+
System.out.println("InBoundHandlerB: " + msg);
13+
ctx.fireChannelRead(msg);
14+
}
15+
16+
@Override
17+
public void channelActive(ChannelHandlerContext ctx) {
18+
ctx.channel().pipeline().fireChannelRead("hello world");
19+
}
20+
}
Collapse file
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.bruis.learnnetty.netty.channelpipeline;
2+
3+
import io.netty.channel.ChannelHandlerContext;
4+
import io.netty.channel.ChannelInboundHandlerAdapter;
5+
import io.netty.util.ReferenceCountUtil;
6+
7+
/**
8+
* @author
9+
*/
10+
public class InBoundHandlerC extends ChannelInboundHandlerAdapter {
11+
@Override
12+
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
13+
System.out.println("InBoundHandlerC: " + msg);
14+
ctx.fireChannelRead(msg);
15+
}
16+
}
Collapse file
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.bruis.learnnetty.netty.channelpipeline;
2+
3+
import io.netty.channel.ChannelHandlerContext;
4+
import io.netty.channel.ChannelOutboundHandlerAdapter;
5+
import io.netty.channel.ChannelPromise;
6+
7+
/**
8+
* @author
9+
*/
10+
public class OutBoundHandlerA extends ChannelOutboundHandlerAdapter {
11+
12+
@Override
13+
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
14+
System.out.println("OutBoundHandlerA: " + msg);
15+
ctx.write(msg, promise);
16+
}
17+
}
Collapse file
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.bruis.learnnetty.netty.channelpipeline;
2+
3+
import io.netty.channel.ChannelHandlerContext;
4+
import io.netty.channel.ChannelOutboundHandlerAdapter;
5+
import io.netty.channel.ChannelPromise;
6+
7+
import java.util.concurrent.TimeUnit;
8+
9+
/**
10+
* @author
11+
*/
12+
public class OutBoundHandlerB extends ChannelOutboundHandlerAdapter {
13+
@Override
14+
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
15+
System.out.println("OutBoundHandlerB: " + msg);
16+
ctx.write(msg, promise);
17+
}
18+
19+
20+
@Override
21+
public void handlerAdded(final ChannelHandlerContext ctx) {
22+
ctx.executor().schedule(() -> {
23+
ctx.channel().write("ctx.channel().write -> hello world");
24+
ctx.write("hello world");
25+
}, 3, TimeUnit.SECONDS);
26+
}
27+
}
Collapse file
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.bruis.learnnetty.netty.channelpipeline;
2+
3+
import io.netty.channel.ChannelHandlerContext;
4+
import io.netty.channel.ChannelOutboundHandlerAdapter;
5+
import io.netty.channel.ChannelPromise;
6+
7+
/**
8+
* @author
9+
*/
10+
public class OutBoundHandlerC extends ChannelOutboundHandlerAdapter {
11+
12+
@Override
13+
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
14+
System.out.println("OutBoundHandlerC: " + msg);
15+
ctx.write(msg, promise);
16+
// throw new BusinessException("from OutBoundHandlerC");
17+
}
18+
}
Collapse file
+48Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.bruis.learnnetty.netty.channelpipeline;
2+
3+
import io.netty.bootstrap.ServerBootstrap;
4+
import io.netty.channel.ChannelFuture;
5+
import io.netty.channel.ChannelInitializer;
6+
import io.netty.channel.ChannelOption;
7+
import io.netty.channel.EventLoopGroup;
8+
import io.netty.channel.nio.NioEventLoopGroup;
9+
import io.netty.channel.socket.SocketChannel;
10+
import io.netty.channel.socket.nio.NioServerSocketChannel;
11+
import io.netty.util.AttributeKey;
12+
13+
/**
14+
* @author
15+
*/
16+
public final class Server {
17+
18+
public static void main(String[] args) throws Exception {
19+
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
20+
EventLoopGroup workerGroup = new NioEventLoopGroup();
21+
22+
try {
23+
ServerBootstrap b = new ServerBootstrap();
24+
b.group(bossGroup, workerGroup)
25+
.channel(NioServerSocketChannel.class)
26+
.childOption(ChannelOption.TCP_NODELAY, true)
27+
.childAttr(AttributeKey.newInstance("childAttr"), "childAttrValue")
28+
.childHandler(new ChannelInitializer<SocketChannel>() {
29+
@Override
30+
public void initChannel(SocketChannel ch) {
31+
ch.pipeline().addLast(new OutBoundHandlerA());
32+
ch.pipeline().addLast(new OutBoundHandlerB());
33+
ch.pipeline().addLast(new OutBoundHandlerC());
34+
// ch.pipeline().addLast(new InBoundHandlerA());
35+
// ch.pipeline().addLast(new InBoundHandlerB());
36+
// ch.pipeline().addLast(new InBoundHandlerC());
37+
}
38+
});
39+
40+
ChannelFuture f = b.bind(8888).sync();
41+
42+
f.channel().closeFuture().sync();
43+
} finally {
44+
bossGroup.shutdownGracefully();
45+
workerGroup.shutdownGracefully();
46+
}
47+
}
48+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.