New to Netty - Basic simple questions

Trustin Lee trustin at gleamynode.net
Tue Jan 6 20:15:42 EST 2009


Hi,

First off, welcome! :)

On Wed, Jan 7, 2009 at 6:34 AM, new2nw <makunag at hotmail.com> wrote:

> The code is based totally on factorial example. And here are my dumb
> questions:
>
> How do I handle netty client?
>
> 1. For each request(thread) out of my process do I need to create a new
> channel (I mean create a new connection) as in the example above? Or shall
> I
> be using the same channel by not closing it and continue to write on it?


You can keep the channel open and keep sending requests.  Of course, you can
open and close a connection for each request, too.


> 2. If I create a new connection for each request; is there a performance
> issue?


Yes.  You will see a lot of TIME_WAIT state if you type 'netstat -an' in
your console.  If the number of TIME_WAIT state fills up, the channel will
not be opened as fast as before until TIME_WAIT states are cleared.  I'd
recommend you to keep the connection open and reconnect only when it's
disconnected for some reason.


> 3. If I create a new connection for each request; shall I use same channel
> factory ?


Yes.  I'd actually use only one channel factory to create all channels.

4. If I can reuse same connection, how do I match request with response? Do
> I have to have my own logic?


There are two ways to achieve this.  The first approach is to assume that
the responses are always responded in order.  Then you can safely assume
that the first response is always correlates to the first request and so
forth.  The second approach is to add some sequence ID for each request and
convey the same sequence ID for the corresponding response.  The second
approach is more flexible but you might be just fine with the first one.

5. In the above example using blocking queue and "one" as channel pipelien
> coverage, am I guranteed to receive the response corresponding to my
> request, or I have to look at the response and decide if its the right
> response?


ChannelPipelineCoverage is solely for documentation purpose.  It just
depends on how many handler instances you create to handler events.  If you
added the same handler instance for more than one ChannelPipeline, then you
will receive all responses from all channels in one handler instance.  If
you added a new handler for each Channel, then each handler instance will
receive the response which is related with the Channel it belongs to.


> 6. Is this an asynchronus process? I am confused as each request is waiting
> for response (synch) but at the same time each requests doesn't have to
> wait
> for the other to finish as netty server will keep receiving requests from
> each channels.


Everything is asynchronous.  The Factorial client is synchronous because it
explicitly waits for a response using a blocking queue.


> 7. Am I missing something important in the above example code.


I think you understood pretty well about Netty.  :)

You might want to add an ExecutionHandler to the pipeline though.  Please
take a look at the Javadoc of the classes in
org.jboss.netty.handler.execution.

HTH,

— Trustin Lee, http://gleamynode.net/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/netty-users/attachments/20090107/fdc50cbe/attachment.html 


More information about the netty-users mailing list