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> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<configuration> <configuration>
<source>1.5</source> <source>1.6</source>
<target>1.5</target> <target>1.6</target>
</configuration> </configuration>
</plugin> </plugin>
@@ -38,7 +38,6 @@ public class Server {
SimpleMessaging messaging; SimpleMessaging messaging;
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
final Server server = new Server(); final Server server = new Server();
server.startServer(); server.startServer();
System.out.println("Server started, version 0.7-SNAPSHOT"); System.out.println("Server started, version 0.7-SNAPSHOT");
@@ -49,7 +48,6 @@ public class Server {
server.stopServer(); server.stopServer();
} }
}); });
} }
/** /**
@@ -69,7 +67,7 @@ public class Server {
try { try {
confParser.parse(configFile); confParser.parse(configFile);
} catch (ParseException pex) { } 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(); Properties configProps = confParser.getProperties();
startServer(configProps); 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); private static final Logger LOG = LoggerFactory.getLogger(NettyAcceptor.class);
EventLoopGroup m_bossGroup; EventLoopGroup m_bossGroup;
EventLoopGroup m_workerGroup; EventLoopGroup m_workerGroup;
//BytesMetricsCollector m_metricsCollector = new BytesMetricsCollector(); //BytesMetricsCollector m_metricsCollector = new BytesMetricsCollector();
MessageMetricsCollector m_metricsCollector = new MessageMetricsCollector(); MessageMetricsCollector m_metricsCollector = new MessageMetricsCollector();
MQTTEncoder mqttEncoder = new MQTTEncoder();
MQTTDecoder mqttDecoder = new MQTTDecoder();
@Override @Override
public void initialize(IMessaging messaging, Properties props) throws IOException { public void initialize(IMessaging messaging, Properties props) throws IOException {
@@ -100,13 +102,7 @@ public class NettyAcceptor implements ServerAcceptor {
initializeSSLTCPTransport(messaging, props); initializeSSLTCPTransport(messaging, props);
} }
private void initializePlainTCPTransport(IMessaging messaging, Properties props) throws IOException { private void initFactory(String host, int port, final PipelineInitializer pipeliner) {
// m_bossGroup = new NioEventLoopGroup();
// m_workerGroup = new NioEventLoopGroup();
final NettyMQTTHandler handler = new NettyMQTTHandler();
handler.setMessaging(messaging);
ServerBootstrap b = new ServerBootstrap(); ServerBootstrap b = new ServerBootstrap();
b.group(m_bossGroup, m_workerGroup) b.group(m_bossGroup, m_workerGroup)
.channel(NioServerSocketChannel.class) .channel(NioServerSocketChannel.class)
@@ -114,7 +110,30 @@ public class NettyAcceptor implements ServerAcceptor {
@Override @Override
public void initChannel(SocketChannel ch) throws Exception { public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline(); 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.addFirst("idleStateHandler", new IdleStateHandler(0, 0, Constants.DEFAULT_CONNECT_TIMEOUT));
pipeline.addAfter("idleStateHandler", "idleEventHandler", new MoquetteIdleTimoutHandler()); pipeline.addAfter("idleStateHandler", "idleEventHandler", new MoquetteIdleTimoutHandler());
//pipeline.addLast("logger", new LoggingHandler("Netty", LogLevel.ERROR)); //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("metrics", new MessageMetricsHandler(m_metricsCollector));
pipeline.addLast("handler", handler); 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 { private void initializeWebSocketTransport(IMessaging messaging, Properties props) throws IOException {
@@ -150,15 +157,10 @@ public class NettyAcceptor implements ServerAcceptor {
final NettyMQTTHandler handler = new NettyMQTTHandler(); final NettyMQTTHandler handler = new NettyMQTTHandler();
handler.setMessaging(messaging); handler.setMessaging(messaging);
ServerBootstrap b = new ServerBootstrap(); String host = props.getProperty("host");
b.group(m_bossGroup, m_workerGroup) initFactory(host, port, new PipelineInitializer() {
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override @Override
public void initChannel(SocketChannel ch) throws Exception { void init(ChannelPipeline pipeline) {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("httpEncoder", new HttpResponseEncoder()); pipeline.addLast("httpEncoder", new HttpResponseEncoder());
pipeline.addLast("httpDdecoder", new HttpRequestDecoder()); pipeline.addLast("httpDdecoder", new HttpRequestDecoder());
pipeline.addLast("aggregator", new HttpObjectAggregator(65536)); pipeline.addLast("aggregator", new HttpObjectAggregator(65536));
@@ -170,18 +172,7 @@ public class NettyAcceptor implements ServerAcceptor {
pipeline.addLast("metrics", new MessageMetricsHandler(m_metricsCollector)); pipeline.addLast("metrics", new MessageMetricsHandler(m_metricsCollector));
pipeline.addLast("handler", handler); 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); int sslPort = Integer.parseInt(sslPortProp);
String host = props.getProperty("host");
final NettyMQTTHandler handler = new NettyMQTTHandler(); final NettyMQTTHandler handler = new NettyMQTTHandler();
handler.setMessaging(messaging); handler.setMessaging(messaging);
initFactory(host, sslPort, new PipelineInitializer() {
ServerBootstrap b = new ServerBootstrap();
b.group(m_bossGroup, m_workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override @Override
public void initChannel(SocketChannel ch) throws Exception { void init(ChannelPipeline pipeline) throws Exception {
InputStream jksInputStream = getClass().getClassLoader().getResourceAsStream(jksPath); InputStream jksInputStream = getClass().getClassLoader().getResourceAsStream(jksPath);
SSLContext serverContext = SSLContext.getInstance("TLS"); SSLContext serverContext = SSLContext.getInstance("TLS");
final KeyStore ks = KeyStore.getInstance("JKS"); final KeyStore ks = KeyStore.getInstance("JKS");
@@ -237,7 +225,6 @@ public class NettyAcceptor implements ServerAcceptor {
engine.setUseClientMode(false); engine.setUseClientMode(false);
final SslHandler sslHandler = new SslHandler(engine); final SslHandler sslHandler = new SslHandler(engine);
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("ssl", sslHandler); pipeline.addLast("ssl", sslHandler);
//pipeline.addFirst("metrics", new BytesMetricsHandler(m_metricsCollector)); //pipeline.addFirst("metrics", new BytesMetricsHandler(m_metricsCollector));
pipeline.addFirst("idleStateHandler", new IdleStateHandler(0, 0, Constants.DEFAULT_CONNECT_TIMEOUT)); 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("metrics", new MessageMetricsHandler(m_metricsCollector));
pipeline.addLast("handler", handler); 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() { public void close() {
+13
View File
@@ -33,6 +33,19 @@
</plugins> </plugins>
</reporting> </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> <!--build>
<pluginManagement> <pluginManagement>
<plugins> <plugins>