Netty Client re-connect implementation

Source: Internet
Author: User

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:

    1. Netty client needs to be re-connected when booting
    2. 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
    1. Http://stackoverflow.com/questions/19739054/whats-the-best-way-to-reconnect-after-connection-closed-in-netty
    2. Https://github.com/netty/netty/blob/master/example/src/main/java/io/netty/example/uptime/UptimeClientHandler.java
    3. Http://tterm.blogspot.jp/2014/03/netty-tcp-client-with-reconnect-handling.html
    4. Ctx.close vs Ctx.channel (). Close
    5. Ctx.write vs Ctx.channel (). Write

Netty Client re-connect implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.