use of java.util.TimerTask in project sling by apache.
the class JobQueueImpl method reschedule.
private void reschedule(final JobHandler handler) {
// we delay putting back the job until the retry delay is over
final long delay = this.getRetryDelay(handler);
if (delay > 0) {
if (this.configuration.getType() == Type.ORDERED) {
this.cache.setIsBlocked(true);
}
handler.addToRetryList();
final Date fireDate = new Date();
fireDate.setTime(System.currentTimeMillis() + delay);
if (this.configuration.getType() == Type.ORDERED) {
this.isSleepingUntil = fireDate.getTime();
}
final Runnable t = new Runnable() {
@Override
public void run() {
try {
if (handler.removeFromRetryList()) {
requeue(handler);
}
waitCounter.decrementAndGet();
} finally {
if (configuration.getType() == Type.ORDERED) {
isSleepingUntil = -1;
cache.setIsBlocked(false);
startJobs();
}
}
}
};
this.waitCounter.incrementAndGet();
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
t.run();
}
}, delay);
} else {
// put directly into queue
this.requeue(handler);
}
}
use of java.util.TimerTask in project socket.io-client-java by socketio.
the class ConnectionTest method reconnectEventFireInSocket.
@Test(timeout = TIMEOUT)
public void reconnectEventFireInSocket() throws URISyntaxException, InterruptedException {
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
socket = client();
socket.on(Socket.EVENT_RECONNECT, new Emitter.Listener() {
@Override
public void call(Object... objects) {
values.offer("done");
}
});
socket.open();
new Timer().schedule(new TimerTask() {
@Override
public void run() {
socket.io().engine.close();
}
}, 500);
values.take();
socket.close();
}
use of java.util.TimerTask in project robovm by robovm.
the class TimerTest method test_scheduleAtFixedRateLjava_util_TimerTaskLjava_util_DateJ.
/**
* java.util.Timer#scheduleAtFixedRate(java.util.TimerTask,
* java.util.Date, long)
*/
public void test_scheduleAtFixedRateLjava_util_TimerTaskLjava_util_DateJ() throws Exception {
Timer t = null;
try {
// Ensure a Timer throws an IllegalStateException after cancelled
t = new Timer();
TimerTestTask testTask = new TimerTestTask();
t.cancel();
Date d = new Date(System.currentTimeMillis() + 100);
try {
t.scheduleAtFixedRate(testTask, d, 100);
fail("scheduleAtFixedRate after Timer.cancel() should throw exception");
} catch (IllegalStateException expected) {
}
// Ensure a Timer throws an IllegalArgumentException if delay is
// negative
t = new Timer();
testTask = new TimerTestTask();
d = new Date(-100);
try {
t.scheduleAtFixedRate(testTask, d, 100);
fail("scheduleAtFixedRate with negative Date should throw IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
t.cancel();
// Ensure a Timer throws an IllegalArgumentException if period is
// negative
t = new Timer();
testTask = new TimerTestTask();
try {
t.scheduleAtFixedRate(testTask, d, -100);
fail("scheduleAtFixedRate with negative period should throw IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
t.cancel();
// Ensure a Timer throws an NullPointerException if date is Null
t = new Timer();
testTask = new TimerTestTask();
try {
t.scheduleAtFixedRate(testTask, null, 100);
fail("scheduleAtFixedRate with null date should throw NullPointerException");
} catch (NullPointerException expected) {
}
t.cancel();
// Ensure proper sequence of exceptions
t = new Timer();
d = new Date(-100);
try {
t.scheduleAtFixedRate(null, d, 10);
fail("Scheduling a null task with negative date should throw IllegalArgumentException first");
} catch (IllegalArgumentException expected) {
}
t.cancel();
// Ensure proper sequence of exceptions
t = new Timer();
try {
t.scheduleAtFixedRate(null, null, -10);
fail("Scheduling a null task & null date & negative period should throw IllegalArgumentException first");
} catch (IllegalArgumentException expected) {
}
t.cancel();
// Ensure a task is run at least twice
t = new Timer();
testTask = new TimerTestTask();
d = new Date(System.currentTimeMillis() + 100);
t.scheduleAtFixedRate(testTask, d, 100);
Thread.sleep(400);
assertTrue("TimerTask.run() method should have been called at least twice (" + testTask.wasRun() + ")", testTask.wasRun() >= 2);
t.cancel();
class SlowThenFastTask extends TimerTask {
int wasRun = 0;
long startedAt;
long lastDelta;
public void run() {
if (wasRun == 0)
startedAt = System.currentTimeMillis();
lastDelta = System.currentTimeMillis() - (startedAt + (100 * wasRun));
wasRun++;
if (wasRun == 2) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
public long lastDelta() {
return lastDelta;
}
public int wasRun() {
return wasRun;
}
}
// Ensure multiple tasks are run
t = new Timer();
SlowThenFastTask slowThenFastTask = new SlowThenFastTask();
d = new Date(System.currentTimeMillis() + 100);
// at least 9 times even when asleep
t.scheduleAtFixedRate(slowThenFastTask, d, 100);
Thread.sleep(1000);
long lastDelta = slowThenFastTask.lastDelta();
assertTrue("Fixed Rate Schedule should catch up, but is off by " + lastDelta + " ms", lastDelta < 300);
t.cancel();
} finally {
if (t != null)
t.cancel();
}
}
use of java.util.TimerTask in project robovm by robovm.
the class TimerTest method testThrowingTaskKillsTimerThread.
/**
* We used to swallow RuntimeExceptions thrown by tasks. Instead, we need to
* let those exceptions bubble up, where they will both notify the thread's
* uncaught exception handler and terminate the timer's thread.
*/
public void testThrowingTaskKillsTimerThread() throws Exception {
final AtomicReference<Thread> threadRef = new AtomicReference<Thread>();
new Timer().schedule(new TimerTask() {
@Override
public void run() {
Thread.currentThread().setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
public void uncaughtException(Thread thread, Throwable ex) {
}
});
threadRef.set(Thread.currentThread());
throw new RuntimeException("task failure!");
}
}, 1);
Thread.sleep(400);
Thread timerThread = threadRef.get();
assertFalse(timerThread.isAlive());
}
use of java.util.TimerTask in project glitch-hq-android by tinyspeck.
the class UnlearnFragment method InitUpdateSkillRemainingTimer.
private void InitUpdateSkillRemainingTimer() {
if (m_RemainingTimer != null)
m_RemainingTimer.cancel();
m_RemainingTimer = new Timer();
m_RemainingTimer.scheduleAtFixedRate(new TimerTask() {
public void run() {
getActivity().runOnUiThread(new Runnable() {
public void run() {
if (m_unlearningList.size() > 0)
setUnlearningSkill();
else if (m_learningList.size() > 0)
setLearningSkill();
}
});
}
}, 1000, 1000);
}
Aggregations