public class KeepAliveFilter extends IoFilterAdapter
IoFilter
that sends a keep-alive request on
IoEventType.SESSION_IDLE
and sends back the response for the
sent keep-alive request.
IoSessionConfig.setIdleTime(IdleStatus, int)
IdleStatus
s that
this filter is interested in automatically (e.g. IdleStatus.READER_IDLE
and IdleStatus.WRITER_IDLE
.) Changing the idleTime
of the IdleStatus
s can lead this filter to a unexpected behavior.
Please also note that any IoFilter
and IoHandler
behind
KeepAliveFilter
will not get any IoEventType.SESSION_IDLE
event. To receive the internal IoEventType.SESSION_IDLE
event,
you can call setForwardEvent(boolean)
with true.
KeepAliveMessageFactory
KeepAliveMessageFactory
, which determines a received or sent
message is a keep-alive message or not and creates a new keep-alive
message:
Name | Description | Implementation |
---|---|---|
Active |
You want a keep-alive request is sent when the reader is idle.
Once the request is sent, the response for the request should be
received within keepAliveRequestTimeout seconds. Otherwise,
the specified KeepAliveRequestTimeoutHandler will be invoked.
If a keep-alive request is received, its response also should be sent back.
|
Both KeepAliveMessageFactory.getRequest(IoSession) and
KeepAliveMessageFactory.getResponse(IoSession, Object) must
return a non-null.
|
Semi-active | You want a keep-alive request to be sent when the reader is idle. However, you don't really care if the response is received or not. If a keep-alive request is received, its response should also be sent back. |
Both KeepAliveMessageFactory.getRequest(IoSession) and
KeepAliveMessageFactory.getResponse(IoSession, Object) must
return a non-null, and the timeoutHandler property
should be set to KeepAliveRequestTimeoutHandler.NOOP ,
KeepAliveRequestTimeoutHandler.LOG or the custom KeepAliveRequestTimeoutHandler
implementation that doesn't affect the session state nor throw an exception.
|
Passive | You don't want to send a keep-alive request by yourself, but the response should be sent back if a keep-alive request is received. |
KeepAliveMessageFactory.getRequest(IoSession) must return
null and KeepAliveMessageFactory.getResponse(IoSession, Object)
must return a non-null.
|
Deaf Speaker | You want a keep-alive request to be sent when the reader is idle, but you don't want to send any response back. |
KeepAliveMessageFactory.getRequest(IoSession) must return
a non-null,
KeepAliveMessageFactory.getResponse(IoSession, Object) must
return null and the timeoutHandler must be set to
KeepAliveRequestTimeoutHandler.DEAF_SPEAKER .
|
Silent Listener | You don't want to send a keep-alive request by yourself nor send any response back. |
Both KeepAliveMessageFactory.getRequest(IoSession) and
KeepAliveMessageFactory.getResponse(IoSession, Object) must
return null.
|
KeepAliveMessageFactory.isRequest(IoSession, Object)
and
KeepAliveMessageFactory.isResponse(IoSession, Object)
properly
whatever case you chose.
KeepAliveFilter
will notify its KeepAliveRequestTimeoutHandler
when KeepAliveFilter
didn't receive the response message for a sent
keep-alive message. The default handler is KeepAliveRequestTimeoutHandler.CLOSE
,
but you can use other presets such as KeepAliveRequestTimeoutHandler.NOOP
,
KeepAliveRequestTimeoutHandler.LOG
or KeepAliveRequestTimeoutHandler.EXCEPTION
.
You can even implement your own handler.
KeepAliveRequestTimeoutHandler.DEAF_SPEAKER
KeepAliveRequestTimeoutHandler.DEAF_SPEAKER
is a special handler which is
dedicated for the 'deaf speaker' mode mentioned above. Setting the
timeoutHandler property to KeepAliveRequestTimeoutHandler.DEAF_SPEAKER
stops this filter from waiting for response messages and therefore disables
response timeout detection.IoFilter.NextFilter
Constructor and Description |
---|
KeepAliveFilter(KeepAliveMessageFactory messageFactory)
Creates a new instance with the default properties.
|
KeepAliveFilter(KeepAliveMessageFactory messageFactory,
IdleStatus interestedIdleStatus)
Creates a new instance with the default properties.
|
KeepAliveFilter(KeepAliveMessageFactory messageFactory,
IdleStatus interestedIdleStatus,
KeepAliveRequestTimeoutHandler policy)
Creates a new instance with the default properties.
|
KeepAliveFilter(KeepAliveMessageFactory messageFactory,
IdleStatus interestedIdleStatus,
KeepAliveRequestTimeoutHandler policy,
int keepAliveRequestInterval,
int keepAliveRequestTimeout)
Creates a new instance.
|
KeepAliveFilter(KeepAliveMessageFactory messageFactory,
KeepAliveRequestTimeoutHandler policy)
Creates a new instance with the default properties.
|
Modifier and Type | Method and Description |
---|---|
IdleStatus |
getInterestedIdleStatus() |
KeepAliveMessageFactory |
getMessageFactory() |
int |
getRequestInterval() |
int |
getRequestTimeout() |
KeepAliveRequestTimeoutHandler |
getRequestTimeoutHandler() |
boolean |
isForwardEvent() |
void |
messageReceived(IoFilter.NextFilter nextFilter,
IoSession session,
Object message)
Filters
IoHandler.messageReceived(IoSession,Object) event. |
void |
messageSent(IoFilter.NextFilter nextFilter,
IoSession session,
WriteRequest writeRequest)
Filters
IoHandler.messageSent(IoSession,Object) event. |
void |
onPostAdd(IoFilterChain parent,
String name,
IoFilter.NextFilter nextFilter)
Invoked after this filter is added to the specified parent.
|
void |
onPostRemove(IoFilterChain parent,
String name,
IoFilter.NextFilter nextFilter)
Invoked after this filter is removed from the specified parent.
|
void |
onPreAdd(IoFilterChain parent,
String name,
IoFilter.NextFilter nextFilter)
Invoked before this filter is added to the specified parent.
|
void |
sessionIdle(IoFilter.NextFilter nextFilter,
IoSession session,
IdleStatus status)
Filters
IoHandler.sessionIdle(IoSession,IdleStatus) event. |
void |
setForwardEvent(boolean forwardEvent)
Sets if this filter needs to forward a
IoEventType.SESSION_IDLE event to the next filter. |
void |
setRequestInterval(int keepAliveRequestInterval)
Sets the interval for keepAlive messages
|
void |
setRequestTimeout(int keepAliveRequestTimeout)
Sets the timeout
|
void |
setRequestTimeoutHandler(KeepAliveRequestTimeoutHandler timeoutHandler)
Set the timeout handler
|
destroy, exceptionCaught, filterClose, filterWrite, init, inputClosed, onPreRemove, sessionClosed, sessionCreated, sessionOpened, toString
public KeepAliveFilter(KeepAliveMessageFactory messageFactory)
IdleStatus.READER_IDLE
KeepAliveRequestTimeoutHandler.CLOSE
messageFactory
- The message factory to usepublic KeepAliveFilter(KeepAliveMessageFactory messageFactory, IdleStatus interestedIdleStatus)
KeepAliveRequestTimeoutHandler.CLOSE
messageFactory
- The message factory to useinterestedIdleStatus
- The IdleStatus the filter is interested inpublic KeepAliveFilter(KeepAliveMessageFactory messageFactory, KeepAliveRequestTimeoutHandler policy)
IdleStatus.READER_IDLE
messageFactory
- The message factory to usepolicy
- The TimeOut handler policypublic KeepAliveFilter(KeepAliveMessageFactory messageFactory, IdleStatus interestedIdleStatus, KeepAliveRequestTimeoutHandler policy)
messageFactory
- The message factory to useinterestedIdleStatus
- The IdleStatus the filter is interested inpolicy
- The TimeOut handler policypublic KeepAliveFilter(KeepAliveMessageFactory messageFactory, IdleStatus interestedIdleStatus, KeepAliveRequestTimeoutHandler policy, int keepAliveRequestInterval, int keepAliveRequestTimeout)
messageFactory
- The message factory to useinterestedIdleStatus
- The IdleStatus the filter is interested inpolicy
- The TimeOut handler policykeepAliveRequestInterval
- the interval to usekeepAliveRequestTimeout
- The timeout to usepublic IdleStatus getInterestedIdleStatus()
IdleStatus
public KeepAliveRequestTimeoutHandler getRequestTimeoutHandler()
public void setRequestTimeoutHandler(KeepAliveRequestTimeoutHandler timeoutHandler)
timeoutHandler
- The instance of KeepAliveRequestTimeoutHandler
to usepublic int getRequestInterval()
public void setRequestInterval(int keepAliveRequestInterval)
keepAliveRequestInterval
- the interval to setpublic int getRequestTimeout()
public void setRequestTimeout(int keepAliveRequestTimeout)
keepAliveRequestTimeout
- The timeout to setpublic KeepAliveMessageFactory getMessageFactory()
public boolean isForwardEvent()
IoEventType.SESSION_IDLE
event to the next filter.
By default, the value of this property is false.public void setForwardEvent(boolean forwardEvent)
IoEventType.SESSION_IDLE
event to the next filter.
By default, the value of this property is false.forwardEvent
- a flag set to tell if the filter has to forward a IoEventType.SESSION_IDLE
eventpublic void onPreAdd(IoFilterChain parent, String name, IoFilter.NextFilter nextFilter) throws Exception
IoFilter.init()
is invoked.onPreAdd
in interface IoFilter
onPreAdd
in class IoFilterAdapter
parent
- the parent who called this methodname
- the name assigned to this filternextFilter
- the IoFilter.NextFilter
for this filter. You can reuse
this object until this filter is removed from the chain.Exception
- If an error occurred while processing the eventpublic void onPostAdd(IoFilterChain parent, String name, IoFilter.NextFilter nextFilter) throws Exception
IoFilter.init()
is invoked.onPostAdd
in interface IoFilter
onPostAdd
in class IoFilterAdapter
parent
- the parent who called this methodname
- the name assigned to this filternextFilter
- the IoFilter.NextFilter
for this filter. You can reuse
this object until this filter is removed from the chain.Exception
- If an error occurred while processing the eventpublic void onPostRemove(IoFilterChain parent, String name, IoFilter.NextFilter nextFilter) throws Exception
IoFilter.destroy()
is invoked.onPostRemove
in interface IoFilter
onPostRemove
in class IoFilterAdapter
parent
- the parent who called this methodname
- the name assigned to this filternextFilter
- the IoFilter.NextFilter
for this filter. You can reuse
this object until this filter is removed from the chain.Exception
- If an error occurred while processing the eventpublic void messageReceived(IoFilter.NextFilter nextFilter, IoSession session, Object message) throws Exception
IoHandler.messageReceived(IoSession,Object)
event.messageReceived
in interface IoFilter
messageReceived
in class IoFilterAdapter
nextFilter
- the IoFilter.NextFilter
for this filter. You can reuse this
object until this filter is removed from the chain.session
- The IoSession
which has received this eventmessage
- The received messageException
- If an error occurred while processing the eventpublic void messageSent(IoFilter.NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception
IoHandler.messageSent(IoSession,Object)
event.messageSent
in interface IoFilter
messageSent
in class IoFilterAdapter
nextFilter
- the IoFilter.NextFilter
for this filter. You can reuse this
object until this filter is removed from the chain.session
- The IoSession
which has received this eventwriteRequest
- The WriteRequest
that contains the sent messageException
- If an error occurred while processing the eventpublic void sessionIdle(IoFilter.NextFilter nextFilter, IoSession session, IdleStatus status) throws Exception
IoHandler.sessionIdle(IoSession,IdleStatus)
event.sessionIdle
in interface IoFilter
sessionIdle
in class IoFilterAdapter
nextFilter
- the IoFilter.NextFilter
for this filter. You can reuse this
object until this filter is removed from the chain.session
- The IoSession
which has received this eventstatus
- The IdleStatus
typeException
- If an error occurred while processing the eventCopyright © 2004–2018 Apache MINA Project. All rights reserved.