Skip to content

Instantly share code, notes, and snippets.

@cgbystrom
Created December 21, 2009 21:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cgbystrom/261277 to your computer and use it in GitHub Desktop.
Save cgbystrom/261277 to your computer and use it in GitHub Desktop.
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
import java.util.HashMap;
import java.util.Map;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.channel.*;
import static org.jboss.netty.channel.Channels.pipeline;
import org.jboss.netty.handler.codec.http.*;
import com.sun.jersey.api.container.ContainerFactory;
import com.sun.jersey.api.core.ResourceConfig;
import com.sun.jersey.api.core.ClassNamesResourceConfig;
import com.sun.jersey.server.impl.container.netty.NettyHandlerContainer;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
@Path("/hellonetty")
public class HelloNettyResource
{
@GET
@Produces("text/plain")
public String getClichedMessage()
{
return "Hello Netty";
}
public static void main(String[] args)
{
ServerBootstrap bootstrap = new ServerBootstrap(
new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()));
bootstrap.setPipelineFactory(new HttpServerPipelineFactory());
bootstrap.bind(new InetSocketAddress(8080));
}
}
class HttpServerPipelineFactory implements ChannelPipelineFactory
{
private NettyHandlerContainer jerseyHandler;
public HttpServerPipelineFactory()
{
this.jerseyHandler = getJerseyHandler();
}
public ChannelPipeline getPipeline() throws Exception
{
ChannelPipeline pipeline = pipeline();
pipeline.addLast("decoder", new HttpRequestDecoder());
pipeline.addLast("encoder", new HttpResponseEncoder());
pipeline.addLast("jerseyHandler", jerseyHandler);
return pipeline;
}
private NettyHandlerContainer getJerseyHandler()
{
Map<String, Object> props = new HashMap<String, Object>();
props.put(ClassNamesResourceConfig.PROPERTY_CLASSNAMES, "HelloNettyResource");
props.put(NettyHandlerContainer.PROPERTY_BASE_URI, "http://localhost:8080/");
ResourceConfig rcf = new ClassNamesResourceConfig(props);
return ContainerFactory.createContainer(NettyHandlerContainer.class, rcf);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment