Newbie questions
"이희승 (Trustin Lee)"
trustin at gmail.com
Sun Aug 16 22:53:01 EDT 2009
On 08/15/2009 03:49 PM, Bozo Juretic wrote:
> Ernesto A. wrote:
>> Hello,
>>
>>
>>> 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?).
>>>
>>
>> About this first question, I think this part belongs to what in netty
>> is called the business logic, or the object in your network framework
>> that will take care of handling connections/disconnections, maintain
>> session state etc. You should check the javadocs for
>> SimpleChannelHandler, this class provides exactly what you need
>> through callbacks. There are plenty of examples about how to use it on
>> netty's website, I recommend reading the guide first and foremost, so
>> you could get in touch with netty principles and get an idea of how
>> things fit together.
>>
>>
> I did reconnects using the IdleStateHandler and bootstrap.connect() in
> the main thread. This seems to be a problem, so it seems that the
> bootstrap.connect should be run in a separate thread. Thus now I have
> this in the main thread:
>
> Channel channel;
> do {
> logger.info("Connecting to server " + host + ":" + port);
> ChannelFuture f = bootstrap.connect(new
> InetSocketAddress(host, port));
>
> // Wait until the connection attempt succeeds or fails
> f.addListener(new ChannelFutureListener() {
>
> public void operationComplete(ChannelFuture future) {
> if (future.isSuccess()) {
> logger.info("Connected to server");
> connected = true;
> } else {
> logger.info("Connection to server failed: " +
> future.getCause().getMessage());
> }
> }
> });
>
> try {
> Thread.sleep(15000);
> } catch (InterruptedException ex) {
> logger.warn("interrupted", ex);
> }
>
> } while (!connected);
>
> This is a problem since there is some other code that needs to be run
> afterwards which takes over this thread (JMS consumer.receive()).
>
> I did reconnects of a failed existing connection with IdleStateHandler's
> help. Is there a way to put this "first time connect" code (above) in
> the handler's channelOpen(), so that it is run by netty, and my main
> thread is left to me for my tasks, since I would like to avoid forking
> threads on my own (maybe the code will be ported to application server
> at some point)?
>
> Also, it the Thread.sleep() a good way to pause execution in netty (i.e.
> if I want to wait before I attempt another (re) connect)?
I would add a listener to the closeFuture of a channel to get notified
on disconnection:
channel.getCloseFuture().addListener(... /* reconnect */ ...);
If you need some delay between disconnection and reconnection, you could
use a java.util.Timer or something similar to schedule reconnection in
your closeFuture listener. Netty provides
org.jboss.netty.util.HashedWheelTimer, so you might want to give it a try.
Please let me know if I missed something.
HTH,
Trustin
More information about the netty-users
mailing list