Issue #5083 - Convert synchronized usages to AutoLock.
Updates after review. Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
This commit is contained in:
parent
089e51f0bc
commit
edd6ca2ee1
|
@ -29,8 +29,10 @@ import java.time.temporal.ChronoUnit;
|
|||
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 @@ import org.eclipse.jetty.util.thread.AutoLock;
|
|||
*/
|
||||
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 class RolloverFileOutputStream extends OutputStream
|
|||
// 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 class RolloverFileOutputStream extends OutputStream
|
|||
|
||||
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,15 +397,12 @@ public class RolloverFileOutputStream extends OutputStream
|
|||
}
|
||||
}
|
||||
|
||||
RollTask rollTask = _rollTask;
|
||||
ScheduledFuture<?> rollTask = _rollTask;
|
||||
if (rollTask != null)
|
||||
rollTask.cancel();
|
||||
rollTask.cancel(false);
|
||||
}
|
||||
|
||||
private class RollTask extends TimerTask
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
private void rollOver()
|
||||
{
|
||||
try
|
||||
{
|
||||
|
@ -417,5 +417,4 @@ public class RolloverFileOutputStream extends OutputStream
|
|||
t.printStackTrace(System.err);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue