public final class XCollie extends Object
An application or a service uses this mechanism to check whether a key service procedure times out. For example, if a procedure is not completed within 5 seconds as expected, the application is notified of the timeout. Upon receiving the notification, the application will take proper measures.
// Start timeout detection.
int id = XCollie.getInstance().setTimeout("ServiceTimeout", 5, new Runnable(){
@Override
public void run () {
// do something
}
}, XCollie.XCOLLIE_FLAG_NOOP);
// Do something.
// Cancel timeout detection.
XCollie.getInstance().cancelTimeout(id);
| 限定符和类型 | 字段 | 说明 |
|---|---|---|
static long |
XCOLLIE_FLAG_LOG |
Indicates that logs are printed after a timeout detected via
setTimeout. |
static long |
XCOLLIE_FLAG_NOOP |
Indicates that no operation is performed after a timeout detected via
setTimeout. |
static long |
XCOLLIE_FLAG_RECOVERY |
Indicates that the application or service exits for recovery after a timeout detected via
setTimeout. |
static int |
XCOLLIE_INVALID_ID |
Indicates an invalid value of the timeout detection handle to be returned
when timeout detection fails to be started by
setTimeout. |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
void |
cancelTimeout(int id) |
Cancels timeout detection.
|
static XCollie |
getInstance() |
Obtains an
XCollie instance. |
int |
setTimeout(String timeoutName,
int timeout,
Runnable callback,
long flag) |
Starts timeout detection.
|
boolean |
updateTimeout(int id,
long timeout) |
Updates the timeout length.
|
public static final long XCOLLIE_FLAG_LOG
setTimeout.public static final long XCOLLIE_FLAG_NOOP
setTimeout.public static final long XCOLLIE_FLAG_RECOVERY
setTimeout.public static final int XCOLLIE_INVALID_ID
setTimeout.public static XCollie getInstance()
XCollie instance.
The XCollie class works in singleton pattern.
XCollie instance.public int setTimeout(String timeoutName, int timeout, Runnable callback, long flag)
When handling a time-consuming procedure, an application or service calls this method to check whether the procedure times out.
timeoutName - Indicates the timeout name.timeout - Indicates the timeout length, in seconds.callback - Indicates the callback upon a timeout.flag - Indicates the processing flag for a timeout. The value can be:
XCOLLIE_FLAG_NOOP: No operation is performed except that the callback is invoked
to notify the application of the timeout.XCOLLIE_FLAG_RECOVERY: The callback is invoked to notify the application of the timeout,
and the application or service exits for recovery.XCOLLIE_FLAG_LOG: The callback is invoked to notify the application of the timeout,
and logs are printed.XCOLLIE_INVALID_ID otherwise.XCOLLIE_FLAG_NOOP,
XCOLLIE_FLAG_RECOVERY,
XCOLLIE_INVALID_IDpublic void cancelTimeout(int id)
id - Indicates the timeout detection handle.public boolean updateTimeout(int id,
long timeout)
id - Indicates the timeout detection handle.timeout - Indicates the updated timeout length, in seconds.true if the update is successful; returns false otherwise.