Search in sources :

Example 1 with ConfigurationScheduler

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();
    }
}
Also used : Path(java.nio.file.Path) FileOutputStream(java.io.FileOutputStream) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ConfigurationScheduler(org.apache.logging.log4j.core.config.ConfigurationScheduler) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 2 with ConfigurationScheduler

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());
}
Also used : ConfigurationScheduler(org.apache.logging.log4j.core.config.ConfigurationScheduler) Date(java.util.Date)

Example 3 with ConfigurationScheduler

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();
    }
}
Also used : Path(java.nio.file.Path) FileOutputStream(java.io.FileOutputStream) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ConfigurationScheduler(org.apache.logging.log4j.core.config.ConfigurationScheduler) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 4 with ConfigurationScheduler

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();
    }
}
Also used : Path(java.nio.file.Path) FileOutputStream(java.io.FileOutputStream) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ConfigurationScheduler(org.apache.logging.log4j.core.config.ConfigurationScheduler) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 5 with ConfigurationScheduler

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();
    }
}
Also used : HttpWatcher(org.apache.logging.log4j.core.config.HttpWatcher) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) Configuration(org.apache.logging.log4j.core.config.Configuration) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) StubMapping(com.github.tomakehurst.wiremock.stubbing.StubMapping) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ConfigurationScheduler(org.apache.logging.log4j.core.config.ConfigurationScheduler) URL(java.net.URL) ConfigurationListener(org.apache.logging.log4j.core.config.ConfigurationListener) TimeZone(java.util.TimeZone) Test(org.junit.Test)

Aggregations

ConfigurationScheduler (org.apache.logging.log4j.core.config.ConfigurationScheduler)6 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)5 File (java.io.File)3 FileOutputStream (java.io.FileOutputStream)3 Path (java.nio.file.Path)3 Test (org.junit.jupiter.api.Test)3 StubMapping (com.github.tomakehurst.wiremock.stubbing.StubMapping)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 Calendar (java.util.Calendar)2 TimeZone (java.util.TimeZone)2 Configuration (org.apache.logging.log4j.core.config.Configuration)2 ConfigurationListener (org.apache.logging.log4j.core.config.ConfigurationListener)2 DefaultConfiguration (org.apache.logging.log4j.core.config.DefaultConfiguration)2 HttpWatcher (org.apache.logging.log4j.core.config.HttpWatcher)2 Test (org.junit.Test)2 Date (java.util.Date)1