use of org.apache.logging.log4j.core.config.ConfigurationScheduler in project logging-log4j2 by apache.
the class WatchManagerTest method testWatchManagerReset.
@Test
public void testWatchManagerReset() throws Exception {
final ConfigurationScheduler scheduler = new ConfigurationScheduler();
scheduler.incrementScheduledItems();
final WatchManager watchManager = new WatchManager(scheduler);
watchManager.setIntervalSeconds(1);
scheduler.start();
watchManager.start();
try {
final File sourceFile = new File(originalFile);
Path source = Paths.get(sourceFile.toURI());
try (final FileOutputStream targetStream = new FileOutputStream(testFile)) {
Files.copy(source, targetStream);
}
final File updateFile = new File(newFile);
final File targetFile = new File(testFile);
final BlockingQueue<File> queue = new LinkedBlockingQueue<>();
watchManager.watchFile(targetFile, new TestWatcher(queue));
watchManager.stop();
Thread.sleep(1000);
source = Paths.get(updateFile.toURI());
Files.copy(source, Paths.get(targetFile.toURI()), StandardCopyOption.REPLACE_EXISTING);
watchManager.reset();
watchManager.start();
Thread.sleep(1000);
final File f = queue.poll(1, TimeUnit.SECONDS);
assertNull(f, "File change detected");
} finally {
watchManager.stop();
scheduler.stop();
}
}
use of org.apache.logging.log4j.core.config.ConfigurationScheduler in project logging-log4j2 by apache.
the class CronTriggeringPolicy method initialize.
/**
* Initializes the policy.
*
* @param aManager
* The RollingFileManager.
*/
@Override
public void initialize(final RollingFileManager aManager) {
this.manager = aManager;
final Date now = new Date();
final Date lastRollForFile = cronExpression.getPrevFireTime(new Date(this.manager.getFileTime()));
final Date lastRegularRoll = cronExpression.getPrevFireTime(new Date());
aManager.getPatternProcessor().setCurrentFileTime(lastRegularRoll.getTime());
LOGGER.debug("LastRollForFile {}, LastRegularRole {}", lastRollForFile, lastRegularRoll);
aManager.getPatternProcessor().setPrevFileTime(lastRegularRoll.getTime());
aManager.getPatternProcessor().setTimeBased(true);
if (checkOnStartup && lastRollForFile != null && lastRegularRoll != null && lastRollForFile.before(lastRegularRoll)) {
lastRollDate = lastRollForFile;
rollover();
}
final ConfigurationScheduler scheduler = configuration.getScheduler();
if (!scheduler.isExecutorServiceSet()) {
// make sure we have a thread pool
scheduler.incrementScheduledItems();
}
if (!scheduler.isStarted()) {
scheduler.start();
}
lastRollDate = lastRegularRoll;
future = scheduler.scheduleWithCron(cronExpression, now, new CronTrigger());
LOGGER.debug(scheduler.toString());
}
use of org.apache.logging.log4j.core.config.ConfigurationScheduler in project logging-log4j2 by apache.
the class WatchManagerTest method testWatchManagerResetFile.
@Test
public void testWatchManagerResetFile() throws Exception {
final ConfigurationScheduler scheduler = new ConfigurationScheduler();
scheduler.incrementScheduledItems();
final WatchManager watchManager = new WatchManager(scheduler);
watchManager.setIntervalSeconds(1);
scheduler.start();
watchManager.start();
try {
final File sourceFile = new File(originalFile);
Path source = Paths.get(sourceFile.toURI());
try (final FileOutputStream targetStream = new FileOutputStream(testFile)) {
Files.copy(source, targetStream);
}
final File updateFile = new File(newFile);
final File targetFile = new File(testFile);
final BlockingQueue<File> queue = new LinkedBlockingQueue<>();
watchManager.watchFile(targetFile, new TestWatcher(queue));
watchManager.stop();
Thread.sleep(1000);
source = Paths.get(updateFile.toURI());
Files.copy(source, Paths.get(targetFile.toURI()), StandardCopyOption.REPLACE_EXISTING);
watchManager.reset(targetFile);
watchManager.start();
Thread.sleep(1000);
final File f = queue.poll(1, TimeUnit.SECONDS);
assertNull(f, "File change detected");
} finally {
watchManager.stop();
scheduler.stop();
}
}
use of org.apache.logging.log4j.core.config.ConfigurationScheduler in project logging-log4j2 by apache.
the class WatchManagerTest method testWatchManager.
@Test
public void testWatchManager() throws Exception {
final ConfigurationScheduler scheduler = new ConfigurationScheduler();
scheduler.incrementScheduledItems();
final WatchManager watchManager = new WatchManager(scheduler);
watchManager.setIntervalSeconds(1);
scheduler.start();
watchManager.start();
try {
final File sourceFile = new File(originalFile);
Path source = Paths.get(sourceFile.toURI());
try (final FileOutputStream targetStream = new FileOutputStream(testFile)) {
Files.copy(source, targetStream);
}
final File updateFile = new File(newFile);
final File targetFile = new File(testFile);
final BlockingQueue<File> queue = new LinkedBlockingQueue<>();
watchManager.watchFile(targetFile, new TestWatcher(queue));
Thread.sleep(1000);
source = Paths.get(updateFile.toURI());
Files.copy(source, Paths.get(targetFile.toURI()), StandardCopyOption.REPLACE_EXISTING);
Thread.sleep(1000);
final File f = queue.poll(1, TimeUnit.SECONDS);
assertNotNull(f, "File change not detected");
} finally {
watchManager.stop();
scheduler.stop();
}
}
use of org.apache.logging.log4j.core.config.ConfigurationScheduler in project logging-log4j2 by apache.
the class WatchHttpTest method testNotModified.
@Test
public void testNotModified() throws Exception {
BlockingQueue<String> queue = new LinkedBlockingQueue<>();
List<ConfigurationListener> listeners = new ArrayList<>();
listeners.add(new TestConfigurationListener(queue, "log4j-test2.xml"));
TimeZone timeZone = TimeZone.getTimeZone("UTC");
Calendar now = Calendar.getInstance(timeZone);
Calendar previous = now;
previous.add(Calendar.MINUTE, -5);
Configuration configuration = new DefaultConfiguration();
Assume.assumeTrue(!IS_WINDOWS || Boolean.getBoolean(FORCE_RUN_KEY));
URL url = new URL("http://localhost:" + wireMockRule.port() + "/log4j-test2.xml");
StubMapping stubMapping = stubFor(get(urlPathEqualTo("/log4j-test2.xml")).willReturn(aResponse().withBodyFile(file).withStatus(304).withHeader("Last-Modified", formatter.format(now) + " GMT").withHeader("Content-Type", XML)));
final ConfigurationScheduler scheduler = new ConfigurationScheduler();
scheduler.incrementScheduledItems();
final WatchManager watchManager = new WatchManager(scheduler);
watchManager.setIntervalSeconds(1);
scheduler.start();
watchManager.start();
try {
watchManager.watch(new Source(url.toURI()), new HttpWatcher(configuration, null, listeners, previous.getTimeInMillis()));
final String str = queue.poll(2, TimeUnit.SECONDS);
assertNull("File changed.", str);
} finally {
removeStub(stubMapping);
watchManager.stop();
scheduler.stop();
}
}
Aggregations