tag:blogger.com,1999:blog-19961064931202255132024-02-22T04:55:25.528-08:00"LIfe is like a box of chocolate, you never know which one you are gonna get " -Forest GumpUnknownnoreply@blogger.comBlogger1125tag:blogger.com,1999:blog-1996106493120225513.post-25703358065285943832014-02-23T12:40:00.003-08:002014-04-30T18:04:25.158-07:00Netty: Basics for beginners<div dir="ltr" style="text-align: left;" trbidi="on">
<h2>
</h2>
<h2>
</h2>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-49710604-1', 'bhardwaj-gaurav.blogspot.com');
ga('send', 'pageview');
</script>
<br />
<h2>
</h2>
<h2>
</h2>
<h2 style="text-align: left;">
<span style="font-weight: normal;">[Update:] please check my next post (Moving on to Netty4 ) <a href="http://wp.me/s4yzgW-n" target="_blank">here</a> </span></h2>
<h2 style="text-align: left;">
<span style="font-weight: normal;"><br /></span></h2>
<div>
<h2 style="text-align: left;">
<span style="font-weight: normal;">[If you dont like this GUI, click <a href="http://bhardwajgaurav.wordpress.com/2014/02/23/netty-basics-for-beginners/" target="_blank">here </a>to see this post in better format ]</span></h2>
</div>
<h2>
<ul style="text-align: left;">
<li><span style="font-family: Verdana, sans-serif;">Synchrnous vs Asynchronous:</span></li>
</ul>
</h2>
<div>
<div style="text-align: left;">
<span style="font-family: Verdana, sans-serif;">Suppose you go to <a class="g-profile" href="https://plus.google.com/117575809843355974839" target="_blank">+Starbucks Coffee</a> , you EnQueue (brush up Data Structures :P ) to order your coffee, you stay in the queue until you get your chance to order, and once you order and pay, you still stay there blocking the queue until you get your coffee, won't that be awkward ?? (if No then go Synchronous, that's best for you :) else come to the world of Netty ) because we love Asynchronous things in life e.g Chat vs Call. </span></div>
<span style="font-family: Verdana, sans-serif;"><br />
<i>In Synchronous systems you wait for reply, while in Asynchronous you don't.</i></span><br />
<div>
<h2>
</h2>
<h2>
<ul style="text-align: left;">
<li><b><span style="font-family: Verdana, sans-serif;">Why Netty: </span></b></li>
</ul>
</h2>
<div>
<div>
<span style="font-family: Verdana, sans-serif;">"Netty is an asynchronous event-driven network application framework </span></div>
<div>
<span style="font-family: Verdana, sans-serif;">for rapid development of maintainable high performance protocol servers & clients." from <a href="http://netty.io/" target="_blank">here</a> </span></div>
</div>
<div>
<ol style="text-align: left;">
<li><div>
<span style="font-family: Verdana, sans-serif; orphans: 2; text-align: -webkit-auto; widows: 2;">an asynchronous Non blocking IO API.</span></div>
</li>
<li><div>
<div>
<span style="orphans: 2; text-align: -webkit-auto; widows: 2;"><span style="font-family: Verdana, sans-serif; text-align: -webkit-auto;">provides low level data transfer and mechanism</span></span></div>
</div>
</li>
<li><div>
<div>
<span style="text-align: -webkit-auto;"><span style="font-family: Verdana, sans-serif; orphans: 2; text-align: -webkit-auto; widows: 2;">No response is generated, while invocation is instantaneous and response is generated in another thread</span></span></div>
</div>
</li>
<li><div>
<div>
<span style="orphans: 2; text-align: -webkit-auto; widows: 2;"><span style="font-family: Verdana, sans-serif; text-align: -webkit-auto;">works through listeners and events. </span></span></div>
<span style="font-family: Verdana, sans-serif; orphans: 2; text-align: -webkit-auto; widows: 2;">
</span></div>
</li>
<li><span style="font-family: Verdana, sans-serif;">still lot many reasons more to use it <a href="http://netty.io/" target="_blank">see here</a></span></li>
</ol>
</div>
<h3>
</h3>
<h2>
</h2>
<h2>
</h2>
<h2>
<ul style="text-align: left;">
<li><span style="font-family: Verdana, sans-serif;">What is NIO and how it differs from OIO:</span></li>
</ul>
</h2>
<div>
<div>
<div style="orphans: 2; text-align: -webkit-auto; widows: 2;">
<span style="font-family: Verdana, sans-serif;">Old IO was a stream of bytes and the new IO is a block of bytes, old one was a synchronous while this one is Asynchronous. NIO is good for the situations where you have to keep a lot of connections open and do a little work amongst them, like in chat applications. In Old IO you keep thread pools to manage lot of thread IO work. source <a href="http://howtodoinjava.com/2013/12/22/difference-between-standard-io-and-nio/" target="_blank">here</a> </span></div>
</div>
</div>
<div>
<h3>
</h3>
<h2>
<ul style="text-align: left;">
<li><span style="font-family: Verdana, sans-serif;">Netty terminology (Rough overview)</span></li>
</ul>
</h2>
<ol style="text-align: left;"><span style="font-family: Verdana, sans-serif; orphans: 2; text-align: -webkit-auto; widows: 2;">
<li><span style="text-align: -webkit-auto;">Channel: a abstraction for communication or transfer of data (byte array)</span></li>
<li><span style="text-align: -webkit-auto;">ChannelBuffer: </span><span style="text-align: -webkit-auto;">Byte array which can provide random or direct data storage mechanism.</span></li>
<li><span style="text-align: -webkit-auto;">ChannelPipeline: collection of handlers to process your events in the way you want. </span></li>
<li><span style="text-align: -webkit-auto;">ChannelFactory: Resource allocators, like thread pools.</span></li>
<li><span style="text-align: -webkit-auto;">ChannelHandler: Provides method to handle events on channel.</span></li>
<li><span style="text-align: -webkit-auto;">ChannelEvents: Events like connected, exception raised, message recieved etc. </span>see in detail <a href="http://netty.io/3.9/api/index.html" target="_blank">here</a>. </li>
</span></ol>
</div>
<h2 style="text-align: left;">
<b><u><span style="font-family: Verdana, sans-serif;">
Writing a simple client server application using Netty and understand its working. </span></u></b></h2>
</div>
<div>
<span style="font-family: Verdana, sans-serif;"><br /></span></div>
<div>
<h3>
</h3>
<h3 style="text-align: left;">
<b><span style="font-family: Verdana, sans-serif;">Write a Server ChannelHandler.(call it <a href="https://github.com/GauravBhardwaj/NettyClientServerChatExample" target="_blank">ChatServerHandler</a>)</span></b></h3>
<div>
<span style="font-family: Verdana, sans-serif;"><b>Step1:</b> Create a class named ChatServerHandler by extending SimpleChannelHandler later you can learn advanced ways of doing this. </span></div>
<div>
<span style="font-family: Verdana, sans-serif;"><br />
<b>Step2:</b> Override its methods to access ChannelEvents, i have overrided </span><br />
<ul style="text-align: left;">
<li><span style="font-family: Verdana, sans-serif;">messageRecieved(): helps us to fetch the message server recieved from client.<b> </b>MessageEvent e object provides getMessage() method to retreve message and you can use it to print or process. </span><div>
<span style="font-family: Verdana, sans-serif;"><i>Remember everything in Netty happens through channelBuffers.</i> </span></div>
</li>
<span style="font-family: Verdana, sans-serif;"><ul style="text-align: left;"><br /></ul>
<ul style="text-align: left;">@Override</ul>
</span><pre><code><span style="font-family: Verdana, sans-serif;"> public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
ChannelBuffer buf = (ChannelBuffer) e.getMessage();
while(buf.readable()) {
System.out.print((char) buf.readByte());
System.out.flush();
}</span></code></pre>
<li><span style="font-family: Verdana, sans-serif;">excaptionCaught(): helps you to get the exception while connecting or like events.</span></li>
</ul>
<span style="font-family: Verdana, sans-serif;">@Override</span><br />
<pre><code><span style="font-family: Verdana, sans-serif;">
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
e.getCause().printStackTrace();
Channel ch = e.getChannel();
ch.close();
}</span></code></pre>
<div>
<span style="font-family: Verdana, sans-serif;"><br /></span></div>
</div>
<div>
<span style="font-family: Verdana, sans-serif;"><script src="https://gist.github.com/GauravBhardwaj/9175332.js"></script>
</span>
<br />
<h3 style="text-align: left;">
<span style="font-family: Verdana, sans-serif;">Write a server to use this ChannelHandler(call it <a href="https://github.com/GauravBhardwaj/NettyClientServerChatExample/blob/master/ChatServer.java" target="_blank"><b>ChatServer</b></a>)</span></h3>
</div>
<div>
<span style="font-family: Verdana, sans-serif;"><b>Step1:</b> Create a ChannelFactory in server it has to be <span style="background-color: white; color: #333333; line-height: 18px; white-space: pre;">NioServerSocketChannelFactory there are several other channel Factories provided by Netty.</span></span></div>
<div>
<span style="background-color: white; color: #333333; font-family: Verdana, sans-serif; line-height: 18px; white-space: pre;"><br /></span></div>
<div>
<pre><code><span style="font-family: Verdana, sans-serif;"> ChannelFactory factory = new NioServerSocketChannelFactory(Executors.newCachedThreadPool(),Executors.newCachedThreadPool());</span></code></pre>
<span style="background-color: white; color: #333333; font-family: Verdana, sans-serif; line-height: 18px; white-space: pre;"><br /></span></div>
<span style="font-family: Verdana, sans-serif;"><b>Step2:</b> Create a server Bootstrap object it helps you create a basic setup of server else there are more detailed way of doing this, for simplicity i use this.</span><br />
<pre><code><span style="font-family: Verdana, sans-serif;"> ServerBootstrap bootstrap = new ServerBootstrap(factory);</span></code></pre>
<span style="font-family: Verdana, sans-serif;"><br /></span>
<br />
<div>
<span style="font-family: Verdana, sans-serif;"><b>Step3:</b> set your channelHandler to ChannelPipeline so that it can process messageEvents. </span></div>
<span style="font-family: Verdana, sans-serif;"><br /></span>
<br />
<div>
<pre><code><span style="font-family: Verdana, sans-serif;"> bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() {
return Channels.pipeline(new ChatServerHandler());
}
});</span></code></pre>
</div>
<div>
<b><span style="font-family: Verdana, sans-serif;"><br /></span></b>
<span style="font-family: Verdana, sans-serif;"><b>Step4:</b> Bind your server to run on a host and port.</span></div>
<div>
<pre><code><span style="font-family: Verdana, sans-serif;"> bootstrap.bind(new InetSocketAddress(8090));</span></code></pre>
</div>
<div>
</div>
<div>
</div>
<div>
<span style="font-family: Verdana, sans-serif;"><br />
</span><br />
<div style="text-align: left;">
<span style="font-family: Verdana, sans-serif;"><script src="https://gist.github.com/GauravBhardwaj/9175379.js"></script></span></div>
<div>
<span style="font-family: Verdana, sans-serif;"><br /></span></div>
<h3 style="text-align: left;">
<span style="font-family: Verdana, sans-serif;">Write a client (<a href="https://github.com/GauravBhardwaj/NettyClientServerChatExample/blob/master/ChatClient.java" target="_blank">ChatClient</a>) which simply takes user input and sends it to Channel.</span></h3>
</div>
<div>
<span style="font-family: Verdana, sans-serif;">Step1: Since Netty is asynchrnous in nature it does not assign dedicated thread for particular operation so we have create two threads:</span></div>
<div>
<ul style="text-align: left;">
<li><span style="font-family: Verdana, sans-serif;">Boss thread: they do main work like connect, bind and pass them for real work to worker threads. </span></li>
<li><span style="font-family: Verdana, sans-serif;">Worker thread: they do the real work.</span></li>
</ul>
</div>
<pre><code><span style="font-family: Verdana, sans-serif;">Executor BossPool = Executors.newCachedThreadPool();
Executor WorkerPool = Executors.newCachedThreadPool(); </span></code></pre>
<div>
<span style="font-family: Verdana, sans-serif;"><br />
</span></div>
<div>
<span style="font-family: Verdana, sans-serif;">Step2: Create a ClientChannelFactory object this time. </span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span></div>
<div>
<pre><code><span style="font-family: Verdana, sans-serif;"> ChannelFactory channelFactory = new NioClientSocketChannelFactory(BossPool,WorkerPool);</span></code></pre>
<span style="font-family: Verdana, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Verdana, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Verdana, sans-serif;">Step3: create a clientBootstrap object as we did in server</span></div>
<div>
<pre><code><span style="font-family: Verdana, sans-serif;"> ClientBootstrap clientBootstrap = new ClientBootstrap(channelFactory);</span></code></pre>
<span style="font-family: Verdana, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Verdana, sans-serif;">Step4: Again set the ChannelPipeline in client (you can ignore it as of now in later steps you will know pipelines better.)</span></div>
<div>
<span style="font-family: Verdana, sans-serif;">//setup the Channel PipeLine Factory
ChannelPipelineFactory pipelineFactory = new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() throws Exception {
return Channels.pipeline(
new ObjectEncoder()
);
}
}; </span></div>
<div>
<span style="font-family: Verdana, sans-serif;">Step5: Give the server socket address to which this client is supposed to connect to.</span></div>
<div>
<pre><code><span style="font-family: Verdana, sans-serif;"> InetSocketAddress addressToConnectTo = new InetSocketAddress("localhost", 8090);</span></code></pre>
<span style="font-family: Verdana, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Verdana, sans-serif;">Step6: Once again since Netty is asynchronous it doesn't give you a immeadiate result back instead it returns a channelFuture which is kind of a result which will be generated in future.</span></div>
<div>
<pre><code><span style="font-family: Verdana, sans-serif;"> ChannelFuture cf = clientBootstrap.connect(addressToConnectTo);</span></code></pre>
<span style="font-family: Verdana, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Verdana, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Verdana, sans-serif;">Step7: Now get the channel object and write your Input data to it.</span></div>
<div>
<pre><code><span style="font-family: Verdana, sans-serif;">channel.write(input.readLine().getBytes());</span></code></pre>
<span style="font-family: Verdana, sans-serif;"><br />
<script src="https://gist.github.com/GauravBhardwaj/9175417.js"></script></span></div>
<div>
<span style="font-family: Verdana, sans-serif;"><br /></span></div>
</div>
<div>
<span style="font-family: Verdana, sans-serif;">Import three source files and run ChatServer.java and clientChat.java and run it. </span><br />
<div style="text-align: left;">
<span style="font-family: Verdana, sans-serif;">git<b> </b>URL: </span><a href="https://github.com/GauravBhardwaj/NettyClientServerChatExample" style="font-family: Verdana, sans-serif;">https://github.com/GauravBhardwaj/NettyClientServerChatExample</a><span style="font-family: Verdana, sans-serif;"> </span></div>
<div style="-webkit-text-stroke-width: 0px; color: black; font-size: medium; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px;">
<div>
<div style="margin: 0px; text-align: left;">
<span style="font-family: Verdana, sans-serif;">Connect: <u style="color: blue; font-weight: normal;"><a href="http://www.linkedin.com/in/bhardwajg">http://www.linkedin.com/in/bhardwajg</a> </u></span></div>
<div style="font-weight: normal; margin: 0px;">
</div>
</div>
</div>
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;">PS:- This post needs to be updated to be used with Netty v4 , there are some changes in terminology as compared to v3, will try to come with a new post with those changes explained.</span><br />
<span style="font-family: Verdana, sans-serif;"><br />
References: </span><br />
<span style="font-family: Verdana, sans-serif;">1. <a href="http://seeallhearall.blogspot.com/2012/05/netty-tutorial-part-1-introduction-to.html">http://seeallhearall.blogspot.com/2012/05/netty-tutorial-part-1-introduction-to.html</a></span><br />
<span style="font-family: Verdana, sans-serif;">2. <a href="http://netty.io/3.9/api/index.html">http://netty.io/3.9/api/index.html</a> </span></div>
</div>
</div>
Unknownnoreply@blogger.com0