keepalive.keepalive
Class KeepAlive
java.lang.Object
|
+--java.lang.Thread
|
+--keepalive.keepalive.KeepAlive
- All Implemented Interfaces:
- java.lang.Runnable
- Direct Known Subclasses:
- ActiveKeepAlive, ActiveKeepAliveWithAYT, AreYouThere, HeartbeatClient, HeartbeatServer, IHearYou, NullKeepAlive, PassiveKeepAlive, PassiveKeepAliveWithIHY
- public abstract class KeepAlive
- extends java.lang.Thread
A Keep-Alive is an object that participates in a Keep-Alive protocol.
A Keep-Alive protocol is a way of detecting that a connection has been
lost so that socket resources can be cleaned up. Similarly, Keep-Alives can
detect that the remote application has ceased functioning (even when the
remote host is still alive). This is the base class for all Keep-Alives.
Includes most of the utility functionality, but leaves the run() method to
the subclasses.
Field Summary |
protected static java.lang.String |
ARE_YOU_THERE
A String to be sent by Are-You-There protocols. |
protected boolean |
endRequested
Shutdown switch for the thread. |
protected static java.lang.String |
I_HEAR_YOU
A String to be sent by I-Hear-You Keep-Alives in respond to
Are-You-There. |
protected static long |
TIME_BETWEEN_ACTIVE_PROBES
Time between active Keep-Alive probes. |
Fields inherited from class java.lang.Thread |
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY |
Constructor Summary |
KeepAlive()
Create a Keep-Alive with no receiver or sender set. |
KeepAlive(Receiver receiver,
Sender sender)
Create the Keep-Alive object with the given receiver and sender. |
Methods inherited from class java.lang.Thread |
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
ARE_YOU_THERE
protected static final java.lang.String ARE_YOU_THERE
- A String to be sent by Are-You-There protocols. I-Hear-You protocols have
to recognize this String and respond to it.
I_HEAR_YOU
protected static final java.lang.String I_HEAR_YOU
- A String to be sent by I-Hear-You Keep-Alives in respond to
Are-You-There.
In the examples given here, the "I Hear You" message has no special
function on the receiving side and could be replaced by a dummy message.
TIME_BETWEEN_ACTIVE_PROBES
protected static final long TIME_BETWEEN_ACTIVE_PROBES
- Time between active Keep-Alive probes.
Other times in the Keep-Alive protocols may be derived
from this to create a proper balance between the
various time periods.
The time period given here is far too short for
most of the KeepAlive classes, since significant
latency on the Internet does not indicate a connection
failure. A sender in TCP, for example,
allows a time period to go by before giving up on receiving
an acknowledgement from the other side. This means
that Keep-Alives based on the TCP ACKs must not expect
instant feedback.
We give a short time period in this test environment
to let us see promptly how KeepAlive works.
In a real program, you should set this value according to the needs of
your system, balancing promptness against minimized network load.
endRequested
protected volatile boolean endRequested
- Shutdown switch for the thread.
KeepAlive
public KeepAlive(Receiver receiver,
Sender sender)
- Create the Keep-Alive object with the given receiver and sender.
- Parameters:
receiver
- Object for sending messages.
Not necessarily used by all Keep-Alive subclasses.sender
- Object for receiving messages.
Not used by all Keep-Alive subclasses.
KeepAlive
public KeepAlive()
- Create a Keep-Alive with no receiver or sender set.
run
public abstract void run()
- Require the subclasses to implement run(), to prevent them
from using the run() method in class Thread with a null body.
- Overrides:
run
in class java.lang.Thread
setSender
public void setSender(Sender sender)
getSender
protected Sender getSender()
setReceiver
public void setReceiver(Receiver receiver)
getReceiver
protected Receiver getReceiver()
getDummyString
protected java.lang.String getDummyString()
- Get a dummy String for use in Keep-Alive messages probes.
For ease of debugging, this String will be "Dummy-" plus a
serial number. In a real application, you may want to
minimize load by sending as short a dummy String as possible.
Recall that there is a buffer on the socket level, typically 4 kilobytes
in size. If there is a network partition, short outgoing messages may
accumulate in this outgoing buffer for a while, so that you
will not get an IOException immediately.
Lower-level applications (typically written in C) will often
use a single byte as the dummy message.
- Returns:
- a dummy string
end
public void end()
- Request that the thread be shut down.
endRequested
protected boolean endRequested()
- Indicate if the thread is being shut down.
- Returns:
- true iff the thread is being shut down.
getSilentTimeForIncoming
protected long getSilentTimeForIncoming()
- How long since we heard from the other side.
Passive Keep-Alives and Are-You-Theres in
may make use of this method.
- Returns:
- the number of milliseconds since we heard from the other side,
or return 0 if receiver == null