Netty realizes Heartbeat sending mechanism

Source: Internet
Author: User
Netty realizes heartbeat sending mechanism
Processing an idle connection is a common task, in order to be able to release resources in a timely manner, it is necessary to detect idle connections and timeouts on the server side, often by sending information to test an inactive link, usually called "Heartbeat," and then at the far end confirming whether it is still alive, Netty provides Idlestatehandler handle this process;
The following is an example implementation controller, at the service end of each interval of 5s (set to 5s only for testing convenience, the actual development can set a longer event interval) to send a heartbeat packet to the client, add a listener, if the client should ring the heartbeat packet, prove that the client connection is valid, If the client does not respond, the client connection is invalid, and the listener obtains the event and shuts down the service-side connection;
Complete sample address (client with sample): Https://gitee.com/assad/netty-test-sample/tree/master/netty-test-sample/src/main/java/heartbeatSample
The following is the implementation of the service-side Heartbeat pack controller, Idlestatehandlerinitializer
public class Idlestatehandlerinitializer extends channelinitializer<channel>{
@Override
protected void Initchannel (Channel ch) throws Exception {
Channelpipeline pipeline = Ch.pipeline ();
Adding an idle state processor, when more than 5s does not receive information about the remote connection, the processor propagates a Idlestatehandler
Pipeline.addlast (New Idlestatehandler (0,0,5, timeunit.seconds));
Add a Idlestatehandler-propagated idlestateevent processor
Pipeline.addlast (New Heartbeathandler ());
Add a normal inbound processor
Pipeline.addlast (New Echoserverhandler ());
  }
Idleevent event Handler
public static final class Heartbeathandler extends channelinboundhandleradapter{
private static final Bytebuf heartbeat_sequence = Unpooled.unreleasablebuffer (
Unpooled.copiedbuffer ("HEARTBEAT", Charset.forname ("UTF-8"));
@Override
public void usereventtriggered (Channelhandlercontext ctx, Object evt) throws Exception {
if (evt instanceof idlestateevent) {
When the idlestateevent is captured, the heartbeat is sent to the far end, and a listener is added, and the server connection is closed if the send fails
Ctx.writeandflush (Heartbeat_sequence.duplicate ()). AddListener (Channelfuturelistener.close_on_failure);
  }else{
If the captured time is not a idlestateevent, the event is passed to the next processor
Super.usereventtriggered (CTX,EVT);
  }
  }
  }
@Sharable
Normal Inbound Processor: Print information that can be sent by the client
public class Echoserverhandler extends channelinboundhandleradapter{
@Override
public void Channelread (Channelhandlercontext ctx, Object msg) throws Exception {
Bytebuf in = (BYTEBUF) msg;
System.out.println ("Server Received:" + in.tostring (charsetutil.utf_8));
  }
@Override
public void Exceptioncaught (Channelhandlercontext ctx, Throwable cause) throws Exception {
Cause.printstacktrace ();
Ctx.close ();
  }
  }
}

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.