building netty servers

Download Building Netty Servers

If you can't read please download the document

Upload: dani-sola-lagares

Post on 25-May-2015

7.745 views

Category:

Technology


3 download

DESCRIPTION

This presentation on building servers explains what is Netty, why choosing it and shows how with very little code you can build an asynchronous app server.

TRANSCRIPT

  • 1. Building Netty Servers Dani Sol

2. What is Netty?Asynchronous event-driven networkapplication framework for rapid development of maintainable high performance protocol servers & clients 3. Meaning... A set of abstractions to build your server with: Good performance Low resource consumption Unified API for various transport types Customizable thread model Good documentation and examples! 4. When to use it? Good choice to build an application server Allows to implement custom protocols Dont need to worry (too much) about concurrency Layered architecture allows to reuse code And not so good for building a web application Little support for web technologies There are much better tools out there 5. The basic API Channel: think of it as a connection MessageEvent: represents the transmission orreception of a message ChannelHandler: reacts to events that happen ina Channel & contains the app logic ChannelPipeline: is an ordered list ofChannelHandlers 6. Socket.read() Socket.write() Upstream HandlerUpstream / Downstream HandlerDownstream HandlerThe basic API Upstream HandlerChannelPipeline 7. Example: echo serverpublic class EchoServer { public static void main(String[] args) { // ChannelFactory manages the creation/removal of Channels ChannelFactory factory = new NioServerSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool()); // Using helper class to boostrap... ServerBootstrap bootstrap = new ServerBootstrap(factory); // Define our ChannelPipelines bootstrap.setPipelineFactory(new ChannelPipelineFactory() { public ChannelPipeline getPipeline() { ChannelPipeline pipeline = getPipeline(); pipeline.addLast("echoHandler", new EchoHandler()); return pipeline; } }); bootstrap.bind(new InetSocketAddress(8080)); // Thats all! } 8. Example: echo serverpublic class EchoHandler extends SimpleChannelUpstreamHandler {@Overridepublic void messageReceived(ChannelHandlerContext ctx,MessageEvent e) throws Exception {// ctx allows to access to the Channel and ChannelPipelineChannel ch = ctx.getChannel();// In Netty3.6 Messages are of class Object// This is improved in Netty 4 where ChannelHandlers are// typed with the message they handlech.write(e.getMessage());}@Overridepublic void exceptionCaught(ChannelHandlerContext ctx,ExceptionEvent e) {// Something unexpected: closed connection, timeout?e.getCause().printStackTrace();Channel ch = e.getChannel();ch.close();} 9. Thread model One boss thread for each socket. Acceptsconnections and passes them to the workers A worker thread performs non-blocking logicfor one or multiple Channels If some blocking operations are needed (e.g.access to DB or files), those ChannelHandlersmust be moved to another executor 10. Example: webservicepublic class WebServicePipelineFactory implementsChannelPipelineFactory {private static final ExecutionHandler executionHandler =new ExecutionHandler(new OrderedMemoryAwareThreadPoolExecutor(16, 1048576, 1048576));@Overridepublic ChannelPipeline getPipeline() throws Exception {ChannelPipeline p = pipeline();p.addLast("decoder", new HttpRequestDecoder());p.addLast("aggregator", new HttpChunkAggregator(65536));p.addLast("encoder", new HttpResponseEncoder());p.addLast("deflater", new HttpContentCompressor());// The executor is shared among pipelines. All handlers// added after will be executed in another thread poolp.addLast("executor", executionHandler);p.addLast("myHandler", new MyBlockingHandler());return p;}