Frank Ross
Frank Ross

Reputation: 349

Netty: stop reconnecting and shutdown

I plan to modify Netty UptimeClient (see here). The original version is designed to run endlessly: it reconnects to the host in case of disconnection. I'd like to add a 'terminate' method in UptimeClient.java. The method would disconnect or stop the reconnection process from an outside thread, shutdown Netty gracefully, and return.

Since the client channel can change because of the reconnection process, would it be safe to keep all channels returned by 'bootstrap.connect()' in a ChannelGroup, and call 'close' using the group before releasing Netty's resources?

How would you implement a 'terminate' method? (edit: using 3.7.0)

Upvotes: 3

Views: 4392

Answers (1)

trustin
trustin

Reputation: 12351

In Nety 4.0, you can just call shutdownGracefully(...) on the EventLoopGroup that manages all your channels. Then all existing channels will be closed automatically and the reconnection attempt should be rejected. Please feel free to file an issue if this doesn't work.

If you are using Netty 3.x, unfortunately, there's no automatic mechanism for this. You have to ensure to close all channels, because the event loop will not terminate itself until it has a channel to manage. For more information, please refer to NioClientChannelFactory ('Life cycle of threads and graceful shutdown')

Upvotes: 5

Related Questions