Skip to content

Commit 4732bac

Browse files
retroqpivovarit
authored andcommitted
BAEL-768 Introduction to Netty (eugenp#1933)
* BAEL-748 quick guide to @value * BAEL-748 changes from review * BAEL-748 inject comma-separated values into array * BAEL-768 Introduction to Netty * BAEL-768 remove commented code
1 parent 4c88eeb commit 4732bac

12 files changed

Lines changed: 291 additions & 0 deletions

‎libraries/pom.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,11 @@
308308
<artifactId>jool</artifactId>
309309
<version>0.9.12</version>
310310
</dependency>
311+
<dependency>
312+
<groupId>io.netty</groupId>
313+
<artifactId>netty-all</artifactId>
314+
<version>${netty.version}</version>
315+
</dependency>
311316
</dependencies>
312317
<properties>
313318
<multiverse.version>0.7.0</multiverse.version>
@@ -332,6 +337,7 @@
332337
<serenity.jira.version>1.1.3-rc.5</serenity.jira.version>
333338
<serenity.plugin.version>1.4.0</serenity.plugin.version>
334339
<jUnitParams.version>1.1.0</jUnitParams.version>
340+
<netty.version>4.1.10.Final</netty.version>
335341
</properties>
336342

337343
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.netty;
2+
3+
import io.netty.channel.ChannelHandlerContext;
4+
import io.netty.channel.ChannelInboundHandlerAdapter;
5+
6+
public class ClientHandler extends ChannelInboundHandlerAdapter {
7+
@Override
8+
public void channelActive(ChannelHandlerContext ctx) throws Exception {
9+
RequestData msg = new RequestData();
10+
msg.setIntValue(123);
11+
msg.setStringValue("all work and no play makes jack a dull boy");
12+
ctx.writeAndFlush(msg);
13+
}
14+
15+
@Override
16+
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
17+
System.out.println(msg);
18+
ctx.close();
19+
}
20+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.netty;
2+
3+
import io.netty.bootstrap.Bootstrap;
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.NioSocketChannel;
11+
12+
public class NettyClient {
13+
public static void main(String[] args) throws Exception {
14+
String host = "localhost";
15+
int port = 8080;
16+
EventLoopGroup workerGroup = new NioEventLoopGroup();
17+
18+
try {
19+
Bootstrap b = new Bootstrap();
20+
b.group(workerGroup);
21+
b.channel(NioSocketChannel.class);
22+
b.option(ChannelOption.SO_KEEPALIVE, true);
23+
b.handler(new ChannelInitializer<SocketChannel>() {
24+
@Override
25+
public void initChannel(SocketChannel ch) throws Exception {
26+
ch.pipeline().addLast(new RequestDataEncoder(), new ResponseDataDecoder(), new ClientHandler());
27+
}
28+
});
29+
30+
ChannelFuture f = b.connect(host, port).sync();
31+
32+
f.channel().closeFuture().sync();
33+
} finally {
34+
workerGroup.shutdownGracefully();
35+
}
36+
}
37+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baeldung.netty;
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+
12+
public class NettyServer {
13+
14+
private int port;
15+
16+
public NettyServer(int port) {
17+
this.port = port;
18+
}
19+
20+
public static void main(String[] args) throws Exception {
21+
int port;
22+
if (args.length > 0) {
23+
port = Integer.parseInt(args[0]);
24+
} else {
25+
port = 8080;
26+
}
27+
new NettyServer(port).run();
28+
}
29+
30+
public void run() throws Exception {
31+
EventLoopGroup bossGroup = new NioEventLoopGroup();
32+
EventLoopGroup workerGroup = new NioEventLoopGroup();
33+
try {
34+
ServerBootstrap b = new ServerBootstrap();
35+
b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {
36+
@Override
37+
public void initChannel(SocketChannel ch) throws Exception {
38+
ch.pipeline().addLast(new RequestDecoder(), new ResponseDataEncoder(), new ProcessingHandler());
39+
}
40+
}).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);
41+
42+
ChannelFuture f = b.bind(port).sync();
43+
f.channel().closeFuture().sync();
44+
} finally {
45+
workerGroup.shutdownGracefully();
46+
bossGroup.shutdownGracefully();
47+
}
48+
}
49+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.netty;
2+
3+
import io.netty.channel.ChannelFuture;
4+
import io.netty.channel.ChannelFutureListener;
5+
import io.netty.channel.ChannelHandlerContext;
6+
import io.netty.channel.ChannelInboundHandlerAdapter;
7+
8+
public class ProcessingHandler extends ChannelInboundHandlerAdapter {
9+
10+
@Override
11+
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
12+
RequestData requestData = (RequestData) msg;
13+
ResponseData responseData = new ResponseData();
14+
responseData.setIntValue(requestData.getIntValue() * 2);
15+
ChannelFuture future = ctx.writeAndFlush(responseData);
16+
future.addListener(ChannelFutureListener.CLOSE);
17+
System.out.println(requestData);
18+
}
19+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.netty;
2+
3+
public class RequestData {
4+
private int intValue;
5+
private String stringValue;
6+
7+
public int getIntValue() {
8+
return intValue;
9+
}
10+
11+
public void setIntValue(int intValue) {
12+
this.intValue = intValue;
13+
}
14+
15+
public String getStringValue() {
16+
return stringValue;
17+
}
18+
19+
public void setStringValue(String stringValue) {
20+
this.stringValue = stringValue;
21+
}
22+
23+
@Override
24+
public String toString() {
25+
return "RequestData{" +
26+
"intValue=" + intValue +
27+
", stringValue='" + stringValue + '\'' +
28+
'}';
29+
}
30+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.netty;
2+
3+
import io.netty.buffer.ByteBuf;
4+
import io.netty.channel.ChannelHandlerContext;
5+
import io.netty.handler.codec.MessageToByteEncoder;
6+
7+
import java.nio.charset.Charset;
8+
9+
public class RequestDataEncoder extends MessageToByteEncoder<RequestData> {
10+
11+
private final Charset charset = Charset.forName("UTF-8");
12+
13+
@Override
14+
protected void encode(ChannelHandlerContext ctx, RequestData msg, ByteBuf out) throws Exception {
15+
out.writeInt(msg.getIntValue());
16+
out.writeInt(msg.getStringValue().length());
17+
out.writeCharSequence(msg.getStringValue(), charset);
18+
}
19+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.netty;
2+
3+
import java.nio.charset.Charset;
4+
import java.util.List;
5+
6+
import io.netty.buffer.ByteBuf;
7+
import io.netty.channel.ChannelHandlerContext;
8+
import io.netty.handler.codec.ReplayingDecoder;
9+
10+
public class RequestDecoder extends ReplayingDecoder<RequestData> {
11+
12+
private final Charset charset = Charset.forName("UTF-8");
13+
14+
@Override
15+
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
16+
RequestData data = new RequestData();
17+
data.setIntValue(in.readInt());
18+
int strLen = in.readInt();
19+
data.setStringValue(in.readCharSequence(strLen, charset).toString());
20+
out.add(data);
21+
}
22+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.netty;
2+
3+
public class ResponseData {
4+
private int intValue;
5+
6+
public int getIntValue() {
7+
return intValue;
8+
}
9+
10+
public void setIntValue(int intValue) {
11+
this.intValue = intValue;
12+
}
13+
14+
@Override
15+
public String toString() {
16+
return "ResponseData{" +
17+
"intValue=" + intValue +
18+
'}';
19+
}
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.netty;
2+
3+
import java.util.List;
4+
5+
import io.netty.buffer.ByteBuf;
6+
import io.netty.channel.ChannelHandlerContext;
7+
import io.netty.handler.codec.ReplayingDecoder;
8+
9+
public class ResponseDataDecoder extends ReplayingDecoder<ResponseData> {
10+
11+
@Override
12+
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
13+
ResponseData data = new ResponseData();
14+
data.setIntValue(in.readInt());
15+
out.add(data);
16+
}
17+
}

0 commit comments

Comments
 (0)