cancel interrupt timer for LiveLeaderbaordUpdater appropriately also in case of exceptions

This commit is contained in:
Axel Uhl committed 2012-07-24 00:11:55 +02:00
1 parent e9464b05d4
commit 42c510c729
1 file changed
+14 -3
@@ -188,11 +188,13 @@ public class LiveLeaderboardUpdater implements Runnable {
@Override @Override
public void run() { public void run() {
assert running; assert running;
Timer interruptTimer = null;
TimerTask interruptTask = null;
try { try {
logger.info("Starting " + LiveLeaderboardUpdater.class.getSimpleName() + " thread for leaderboard " logger.info("Starting " + LiveLeaderboardUpdater.class.getSimpleName() + " thread for leaderboard "
+ leaderboardName); + leaderboardName);
interruptTimer = new Timer("Interrupt timer for "+LiveLeaderboardUpdater.class.getSimpleName()+" for leaderboard "+leaderboardName);
// interrupt the current thread if not producing a single result within the overall timeout // interrupt the current thread if not producing a single result within the overall timeout
Timer interruptTimer = new Timer("Interrupt timer for "+LiveLeaderboardUpdater.class.getSimpleName()+" for leaderboard "+leaderboardName);
while (true) { while (true) {
MillisecondsTimePoint now = MillisecondsTimePoint.now(); MillisecondsTimePoint now = MillisecondsTimePoint.now();
TimePoint timePoint = now.minus(getLeaderboard().getDelayToLiveInMillis()); TimePoint timePoint = now.minus(getLeaderboard().getDelayToLiveInMillis());
@@ -206,7 +208,7 @@ public class LiveLeaderboardUpdater implements Runnable {
TimePoint timeLastUpdateWasStarted = now; TimePoint timeLastUpdateWasStarted = now;
try { try {
final Set<String> namesOfRaceColumnsForWhichToLoadLegDetails = getColumnNamesForWhichToFetchDetails(timePoint); final Set<String> namesOfRaceColumnsForWhichToLoadLegDetails = getColumnNamesForWhichToFetchDetails(timePoint);
TimerTask interruptTask = new TimerTask() { interruptTask = new TimerTask() {
@Override @Override
public void run() { public void run() {
synchronized (LiveLeaderboardUpdater.this) { synchronized (LiveLeaderboardUpdater.this) {
@@ -221,7 +223,6 @@ public class LiveLeaderboardUpdater implements Runnable {
interruptTimer.schedule(interruptTask, UPDATE_TIMEOUT_IN_MILLIS); interruptTimer.schedule(interruptTask, UPDATE_TIMEOUT_IN_MILLIS);
LeaderboardDTO newCacheValue = sailingService.computeLeaderboardByName(leaderboardName, timePoint, LeaderboardDTO newCacheValue = sailingService.computeLeaderboardByName(leaderboardName, timePoint,
namesOfRaceColumnsForWhichToLoadLegDetails, /* waitForLatestAnalyses */false); namesOfRaceColumnsForWhichToLoadLegDetails, /* waitForLatestAnalyses */false);
interruptTask.cancel();
updateCacheContents(namesOfRaceColumnsForWhichToLoadLegDetails, newCacheValue); updateCacheContents(namesOfRaceColumnsForWhichToLoadLegDetails, newCacheValue);
} catch (NoWindException e) { } catch (NoWindException e) {
logger.info("Unable to update cached leaderboard results for leaderboard " + leaderboardName + ": " logger.info("Unable to update cached leaderboard results for leaderboard " + leaderboardName + ": "
@@ -233,6 +234,9 @@ public class LiveLeaderboardUpdater implements Runnable {
logger.throwing(LiveLeaderboardUpdater.class.getName(), "run", e1); logger.throwing(LiveLeaderboardUpdater.class.getName(), "run", e1);
} }
} }
if (interruptTask != null) {
interruptTask.cancel();
}
TimePoint computeLeaderboardByNameFinishedAt = MillisecondsTimePoint.now(); TimePoint computeLeaderboardByNameFinishedAt = MillisecondsTimePoint.now();
long millisToSleep = MINIMUM_TIME_BETWEEN_UPDATES long millisToSleep = MINIMUM_TIME_BETWEEN_UPDATES
- (computeLeaderboardByNameFinishedAt.asMillis() - timeLastUpdateWasStarted.asMillis()); - (computeLeaderboardByNameFinishedAt.asMillis() - timeLastUpdateWasStarted.asMillis());
@@ -250,6 +254,13 @@ public class LiveLeaderboardUpdater implements Runnable {
running = false; running = false;
logger.info("exception in "+LiveLeaderboardUpdater.class.getName()+".run(): "+t.getMessage()); logger.info("exception in "+LiveLeaderboardUpdater.class.getName()+".run(): "+t.getMessage());
logger.throwing(LiveLeaderboardUpdater.class.getName(), "run", t); logger.throwing(LiveLeaderboardUpdater.class.getName(), "run", t);
} finally {
if (interruptTask != null) {
interruptTask.cancel();
}
if (interruptTimer != null) {
interruptTimer.cancel();
}
} }
} }