Extracted common Netty bootstrapping method

This commit is contained in:
selva.andre
2014-08-26 21:55:42 +02:00
parent 2be0fd84fe
commit 7314dbee73
4 changed files with 89 additions and 103 deletions
+2 -2
View File
@@ -161,8 +161,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
@@ -38,7 +38,6 @@ public class Server {
SimpleMessaging messaging;
public static void main(String[] args) throws IOException {
final Server server = new Server();
server.startServer();
System.out.println("Server started, version 0.7-SNAPSHOT");
@@ -49,7 +48,6 @@ public class Server {
server.stopServer();
}
});
}
/**
@@ -69,7 +67,7 @@ public class Server {
try {
confParser.parse(configFile);
} catch (ParseException pex) {
LOG.warn("An error occured in parsing configuration, fallback on default configuration", pex);
LOG.warn("An error occurred in parsing configuration, fallback on default configuration", pex);
}
Properties configProps = confParser.getProperties();
startServer(configProps);
@@ -80,15 +80,17 @@ public class NettyAcceptor implements ServerAcceptor {
}
}
abstract class PipelineInitializer {
abstract void init(ChannelPipeline pipeline) throws Exception;
}
private static final Logger LOG = LoggerFactory.getLogger(NettyAcceptor.class);
EventLoopGroup m_bossGroup;
EventLoopGroup m_workerGroup;
//BytesMetricsCollector m_metricsCollector = new BytesMetricsCollector();
MessageMetricsCollector m_metricsCollector = new MessageMetricsCollector();
MQTTEncoder mqttEncoder = new MQTTEncoder();
MQTTDecoder mqttDecoder = new MQTTDecoder();
@Override
public void initialize(IMessaging messaging, Properties props) throws IOException {
@@ -100,13 +102,7 @@ public class NettyAcceptor implements ServerAcceptor {
initializeSSLTCPTransport(messaging, props);
}
private void initializePlainTCPTransport(IMessaging messaging, Properties props) throws IOException {
// m_bossGroup = new NioEventLoopGroup();
// m_workerGroup = new NioEventLoopGroup();
final NettyMQTTHandler handler = new NettyMQTTHandler();
handler.setMessaging(messaging);
private void initFactory(String host, int port, final PipelineInitializer pipeliner) {
ServerBootstrap b = new ServerBootstrap();
b.group(m_bossGroup, m_workerGroup)
.channel(NioServerSocketChannel.class)
@@ -114,7 +110,30 @@ public class NettyAcceptor implements ServerAcceptor {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
//pipeline.addFirst("metrics", new BytesMetricsHandler(m_metricsCollector));
pipeliner.init(pipeline);
}
})
.option(ChannelOption.SO_BACKLOG, 128)
.option(ChannelOption.SO_REUSEADDR, true)
.childOption(ChannelOption.SO_KEEPALIVE, true);
try {
// Bind and start to accept incoming connections.
ChannelFuture f = b.bind(host, port);
LOG.info("Server binded");
f.sync();
} catch (InterruptedException ex) {
LOG.error(null, ex);
}
}
private void initializePlainTCPTransport(IMessaging messaging, Properties props) throws IOException {
final NettyMQTTHandler handler = new NettyMQTTHandler();
handler.setMessaging(messaging);
String host = props.getProperty("host");
int port = Integer.parseInt(props.getProperty("port"));
initFactory(host, port, new PipelineInitializer() {
@Override
void init(ChannelPipeline pipeline) {
pipeline.addFirst("idleStateHandler", new IdleStateHandler(0, 0, Constants.DEFAULT_CONNECT_TIMEOUT));
pipeline.addAfter("idleStateHandler", "idleEventHandler", new MoquetteIdleTimoutHandler());
//pipeline.addLast("logger", new LoggingHandler("Netty", LogLevel.ERROR));
@@ -123,19 +142,7 @@ public class NettyAcceptor implements ServerAcceptor {
pipeline.addLast("metrics", new MessageMetricsHandler(m_metricsCollector));
pipeline.addLast("handler", handler);
}
})
.option(ChannelOption.SO_BACKLOG, 128)
.option(ChannelOption.SO_REUSEADDR, true)
.childOption(ChannelOption.SO_KEEPALIVE, true);
try {
// Bind and start to accept incoming connections.
// ChannelFuture f = b.bind(Constants.PORT);
ChannelFuture f = b.bind(props.getProperty("host"), Integer.parseInt(props.getProperty("port")));
LOG.info("Server binded");
f.sync();
} catch (InterruptedException ex) {
LOG.error(null, ex);
}
});
}
private void initializeWebSocketTransport(IMessaging messaging, Properties props) throws IOException {
@@ -150,15 +157,10 @@ public class NettyAcceptor implements ServerAcceptor {
final NettyMQTTHandler handler = new NettyMQTTHandler();
handler.setMessaging(messaging);
ServerBootstrap b = new ServerBootstrap();
b.group(m_bossGroup, m_workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
String host = props.getProperty("host");
initFactory(host, port, new PipelineInitializer() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
void init(ChannelPipeline pipeline) {
pipeline.addLast("httpEncoder", new HttpResponseEncoder());
pipeline.addLast("httpDdecoder", new HttpRequestDecoder());
pipeline.addLast("aggregator", new HttpObjectAggregator(65536));
@@ -170,18 +172,7 @@ public class NettyAcceptor implements ServerAcceptor {
pipeline.addLast("metrics", new MessageMetricsHandler(m_metricsCollector));
pipeline.addLast("handler", handler);
}
})
.option(ChannelOption.SO_BACKLOG, 128)
.option(ChannelOption.SO_REUSEADDR, true)
.childOption(ChannelOption.SO_KEEPALIVE, true);
try {
ChannelFuture f = b.bind(props.getProperty("host"), port);
System.out.println("Web socket server started at port " + port + '.');
System.out.println("Open your browser and navigate to http://localhost:" + port + '/');
f.sync();
} catch (InterruptedException ex) {
LOG.error(null, ex);
}
});
}
@@ -215,16 +206,13 @@ public class NettyAcceptor implements ServerAcceptor {
}
int sslPort = Integer.parseInt(sslPortProp);
String host = props.getProperty("host");
final NettyMQTTHandler handler = new NettyMQTTHandler();
handler.setMessaging(messaging);
ServerBootstrap b = new ServerBootstrap();
b.group(m_bossGroup, m_workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
initFactory(host, sslPort, new PipelineInitializer() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
void init(ChannelPipeline pipeline) throws Exception {
InputStream jksInputStream = getClass().getClassLoader().getResourceAsStream(jksPath);
SSLContext serverContext = SSLContext.getInstance("TLS");
final KeyStore ks = KeyStore.getInstance("JKS");
@@ -237,7 +225,6 @@ public class NettyAcceptor implements ServerAcceptor {
engine.setUseClientMode(false);
final SslHandler sslHandler = new SslHandler(engine);
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("ssl", sslHandler);
//pipeline.addFirst("metrics", new BytesMetricsHandler(m_metricsCollector));
pipeline.addFirst("idleStateHandler", new IdleStateHandler(0, 0, Constants.DEFAULT_CONNECT_TIMEOUT));
@@ -248,19 +235,7 @@ public class NettyAcceptor implements ServerAcceptor {
pipeline.addLast("metrics", new MessageMetricsHandler(m_metricsCollector));
pipeline.addLast("handler", handler);
}
})
.option(ChannelOption.SO_BACKLOG, 128)
.option(ChannelOption.SO_REUSEADDR, true)
.childOption(ChannelOption.SO_KEEPALIVE, true);
try {
// Bind and start to accept incoming connections.
// ChannelFuture f = b.bind(Constants.PORT);
ChannelFuture f = b.bind(props.getProperty("host"), sslPort);
LOG.info("Server binded");
f.sync();
} catch (InterruptedException ex) {
LOG.error(null, ex);
}
});
}
public void close() {
+13
View File
@@ -33,6 +33,19 @@
</plugins>
</reporting>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<!--build>
<pluginManagement>
<plugins>