Search in sources :

Example 1 with HttpWatcher

use of org.apache.logging.log4j.core.config.HttpWatcher 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)

Example 2 with HttpWatcher

use of org.apache.logging.log4j.core.config.HttpWatcher in project logging-log4j2 by apache.

the class WatchHttpTest method testWatchManager.

@Test
public void testWatchManager() throws Exception {
    BlockingQueue<String> queue = new LinkedBlockingQueue<>();
    List<ConfigurationListener> listeners = new ArrayList<>();
    listeners.add(new TestConfigurationListener(queue, "log4j-test1.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-test1.xml");
    StubMapping stubMapping = stubFor(get(urlPathEqualTo("/log4j-test1.xml")).willReturn(aResponse().withBodyFile(file).withStatus(200).withHeader("Last-Modified", formatter.format(previous) + " 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);
        assertNotNull("File change not detected", 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

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 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)2 Configuration (org.apache.logging.log4j.core.config.Configuration)2 ConfigurationListener (org.apache.logging.log4j.core.config.ConfigurationListener)2 ConfigurationScheduler (org.apache.logging.log4j.core.config.ConfigurationScheduler)2 DefaultConfiguration (org.apache.logging.log4j.core.config.DefaultConfiguration)2 HttpWatcher (org.apache.logging.log4j.core.config.HttpWatcher)2 Test (org.junit.Test)2