From:http://itindex.net/detail/54161-netty-client
When we implement a TCP client with Netty, we certainly hope that Netty can reconnect automatically when the connection is broken.
Netty client needs to be re-connected in two cases:
- Netty client needs to be re-connected when booting
- Reconnection is required when the program is running.
For the first case, the author of Netty the solution on StackOverflow,
For the second case, a solution is implemented in the Netty example uptime.
Thomas, in his article, provides examples of how these two approaches are implemented.
Implement the Channelfuturelistener used to monitor if the connection is successful at startup, and retry if unsuccessful:
1 2 3 4 5 6 7 8 9 28 of the same. |
public class Client {private Eventloopgroup loop = new Nioeventloopgroup (); public static void Main (string[] args) {new Client (). run (); } public Bootstrap Createbootstrap (Bootstrap Bootstrap, Eventloopgroup eventloop) {if (Bootstrap! = N ull) {Final Myinboundhandler handler = new Myinboundhandler (this); Bootstrap.group (EventLoop); Bootstrap.channel (Niosocketchannel.class); Bootstrap.option (channeloption.so_keepalive, true); Bootstrap.handler (New channelinitializer<socketchannel> () {@Override protected void I Nitchannel (Socketchannel socketchannel) throws Exception {Socketchannel.pipeline (). AddLast (handler); } }); Bootstrap.remoteaddress ("localhost", 8888); Bootstrap.connect (). AddListener (New Connectionlistener (this)); } return bootstrap; } public void Run () {Createbootstrap (New Bootstrap (), loop); } } |
connectionlistener Responsible for re-connecting:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class Connectionlistener implements Channelfuturelistener { Private Client client; Public Connectionlistener (client client) {this.client = client; } @Override public void Operationcomplete (Channelfuture channelfuture) throws Exception {if (!c Hannelfuture.issuccess ()) {System.out.println ("reconnect"); Final EventLoop loop = Channelfuture.channel (). EventLoop (); Loop.schedule (New Runnable () {@Override public void run () {Client.createboot Strap (new Bootstrap (), loop); }}, 1L, timeunit.seconds); } } } |
also in Channelhandler monitoring If the connection is broken or broken, it must be connected again:
1 2 3 4 5 6 7 8 9 Ten - 16 17 |
public class Myinboundhandler extends Simplechannelinboundhandler { Private client client; Public Myinboundhandler (client client) { this.client = client; } @Override public void Channelinactive (Channelhandlercontext ctx) throws Exception { final eventloop EventLoop = Ctx.channel (). EventLoop (); Eventloop.schedule (New Runnable () { @Override public void Run () { client.createbootstrap (new Bootstrap (), eventloop); } , 1L, timeunit.seconds); Super.channelinactive (CTX); } } |
Reference documents
- Http://stackoverflow.com/questions/19739054/whats-the-best-way-to-reconnect-after-connection-closed-in-netty
- Https://github.com/netty/netty/blob/master/example/src/main/java/io/netty/example/uptime/UptimeClientHandler.java
- Http://tterm.blogspot.jp/2014/03/netty-tcp-client-with-reconnect-handling.html
- Ctx.close vs Ctx.channel (). Close
- Ctx.write vs Ctx.channel (). Write
Netty Client re-connect implementation