Skip to content

Commit 92efa3c

Browse files
kennylbjnormanmaurer
authored andcommitted
Add UptimeServer and adjust UptimeClient's code style.
Motivation: Uptime example is lack of server. UptimeClient's code style is a little bit different from others, which make reader feel confused. We don't need to create a new Bootstrap instance each time client reconnect to server. Modification: Add UptimeServer and UptimeServerHandler which simply accept all connection and discard all message. Change UptimeClient's code style. Share a single Bootstrap instance. Result: Uptime server support. Consistent code style. Single Bootstrap for all reconnection.
1 parent ea1cb20 commit 92efa3c

File tree

5 files changed

+120
-27
lines changed

5 files changed

+120
-27
lines changed

Diff for: example/src/main/java/io/netty/example/uptime/UptimeClient.java

+15-22
Original file line numberDiff line numberDiff line change
@@ -38,34 +38,27 @@ public final class UptimeClient {
3838
// Sleep 5 seconds before a reconnection attempt.
3939
static final int RECONNECT_DELAY = Integer.parseInt(System.getProperty("reconnectDelay", "5"));
4040
// Reconnect when the server sends nothing for 10 seconds.
41-
static final int READ_TIMEOUT = Integer.parseInt(System.getProperty("readTimeout", "10"));
41+
private static final int READ_TIMEOUT = Integer.parseInt(System.getProperty("readTimeout", "10"));
4242

4343
private static final UptimeClientHandler handler = new UptimeClientHandler();
44+
private static final Bootstrap bs = new Bootstrap();
4445

4546
public static void main(String[] args) throws Exception {
46-
configureBootstrap(new Bootstrap()).connect();
47+
EventLoopGroup group = new NioEventLoopGroup();
48+
bs.group(group)
49+
.channel(NioSocketChannel.class)
50+
.remoteAddress(HOST, PORT)
51+
.handler(new ChannelInitializer<SocketChannel>() {
52+
@Override
53+
protected void initChannel(SocketChannel ch) throws Exception {
54+
ch.pipeline().addLast(new IdleStateHandler(READ_TIMEOUT, 0, 0), handler);
55+
}
56+
});
57+
bs.connect();
4758
}
4859

49-
private static Bootstrap configureBootstrap(Bootstrap b) {
50-
return configureBootstrap(b, new NioEventLoopGroup());
51-
}
52-
53-
static Bootstrap configureBootstrap(Bootstrap b, EventLoopGroup g) {
54-
b.group(g)
55-
.channel(NioSocketChannel.class)
56-
.remoteAddress(HOST, PORT)
57-
.handler(new ChannelInitializer<SocketChannel>() {
58-
@Override
59-
public void initChannel(SocketChannel ch) throws Exception {
60-
ch.pipeline().addLast(new IdleStateHandler(READ_TIMEOUT, 0, 0), handler);
61-
}
62-
});
63-
64-
return b;
65-
}
66-
67-
static void connect(Bootstrap b) {
68-
b.connect().addListener(new ChannelFutureListener() {
60+
static void connect() {
61+
bs.connect().addListener(new ChannelFutureListener() {
6962
@Override
7063
public void operationComplete(ChannelFuture future) throws Exception {
7164
if (future.cause() != null) {

Diff for: example/src/main/java/io/netty/example/uptime/UptimeClientHandler.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@
1515
*/
1616
package io.netty.example.uptime;
1717

18-
import io.netty.bootstrap.Bootstrap;
1918
import io.netty.channel.ChannelHandler.Sharable;
2019
import io.netty.channel.ChannelHandlerContext;
21-
import io.netty.channel.EventLoop;
2220
import io.netty.channel.SimpleChannelInboundHandler;
2321
import io.netty.handler.timeout.IdleState;
2422
import io.netty.handler.timeout.IdleStateEvent;
@@ -70,12 +68,11 @@ public void channelInactive(final ChannelHandlerContext ctx) {
7068
public void channelUnregistered(final ChannelHandlerContext ctx) throws Exception {
7169
println("Sleeping for: " + UptimeClient.RECONNECT_DELAY + 's');
7270

73-
final EventLoop loop = ctx.channel().eventLoop();
74-
loop.schedule(new Runnable() {
71+
ctx.channel().eventLoop().schedule(new Runnable() {
7572
@Override
7673
public void run() {
7774
println("Reconnecting to: " + UptimeClient.HOST + ':' + UptimeClient.PORT);
78-
UptimeClient.connect(UptimeClient.configureBootstrap(new Bootstrap(), loop));
75+
UptimeClient.connect();
7976
}
8077
}, UptimeClient.RECONNECT_DELAY, TimeUnit.SECONDS);
8178
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2017 The Netty Project
3+
*
4+
* The Netty Project licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package io.netty.example.uptime;
17+
18+
import io.netty.bootstrap.ServerBootstrap;
19+
import io.netty.channel.ChannelFuture;
20+
import io.netty.channel.ChannelInitializer;
21+
import io.netty.channel.EventLoopGroup;
22+
import io.netty.channel.nio.NioEventLoopGroup;
23+
import io.netty.channel.socket.SocketChannel;
24+
import io.netty.channel.socket.nio.NioServerSocketChannel;
25+
import io.netty.handler.logging.LogLevel;
26+
import io.netty.handler.logging.LoggingHandler;
27+
28+
/**
29+
* Uptime server is served as a connection server.
30+
* So it simply discards all message received.
31+
*/
32+
public final class UptimeServer {
33+
private static final int PORT = Integer.parseInt(System.getProperty("port", "8080"));
34+
private static final UptimeServerHandler handler = new UptimeServerHandler();
35+
36+
private UptimeServer() {
37+
}
38+
39+
public static void main(String[] args) throws Exception {
40+
41+
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
42+
EventLoopGroup workerGroup = new NioEventLoopGroup();
43+
try {
44+
ServerBootstrap b = new ServerBootstrap();
45+
b.group(bossGroup, workerGroup)
46+
.channel(NioServerSocketChannel.class)
47+
.handler(new LoggingHandler(LogLevel.INFO))
48+
.childHandler(new ChannelInitializer<SocketChannel>() {
49+
@Override
50+
public void initChannel(SocketChannel ch) {
51+
ch.pipeline().addLast(handler);
52+
}
53+
});
54+
55+
// Bind and start to accept incoming connections.
56+
ChannelFuture f = b.bind(PORT).sync();
57+
58+
// Wait until the server socket is closed.
59+
// In this example, this does not happen, but you can do that to gracefully
60+
// shut down your server.
61+
f.channel().closeFuture().sync();
62+
} finally {
63+
workerGroup.shutdownGracefully();
64+
bossGroup.shutdownGracefully();
65+
}
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2017 The Netty Project
3+
*
4+
* The Netty Project licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package io.netty.example.uptime;
17+
18+
import io.netty.channel.ChannelHandler.Sharable;
19+
import io.netty.channel.ChannelHandlerContext;
20+
import io.netty.channel.SimpleChannelInboundHandler;
21+
22+
@Sharable
23+
public class UptimeServerHandler extends SimpleChannelInboundHandler<Object> {
24+
@Override
25+
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
26+
// discard
27+
}
28+
29+
@Override
30+
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
31+
// Close the connection when an exception is raised.
32+
cause.printStackTrace();
33+
ctx.close();
34+
}
35+
}

Diff for: run-example.sh

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ EXAMPLE_MAP=(
3838
'memcache-binary-client:io.netty.example.memcache.binary.MemcacheClient'
3939
'stomp-client:io.netty.example.stomp.StompClient'
4040
'uptime-client:io.netty.example.uptime.UptimeClient'
41+
'uptime-server:io.netty.example.uptime.UptimeServer'
4142
'sctpecho-client:io.netty.example.sctp.SctpEchoClient'
4243
'sctpecho-server:io.netty.example.sctp.SctpEchoServer'
4344
'localecho:io.netty.example.localecho.LocalEcho'

0 commit comments

Comments
 (0)