Java Code Examples for org.apache.logging.log4j.ThreadContext#getContext()

The following examples show how to use org.apache.logging.log4j.ThreadContext#getContext() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ThreadContextUtilityClass.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
public static void testGetContextReturnsMutableCopy() {
    ThreadContext.clearMap();
    final Map<String, String> map1 = ThreadContext.getContext();
    assertTrue(map1.isEmpty());
    map1.put("K", "val"); // no error
    assertEquals("val", map1.get("K"));

    // adding to copy does not affect thread context map
    assertTrue(ThreadContext.getContext().isEmpty());

    ThreadContext.put("key", "val2");
    final Map<String, String> map2 = ThreadContext.getContext();
    assertEquals(1, map2.size());
    assertEquals("val2", map2.get("key"));
    map2.put("K", "val"); // no error
    assertEquals("val", map2.get("K"));

    // first copy is not affected
    assertNotSame(map1, map2);
    assertEquals(1, map1.size());
}
 
Example 2
Source File: ThreadContextUtils.java    From zstack with Apache License 2.0 5 votes vote down vote up
public static Runnable saveThreadContext() {
    ThreadContextMapSaved savedThread = new ThreadContextMapSaved();
    savedThread.contextMap = ThreadContext.getContext();
    savedThread.contextStack = ThreadContext.cloneStack();

    return () -> {
        ThreadContext.clearAll();
        ThreadContext.putAll(savedThread.contextMap);
        ThreadContext.setStack(savedThread.contextStack.asList());
    };
}
 
Example 3
Source File: FDBDatabaseRunnerTest.java    From fdb-record-layer with Apache License 2.0 4 votes vote down vote up
@Test
void testRestoreMdc() {
    Executor oldExecutor = FDBDatabaseFactory.instance().getExecutor();
    try {
        ThreadContext.clearAll();
        ThreadContext.put("outer", "Echidna");
        final Map<String, String> outer = ThreadContext.getContext();
        final ImmutableMap<String, String> restored = ImmutableMap.of("restored", "Platypus");

        FDBDatabaseFactory.instance().setExecutor(new ContextRestoringExecutor(
                new ForkJoinPool(2), ImmutableMap.of("executor", "Water Bear")));
        AtomicInteger attempts = new AtomicInteger(0);
        final FDBDatabaseRunner runner = new FDBDatabaseRunnerImpl(database, null, restored);
        List<Map<String, String>> threadContexts = new Vector<>();
        Consumer<String> saveThreadContext =
                name -> threadContexts.add(threadContextPlus(name, attempts.get(), ThreadContext.getContext()));
        final String runnerRunAsyncName = "runner runAsync";
        final String supplyAsyncName = "supplyAsync";
        final String handleName = "handle";

        // Delay starting the future until all callbacks have been set up so that the handle lambda
        // runs in the context-restoring executor.
        CompletableFuture<Void> signal = new CompletableFuture<>();
        CompletableFuture<?> task = runner.runAsync(recordContext -> {
            saveThreadContext.accept(runnerRunAsyncName);
            return signal.thenCompose(vignore -> CompletableFuture.supplyAsync(() -> {
                saveThreadContext.accept(supplyAsyncName);
                if (attempts.getAndIncrement() == 0) {
                    throw new RecordCoreRetriableTransactionException("Retriable and lessener",
                            new FDBException("not_committed", 1020));
                } else {
                    return null;
                }
            }, recordContext.getExecutor()));
        }).handle((result, exception) -> {
            saveThreadContext.accept(handleName);
            return exception;

        });
        signal.complete(null);
        assertNull(task.join());
        List<Map<String, String>> expected = ImmutableList.of(
                // first attempt:
                // it is known behavior that the first will be run in the current context
                threadContextPlus(runnerRunAsyncName, 0, outer),
                threadContextPlus(supplyAsyncName, 0, restored),
                // second attempt
                // the code that creates the future, should now have the correct MDC
                threadContextPlus(runnerRunAsyncName, 1, restored),
                threadContextPlus(supplyAsyncName, 1, restored),
                // handle
                // this should also have the correct MDC
                threadContextPlus(handleName, 2, restored));
        assertEquals(expected, threadContexts);
        assertEquals(outer, ThreadContext.getContext());
    } finally {
        FDBDatabaseFactory.instance().setExecutor(oldExecutor);
    }

}
 
Example 4
Source File: Log4jMDCAdapter.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Override
public Map<String, String> getCopyOfContextMap() {
    return ThreadContext.getContext();
}
 
Example 5
Source File: Log4jMDCAdapter.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Override
public Map<String, String> getCopyOfContextMap() {
    return ThreadContext.getContext();
}