Netty timeout mechanism and implementation of heartbeat program

Source: Internet
Author: User
Tags telnet program

This article describes the principle of the Netty timeout mechanism and how to send a heartbeat to maintain the connection when the connection is idle.

Introduction to the Netty timeout mechanism

The Netty timeout type idlestate is mainly divided into:

All_idle: No data received or sent over time

Reader_idle: No data received over time

Writer_idle: No data sent over time

Under Netty's timeout package, the main classes are:

Idlestateevent: Timed-out event

Idlestatehandler: Timeout Status processing

Readtimeouthandler: Read Timeout status processing

Writetimeouthandler: Write timeout status processing

where Idlestatehandler contains read \ Write timeout status processing, such as

private static final int read_idel_time_out = 4; Read timeout

private static final int write_idel_time_out = 5;//Write timeout

private static final int all_idel_time_out = 7; All timeouts

New Idlestatehandler (Read_idel_time_out,

Write_idel_time_out, All_idel_time_out, timeunit.seconds));

In the example above, the read timeout defined in Idlestatehandler is 4 seconds, the write timeout is 5 seconds, and all other timeouts are 7 seconds.

Application Idlestatehandler

Since Idlestatehandler includes read \ Write timeout status processing, many times Readtimeouthandler, Writetimeouthandler can be used. Define another channelinitializer named Heartbeathandlerinitializer:

public class Heartbeathandlerinitializer extends Channelinitializer {

private static final int read_idel_time_out = 4; Read timeout

private static final int write_idel_time_out = 5;//Write timeout

private static final int all_idel_time_out = 7; All timeouts

@Override

protected void Initchannel (Channel ch) throws Exception {

Channelpipeline pipeline = Ch.pipeline ();

Pipeline.addlast (New Idlestatehandler (Read_idel_time_out,

Write_idel_time_out, All_idel_time_out, timeunit.seconds)); 1

Pipeline.addlast (New Heartbeatserverhandler ()); 2

}

}

Use Idlestatehandler to set the time for read and write timeouts

Defines a Heartbeatserverhandler processor that is used to handle timeouts when the heartbeat is sent

Defines a heartbeat processor

public class Heartbeatserverhandler extends Channelinboundhandleradapter {

Return a unreleasable view on the given bytebuf

Which'll just ignore release and retain calls.

private static final Bytebuf heartbeat_sequence = unpooled

. Unreleasablebuffer (Unpooled.copiedbuffer ("Heartbeat",

Charsetutil.utf_8)); 1

@Override

public void usereventtriggered (Channelhandlercontext ctx, Object evt)

Throws Exception {

if (evt instanceof idlestateevent) {//2

Idlestateevent event = (idlestateevent) evt;

String type = "";

if (event.state () = = Idlestate.reader_idle) {

Type = "Read Idle";

} else if (event.state () = = Idlestate.writer_idle) {

Type = "Write idle";

} else if (event.state () = = Idlestate.all_idle) {

Type = "All idle";

}

Ctx.writeandflush (Heartbeat_sequence.duplicate ()). AddListener (

Channelfuturelistener.close_on_failure); 3

System.out.println (Ctx.channel (). Remoteaddress () + "Timeout type:" + type);

} else {

Super.usereventtriggered (CTX, evt);

}

}

}

What to send when a heartbeat is defined

Determine if it is a idlestateevent event, and then handle

Send heartbeat content to client

Server

Server code is simple and listens on port 8082 after startup

Public final class Heartbeatserver {

static final int PORT = 8082;

public static void Main (string[] args) throws Exception {

Configure the server.

Eventloopgroup Bossgroup = new Nioeventloopgroup (1);

Eventloopgroup Workergroup = new Nioeventloopgroup ();

try {

Serverbootstrap B = new Serverbootstrap ();

B.group (Bossgroup, Workergroup)

. Channel (Nioserversocketchannel.class)

. option (Channeloption.so_backlog, 100)

. Handler (new Logginghandler (Loglevel.info))

. Childhandler (New Heartbeathandlerinitializer ());

Start the server.

Channelfuture f = b.bind (PORT). sync ();

Wait until the server socket is closed.

F.channel (). Closefuture (). sync ();

} finally {

Shut down all event loops to terminate all threads.

Bossgroup.shutdowngracefully ();

Workergroup.shutdowngracefully ();

}

}

}

Client Testing

The client uses the Telnet program that comes with the operating system:

Telnet 127.0.0.1 8082

Effect

20151106-heartbeat

Source

See Https://github.com/waylau/netty-4-user-guide-demos in Heartbeat bag

Welcome message discussion, add attention, continue to update!

Netty timeout mechanism and implementation of heartbeat program

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.