Mina's Heartbeat protocol application

Source: Internet
Author: User

Transferred from: http://my.oschina.net/yjwxh/blog/174633

Abstract heartbeat protocol, based on CS mode system development is a more common and effective connection detection method, recently in the Mina framework, originally wrote a Heartbeat protocol implementation, and then suddenly found that the Mina itself with such a heartbeat to achieve, feeling in the framework of a small worship, in practice at the same time to study a bit!

MINA TCP/IP SOCKET heartbeat MINA itself provides a filter class: Org.apache.mina.filter.keepalive. Keepalivefilter, the filter is used to send and feedback heartbeat packets (keep-alive request/response) when IO is idle.

Speaking of Keepalivefilter, this class is necessary to first say its constructor, that is, to instantiate what the class needs, the parameters of the class constructor three are:
(1) Keepavliemessagefactory: this instance reference is used to determine whether the received and sent packets are heartbeat packets, and the implementation of the heartbeat request package
(2) Idlestatus: The idle state that the filter is concerned about, the default is that the read is idle. The heartbeat packet is sent when the reading channel is idle
(3) Keepaliverequesttimeouthandler: The processing mechanism of time-out without feedback after Heartbeat packet request closes the connection by default of close

The first step is to implement the interface keepalivemessagefactory. The abstract methods in this interface are:
Modifier and Type Method and Description
Object getrequest (Iosession session) Returns A (new) Keep-alive request message.
Object GetResponse (iosession session, Object request) Returns A (new) response message for the specified keep-alive request.
Boolean isrequest (iosession session, Object message) Returns true if and only if the specified message is a keep-alive request message.
Boolean IsResponse (iosession session, Object message) Returns true if and only if the specified message is a keep-alive response message;

In general, the heartbeat mechanism is divided into the following four categories:
1, Active: When the Read channel is idle to send a heartbeat request, once the heartbeat request is sent, you need to receive the heartbeat feedback within keepaliverequesttimeout time, Otherwise the Keepaliverequesttimeouthandler will be called, and when a heartbeat request packet is received, the heartbeat feedback will also be sent immediately.
For the active heartbeat mechanism: Keepalivemessagefactory class implementation method: getrequest(Iosession session) and GetResponse(iosession session, Object request) must return non-empty.

2, semi-active semi-active: When the Read channel is idle to send a heartbeat request, but does not care whether the heartbeat feedback is not, when a heartbeat request packet is received, then the heartbeat feedback will also be issued immediately.
For the semi-active heartbeat mechanism: Keepalivemessagefactory class implementation method: getrequest(Iosession session) and GetResponse(iosession session, Object request) must return non-empty. and the heartbeat packet request after the timeout without feedback processing mechanism set to Keepaliverequesttimeouthandler.noop (do not do any processing), KeepAliveRequestTimeoutHandler.LOG ( Only output warning messages do not do other processing)

3, Passive passive: The current IO does not want to actively send a heartbeat request, but when a heartbeat request is received, the heartbeat feedback is also issued immediately.
For the passive heartbeat mechanism: Keepalivemessagefactory class implementation method: getrequest(iosession session) must feedback null and GetResponse(iosession session, Object request) must feedback non-null.

4, Deaf speaker Deaf Type: The current IO will actively send a heartbeat request, but does not want to send any heartbeat feedback.
For the deaf-type heartbeat mechanism: Keepalivemessagefactory class implementation method: getrequest(iosession session) must feedback Non-null and GetResponse(iosession session, Object request) must have feedback of NULL and set Keepaliverequesttimeouthandler to Deaf_speaker.

5, Sient-listener persistent monitor type: neither want to send heartbeat request nor send heartbeat feedback.
For the continuous listening type heartbeat mechanism: Keepalivemessagefactory class implementation method: getrequest(iosession session) must feedback null and GetResponse(iosession session, Object request) must have feedback null.


Heartbeat packet Request timeout after processing mechanism: interface Keepaliverequesttimeouthandler, generally this processing is mainly for the ability to send heartbeat request heartbeat mechanism.
1.CLOSE: Close Connection
2,log: Output warning message
3,noop: Do not do any processing
4,exception: Throwing Exceptions
5,deaf_speaker: A special treatment that stops the current filter on the heartbeat feedback monitoring, so that the filter loses the request timeout detection function. (Let it become deaf)
6,keepaliverequesttimeout (Keepalivefilter filter, iosession session); Custom processing



The following is an example of a client and a server and a separate instance:
Server:
As an example of a passive heartbeat mechanism, the server passively accepts a heartbeat request after receiving a client connection and shuts down the client connection when the client heartbeat request is not received within the specified time
The main code is as follows:
Keepalivemessagefactoryimpl Kamfi = new Keepalivemessagefactoryimpl ();
Implementation method: Public Boolean isrequest (iosession session, Object message): Determines if heartbeat request packet is true
public Boolean IsResponse (iosession session, Object message): Because of the passive heartbeat mechanism, there is no request for feedback, so it returns false directly.
Public Object Getrequest (iosession session): Passive heartbeat mechanism no request so direct return null
public Object GetResponse (iosession session, object request): Returns a heartbeat feedback message based on heartbeat request requests Non-nul
Description: Keepalivemessagefactoryimpl is an implementation class for Keepalivemessagefactory, in which the implementation method satisfies the passive heartbeat mechanism.
Keepalivefilter KAF = new Keepalivefilter (Kamfi, Idlestatus.both_idle);
Description: Instantiate a keepalivefilter filter, pass in Keepalivemessagefactory reference, idlestatus parameter is Both_idle, and indicates that the start idle event is sent after the specified time interval getrequestinterval if the read-write channel for the current connection is idle.
Kaf.setforwardevent (TRUE); Idle event postback still calls the idled method in handler when the session enters the idle state
Note: Pay particular attention to this sentence, after using the Keepalivefilter, Iohandleradapter in the Sessionidle method is no longer called by default! That's why we have to add this, Sessionidle will be called.
Kaf.setrequestinterval (Heartperiod); This server needs to accept a heartbeat request every 10 seconds for a trained heartbeat otherwise the connection enters an idle state and emits a idled method callback
Description: Set the heartbeat packet request interval, in fact, for the passive heartbeat mechanism, set the heartbeat packet request interval seems to be useless, because it does not send the heartbeat packet, but it will trigger the Sessionidle event, we use this method, It is possible to determine whether the client does not have a heartbeat packet within that interval, and once the Sessionidle method is called, the client is considered to be missing the connection and kicking it out. So the parameter heartperiod is actually the idle monitoring time of the server for the client. Kaf.setrequesttimeout (5); Timeout If feedback is required after a heartbeat request is currently issued if the feedback exceeds this event, the connection Acceptor.getfilterchain () is closed by default. AddLast ("Heart", KAF);
Note: The filter is added to the entire communication filter chain.




Client:
The client will periodically send a heartbeat request (note that the timing must be less than the idle monitoring time on the server), and the heartbeat feedback needs to be monitored to determine if the connection is lost to the server. The heartbeat request for the server does not give feedback.
The main code is as follows:
Clientkeepalivefactoryimpl Ckafi = new Clientkeepalivefactoryimpl ();
Implementation method: Public Boolean isrequest (iosession session, Object message): The server does not send a request packet to the client, so it does not pay attention to the request packet and returns false directly
public Boolean IsResponse (iosession session, Object message): The client is interested in requesting feedback, so determine if the mesaage is a feedback package
Public Object Getrequest (iosession session): Gets the heartbeat request packet Non-null
public Object GetResponse (iosession session, Object request): The server does not send a heartbeat request to the client, and the client certainly does not have to feedback that the method returns null
Description: Clientkeepalivefactoryimpl is an implementation class for Keepalivemessagefactory.

Keepalivefilter KAF = new Keepalivefilter (Ckafi, idlestatus.reader_idle,keepaliverequesttimeouthandler.close);
Description: Instantiate a keepalivefilter filter, pass in Keepalivemessagefactory reference, idlestatus parameter is Reader_idle, and indicates that if the current connected read channel is idle, the heartbeat request is sent after the specified interval of getrequestinterval, and an idle event is emitted. Keepaliverequesttimeouthandler set to Clos indicates that when the heartbeat request has not received feedback within the specified time, the call Close method closes the connection

Kaf.setforwardevent (TRUE);
Description: Continue calling Sessionidle time in Iohandleradapter

Kaf.setrequestinterval (Heart_interval);
Description: Sets the heartbeat packet request interval when the read channel of the connection is idle

Kaf.setrequesttimeout (heart_timeout);
Description: Wait for the feedback timeout after setting the heartbeat package request. Call Keepaliverequesttimeouthandler.close after the time is exceeded

Connector.getfilterchain (). AddLast ("Heart", KAF); Note: The filter is added to the entire communication filter chain.

Mina's Heartbeat protocol application

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.