Skip to content

Commit

Permalink
Issue #5083 - Convert synchronized usages to AutoLock.
Browse files Browse the repository at this point in the history
Updates after review.

Signed-off-by: Simone Bordet <[email protected]>
  • Loading branch information
sbordet committed Aug 4, 2020
1 parent 089e51f commit edd6ca2
Showing 1 changed file with 29 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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",
Expand Down Expand Up @@ -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);
}
Expand All @@ -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()
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit edd6ca2

Please sign in to comment.