From edd6ca2ee1d70b3e5cc578103b8371c04f14f7b6 Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Tue, 4 Aug 2020 15:43:39 +0200 Subject: [PATCH] Issue #5083 - Convert synchronized usages to AutoLock. Updates after review. Signed-off-by: Simone Bordet --- .../jetty/util/RolloverFileOutputStream.java | 59 +++++++++---------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/RolloverFileOutputStream.java b/jetty-util/src/main/java/org/eclipse/jetty/util/RolloverFileOutputStream.java index ba150d0c11e8..f4ef64a25ac6 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/RolloverFileOutputStream.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/RolloverFileOutputStream.java @@ -29,8 +29,10 @@ import java.util.Date; import java.util.Locale; import java.util.TimeZone; -import java.util.Timer; -import java.util.TimerTask; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; import org.eclipse.jetty.util.thread.AutoLock; @@ -48,22 +50,26 @@ */ public class RolloverFileOutputStream extends OutputStream { - private static Timer __rollover; - static final String YYYY_MM_DD = "yyyy_mm_dd"; static final String ROLLOVER_FILE_DATE_FORMAT = "yyyy_MM_dd"; static final String ROLLOVER_FILE_BACKUP_FORMAT = "HHmmssSSS"; static final int ROLLOVER_FILE_RETAIN_DAYS = 31; + private static final ScheduledExecutorService __scheduler = Executors.newSingleThreadScheduledExecutor(job -> + { + Thread thread = new Thread(job, RolloverFileOutputStream.class.getName()); + thread.setDaemon(true); + return thread; + }); private final AutoLock _lock = new AutoLock(); private OutputStream _out; - private RollTask _rollTask; - private SimpleDateFormat _fileBackupFormat; - private SimpleDateFormat _fileDateFormat; + private ScheduledFuture _rollTask; + private final SimpleDateFormat _fileBackupFormat; + private final SimpleDateFormat _fileDateFormat; private String _filename; private File _file; - private boolean _append; - private int _retainDays; + private final boolean _append; + private final int _retainDays; /** * @param filename The filename must include the string "yyyy_mm_dd", @@ -178,8 +184,6 @@ public RolloverFileOutputStream(String filename, // Calculate Today's Midnight, based on Configured TimeZone (will be in past, even if by a few milliseconds) setFile(now); - __rollover = new Timer(RolloverFileOutputStream.class.getName(), true); - // This will schedule the rollover event to the next midnight scheduleNextRollover(now); } @@ -197,13 +201,12 @@ public static ZonedDateTime toMidnight(ZonedDateTime now) private void scheduleNextRollover(ZonedDateTime now) { - _rollTask = new RollTask(); // Get tomorrow's midnight based on Configured TimeZone ZonedDateTime midnight = toMidnight(now); // Schedule next rollover event to occur, based on local machine's Unix Epoch milliseconds long delay = midnight.toInstant().toEpochMilli() - now.toInstant().toEpochMilli(); - __rollover.schedule(_rollTask, delay); + _rollTask = __scheduler.schedule(this::rollOver, delay, TimeUnit.MILLISECONDS); } public String getFilename() @@ -394,28 +397,24 @@ public void close() throws IOException } } - RollTask rollTask = _rollTask; + ScheduledFuture rollTask = _rollTask; if (rollTask != null) - rollTask.cancel(); + rollTask.cancel(false); } - private class RollTask extends TimerTask + private void rollOver() { - @Override - public void run() + try { - try - { - ZonedDateTime now = ZonedDateTime.now(_fileDateFormat.getTimeZone().toZoneId()); - RolloverFileOutputStream.this.setFile(now); - RolloverFileOutputStream.this.removeOldFiles(now); - RolloverFileOutputStream.this.scheduleNextRollover(now); - } - catch (Throwable t) - { - // Cannot log this exception to a LOG, as RolloverFOS can be used by logging - t.printStackTrace(System.err); - } + ZonedDateTime now = ZonedDateTime.now(_fileDateFormat.getTimeZone().toZoneId()); + RolloverFileOutputStream.this.setFile(now); + RolloverFileOutputStream.this.removeOldFiles(now); + RolloverFileOutputStream.this.scheduleNextRollover(now); + } + catch (Throwable t) + { + // Cannot log this exception to a LOG, as RolloverFOS can be used by logging + t.printStackTrace(System.err); } } }