Created
December 21, 2009 21:44
-
-
Save cgbystrom/261277 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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