Skip to content

Instantly share code, notes, and snippets.

@aolshevskiy
Created January 21, 2012 15:35
Show Gist options
  • Save aolshevskiy/1653087 to your computer and use it in GitHub Desktop.
Save aolshevskiy/1653087 to your computer and use it in GitHub Desktop.
Hello World Netty Http Server
apply plugin: "java"
apply plugin: "eclipse"
repositories {
mavenCentral()
}
dependencies {
compile (
"org.jboss.netty:netty:latest.integration",
"com.google.inject:guice:latest.integration",
"com.google.guava:guava:latest.integration"
)
}
package netty;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.group.ChannelGroup;
import org.jboss.netty.channel.group.DefaultChannelGroup;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Provides;
import com.google.inject.Singleton;
public class NettyModule extends AbstractModule {
@Override
protected void configure() {
}
@Provides
SocketAddress provideSocketAddress() {
return new InetSocketAddress(8080);
}
@Provides @Singleton
ChannelGroup provideChannelGroup() {
return new DefaultChannelGroup("http-server");
}
@Provides
ChannelFactory provideChannelFactory() {
return new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(), Executors.newCachedThreadPool());
}
public static void main(String[] args) {
Injector injector = Guice.createInjector(new NettyModule());
final NettyServer server = injector.getInstance(NettyServer.class);
server.startAndWait();
Runtime.getRuntime().addShutdownHook(new Thread(){
@Override
public void run() {
server.stopAndWait();
}
});
}
}
package netty;
import java.net.SocketAddress;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelFutureListener;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.channel.group.ChannelGroup;
import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
import org.jboss.netty.handler.codec.http.HttpServerCodec;
import org.jboss.netty.handler.codec.http.HttpVersion;
import com.google.common.base.Charsets;
import com.google.common.util.concurrent.AbstractIdleService;
import com.google.inject.Inject;
import com.google.inject.Provider;
class NettyServer extends AbstractIdleService {
private final ChannelGroup allChannels;
private final SocketAddress address;
private final ChannelFactory factory;
private final ServerBootstrap bootstrap;
private final Provider<Handler> handler;
@Inject
NettyServer(ChannelFactory factory, ChannelGroup allChannels, SocketAddress address, Provider<Handler> handler) {
this.factory = factory;
this.bootstrap = new ServerBootstrap(factory);
this.allChannels = allChannels;
this.address = address;
this.handler = handler;
}
@Override
protected void startUp() throws Exception {
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() throws Exception {
return Channels.pipeline(new HttpServerCodec(), handler.get());
}
});
bootstrap.setOption("child.tcpNoDelay", true);
bootstrap.setOption("child.keepAlive", true);
Channel channel = bootstrap.bind(address);
allChannels.add(channel);
}
@Override
protected void shutDown() throws Exception {
allChannels.close().awaitUninterruptibly();
factory.releaseExternalResources();
}
}
class Handler extends SimpleChannelHandler {
private final ChannelGroup allChannels;
@Inject
Handler(ChannelGroup allChannels) {
this.allChannels = allChannels;
}
@Override
public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e)
throws Exception {
allChannels.add(e.getChannel());
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
throws Exception {
e.getCause().printStackTrace();
e.getChannel().close();
}
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
throws Exception {
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
response.setContent(ChannelBuffers.copiedBuffer("Hello, world!", Charsets.UTF_8));
e.getChannel().write(response).addListener(new ChannelFutureListener() {
public void operationComplete(ChannelFuture future) throws Exception {
future.getChannel().close();
}});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment