forked from hazelcast/hazelcast-jet-code-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNettyServer.java
95 lines (81 loc) · 3.67 KB
/
NettyServer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* Copyright (c) 2008-2017, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import java.util.function.Consumer;
/**
* A server instance which accepts a port number to connect,
* a consumer to be used for each channel activated and
* a consumer to be used for each message read.
*/
class NettyServer {
private final int port;
private final Consumer<Channel> channelActiveHandler;
private final Consumer<Object> messageConsumer;
private EventLoopGroup parentGroup;
private EventLoopGroup childGroup;
private Channel channel;
NettyServer(int port, Consumer<Channel> channelActiveHandler, Consumer<Object> messageConsumer) {
this.port = port;
this.channelActiveHandler = channelActiveHandler;
this.messageConsumer = messageConsumer;
}
void start() throws Exception {
parentGroup = new NioEventLoopGroup(1);
childGroup = new NioEventLoopGroup();
ServerBootstrap bootstrap = new ServerBootstrap()
.group(parentGroup, childGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override protected void initChannel(SocketChannel channel) throws Exception {
ChannelPipeline pipeline = channel.pipeline();
pipeline.addLast("lineFrame", new LineBasedFrameDecoder(256));
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
pipeline.addLast("handler", new ChannelInboundHandlerAdapter() {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
Channel channel = ctx.channel();
channelActiveHandler.accept(channel);
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
messageConsumer.accept(msg);
}
});
}
});
channel = bootstrap.bind(port).channel();
System.out.println("NettyServer started");
}
void stop() throws Exception {
channel.close().get();
parentGroup.shutdownGracefully().get();
childGroup.shutdownGracefully().get();
System.out.println("NettyServer closed");
}
}