Java tutorial
/* * Copyright (c) 2014 Oliver Seemann * * This file is part of Jalos. * * Jalos is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Jalos is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Jalos. If not, see <http://www.gnu.org/licenses/>. */ package net.oebs.jalos.netty; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.Channel; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; import net.oebs.jalos.Settings; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class HttpServer { private final Integer port; private final String host; static final Logger log = LoggerFactory.getLogger(HttpServer.class); public HttpServer(Settings settings) { this.port = settings.getHttpPort(); this.host = settings.getHttpHost(); } public void run() throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.option(ChannelOption.SO_BACKLOG, 8192); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new HttpInitializer()); Channel ch = b.bind(host, port).sync().channel(); log.info("Listening at http://{}:{}/", host, port); ch.closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } }