394215 - Scheduled tasks throwing exceptions kill java.util.Timer thread.

Wrapping the the task run() method into a try/catch to avoid that the Timer thread dies.
This commit is contained in:
Simone Bordet 2012-11-13 19:29:58 +01:00
parent a37f9c813d
commit c07bab8a05
2 changed files with 78 additions and 36 deletions

View File

@ -24,9 +24,13 @@ import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.util.component.AbstractLifeCycle;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
public class TimerScheduler extends AbstractLifeCycle implements Scheduler
{
private static final Logger LOG = Log.getLogger(TimerScheduler.class);
/*
* This class uses the Timer class rather than an ScheduledExecutionService because
* it uses the same algorithm internally and the signature is cheaper to use as there are no
@ -84,9 +88,16 @@ public class TimerScheduler extends AbstractLifeCycle implements Scheduler
@Override
public void run()
{
try
{
_task.run();
}
catch (Throwable x)
{
LOG.debug("Exception while executing task "+_task,x);
}
}
@Override
public String toString()

View File

@ -21,6 +21,7 @@ package org.eclipse.jetty.util.thread;
import java.util.Arrays;
import java.util.Collection;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
@ -174,6 +175,36 @@ public class SchedulerTest
Assert.assertEquals(0,executed.get());
}
@Test
public void testTaskThrowsException() throws Exception
{
long delay = 500;
Scheduler.Task task=_scheduler.schedule(new Runnable()
{
@Override
public void run()
{
throw new RuntimeException();
}
}, delay, TimeUnit.MILLISECONDS);
TimeUnit.MILLISECONDS.sleep(2 * delay);
// Check whether after a task throwing an exception, the scheduler is still working
final CountDownLatch latch = new CountDownLatch(1);
_scheduler.schedule(new Runnable()
{
@Override
public void run()
{
latch.countDown();
}
}, delay, TimeUnit.MILLISECONDS);
Assert.assertTrue(latch.await(2 * delay, TimeUnit.MILLISECONDS));
}
@Test
@Slow
public void testManySchedulesAndCancels() throws Exception