|
xmlBlaster 2.2.0 client API | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.lang.Thread org.xmlBlaster.util.TimeoutPooled
public class TimeoutPooled
Allows you be called back after a given delay.
Note that this class should be called Timer, but with JDK 1.3 there will be a java.util.Timer. There is pool of threads used to execute the I_Timeout.timeout() callback. Timer callbacks should complete quickly to not exhaust the pool. This singleton is thread-safe. This class does not offer real-time guarantees, but usually notifies you within ~ 20 milliseconds of the scheduled time. Adding or removing a timer is good performing, also when huge amounts of timers (> 1000) are used.public class MyClass implements I_Timeout { ... TimeoutPooled timeout = new TimeoutPooled("TestTimer"); Timestamp timeoutHandle = timeout.addTimeoutListener(this, 4000L, "myTimeout"); ... public void timeout(Object userData) { // userData contains String "myTimeout" System.out.println("Timeout happened"); ... // If you want to activate the timer again: timeoutHandle = timeout.addTimeoutListener(this, 4000L, "myTimeout"); } ... // if you want to refresh the timer: timeoutHandle = timeout.refreshTimeoutListener(timeoutHandle, 1500L); ... }Or a short form:
TimeoutPooled timeout = new TimeoutPooled("TestTimer"); Timestamp timeoutHandle = timeout.addTimeoutListener(new I_Timeout() { public void timeout(Object userData) { System.out.println("Timeout happened"); System.exit(0); } }, 2000L, null);JDK 1.2 or higher only.
-logging/org.xmlBlaster.util.TimeoutPooled FINER
org.xmlBlaster.test.classtest.TimeoutTest
Nested Class Summary |
---|
Nested classes/interfaces inherited from class java.lang.Thread |
---|
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler |
Field Summary |
---|
Fields inherited from class java.lang.Thread |
---|
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY |
Constructor Summary | |
---|---|
TimeoutPooled()
Create a timer thread with a strong reference on the callback objects. |
|
TimeoutPooled(java.lang.String name)
Create a timer thread with a strong reference on the callback objects. |
|
TimeoutPooled(java.lang.String name,
boolean useWeakReference)
|
|
TimeoutPooled(java.lang.String name,
boolean useWeakReference,
java.util.concurrent.ThreadPoolExecutor threadPool)
|
Method Summary | |
---|---|
Timestamp |
addOrRefreshTimeoutListener(I_Timeout listener,
long delay,
java.lang.Object userData,
Timestamp key)
Checks if key is null -> addTimeoutListener else refreshTimeoutListener() in a thread save way. |
Timestamp |
addTimeoutListener(I_Timeout listener,
long delay,
java.lang.Object userData)
Add a listener which gets informed after 'delay' milliseconds. |
java.lang.String |
dumpStatus()
|
long |
elapsed(Timestamp key)
How long am i running. |
org.xmlBlaster.util.Container[] |
getContainers()
|
int |
getSize()
Get number of current used timers. |
long |
getTimeout(Timestamp key)
Access the end of life span. |
boolean |
isExpired(Timestamp key)
Is this handle expired? |
static void |
main(java.lang.String[] args)
Method for testing only. |
Timestamp |
refreshTimeoutListener(Timestamp key,
long delay)
Refresh a listener before the timeout happened. |
void |
removeAll()
Reset all pending timeouts. |
void |
removeTimeoutListener(Timestamp key)
Remove a listener before the timeout happened. |
void |
run()
Starts the Timeout manager thread. |
void |
shutdown()
Reset and stop the Timeout manager thread. |
long |
spanToTimeout(Timestamp key)
How long to my timeout. |
java.lang.String |
toString()
|
Methods inherited from class java.lang.Thread |
---|
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, yield |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public TimeoutPooled()
public TimeoutPooled(java.lang.String name)
name
- The name of the threadpublic TimeoutPooled(java.lang.String name, boolean useWeakReference)
name
- The name of the threaduseWeakReference
- If true the reference on your I_Timeout implementation is only
weak referenced and may be garbage collected even that we hold a
weak reference.public TimeoutPooled(java.lang.String name, boolean useWeakReference, java.util.concurrent.ThreadPoolExecutor threadPool)
Method Detail |
---|
public int getSize()
I_TimeoutManager
getSize
in interface I_TimeoutManager
public java.lang.String toString()
toString
in class java.lang.Thread
public java.lang.String dumpStatus()
dumpStatus
in interface I_TimeoutManager
public org.xmlBlaster.util.Container[] getContainers()
public void run()
run
in interface java.lang.Runnable
run
in class java.lang.Thread
public final Timestamp addTimeoutListener(I_Timeout listener, long delay, java.lang.Object userData)
I_TimeoutManager
addTimeoutListener
in interface I_TimeoutManager
listener
- Your callback handle (you need to implement this interface).delay
- The timeout in milliseconds. You can pass 0L and the Timeout
thread will fire immediately, this can be useful to dispatch a
task to the timeoutlisteneruserData
- Some arbitrary data you supply, it will be routed back to you
when the timeout occurs through method I_Timeout.timeout().
public final Timestamp refreshTimeoutListener(Timestamp key, long delay) throws XmlBlasterException
I_TimeoutManager
timeout.removeTimeoutListener(timeoutHandle); timeoutHandle = timeout.addTimeoutListener(this, "1000L", "UserData");
refreshTimeoutListener
in interface I_TimeoutManager
key
- The timeout handle you received by a previous
addTimeoutListener() call.delay
- The timeout in milliseconds measured from now.
XmlBlasterException
- if key is null or unknown or invalid because timer elapsed
alreadypublic final Timestamp addOrRefreshTimeoutListener(I_Timeout listener, long delay, java.lang.Object userData, Timestamp key) throws XmlBlasterException
I_TimeoutManager
addOrRefreshTimeoutListener
in interface I_TimeoutManager
XmlBlasterException
public final void removeTimeoutListener(Timestamp key)
I_TimeoutManager
removeTimeoutListener
in interface I_TimeoutManager
key
- The timeout handle you received by a previous
addTimeoutListener() call.public final boolean isExpired(Timestamp key)
I_TimeoutManager
isExpired
in interface I_TimeoutManager
key
- The timeout handle you received by a previous
addTimeoutListener() callpublic final long spanToTimeout(Timestamp key)
I_TimeoutManager
spanToTimeout
in interface I_TimeoutManager
key
- The timeout handle you received by a previous
addTimeoutListener() call.
public final long elapsed(Timestamp key)
I_TimeoutManager
elapsed
in interface I_TimeoutManager
key
- The timeout handle you received by a previous
addTimeoutListener() call.
public final long getTimeout(Timestamp key)
I_TimeoutManager
getTimeout
in interface I_TimeoutManager
key
- The timeout handle you received by a previous
addTimeoutListener() call.
public final void removeAll()
I_TimeoutManager
removeAll
in interface I_TimeoutManager
public void shutdown()
I_TimeoutManager
shutdown
in interface I_TimeoutManager
public static void main(java.lang.String[] args) throws java.lang.Exception
java.lang.Exception
|
xmlBlaster 2.2.0 client API | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |