Newbie questions

"이희승 (Trustin Lee)" trustin at gmail.com
Sun Aug 16 22:54:22 EDT 2009


On 08/15/2009 04:05 PM, Bozo Juretic wrote:
> Christian Migowski wrote:
>> Hi,
>>
>> On Tue, Aug 11, 2009 at 10:13 AM, Bozo Juretic<bjuretic at inet.hr> wrote:
>>   
>>> Hi all,
>>>
>>> I have two newbie questions; I couldn't find examples dealing with those
>>> two, so perhaps someone can give me a hand here.
>>>
>>> 1. What is the best way to implement reconnecting mechanism for TCP
>>> client in which the connection with the server should be up at all
>>> times. I need to send periodic "ping status" messages to the server as
>>> well. At the start of app I need to connect to host (and retry if host
>>> is not available), and if the connection goes down I need to reconnect
>>> up with the server in some smart way (perhaps you have reconnecting
>>> strategies implemented somewhere in netty code?).
>>>     
>>
>> As far as I know, there is not (yet) automatic reconnection in Netty -
>> ie. you need to implement it by yourself. Pass a reference of the
>> ClientBootStrap to the handler where you want to handle the reconnect
>> and then use it in channelClosed()
>>
>> for example (don't use it as-it-is!):
>>
>> while(ch.isConnected()) {
>> ChannelFuture f = bootstrap.connect(new InetSocketAddress(host, port));
>> f.awaitUninterruptibly();
>> ch = f.getChannel();
>> }
>>
>> The "ping" mechanism can be implemented very well using a
>> IdleChannelHandler in your pipeline - there are examples within Netty
>> for this (package timeout).
>>   
> I've done it, and it works, although I had to replace await() with a
> listener or netty complained.
> 
> Another thing related to IdleStateHandler.. it works for all data
> sent/received.
> 
> What's the best way to check if the single request message got response
> or not in specified time? I guess there should be a separate thread that
> would browse some kind of a queue (filled by handler with all incoming
> messages), read messages from it and see if some expired (all of this
> outside of netty handler)? I'm not sure how useful IdleStateHandler is
> here since it may as well happen that a lot of traffic goes over the
> wire, but none of it is the required response message, so
> IdleStateHandler would never send me an event I need. Is there a netty
> component that can send a MessageTimeoutedEvent for a specific message
> request, where a message would be registered with this component before
> channel.write(), and then it would work similar to an IdleStateHandler -
> you could catch the exception and place your business logic there (i.e.
> send error to the original requestor).

You could use org.jboss.netty.util.HashedWheelTimer and schedule a
timeout for each request, cancel the scheduled timeout when a
corresponding response is received, and handle the timeout if no
response was received.

HTH,
Trustin


More information about the netty-users mailing list