org.apache.logging.log4j.ThreadContext.putAll()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(5.2k)|赞(0)|评价(0)|浏览(133)

本文整理了Java中org.apache.logging.log4j.ThreadContext.putAll()方法的一些代码示例,展示了ThreadContext.putAll()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ThreadContext.putAll()方法的具体详情如下:
包路径:org.apache.logging.log4j.ThreadContext
类名称:ThreadContext
方法名:putAll

ThreadContext.putAll介绍

[英]Puts all given context map entries into the current thread's context map.

If the current thread does not have a context map it is created as a side effect.
[中]将所有给定的上下文映射条目放入当前线程的上下文映射中。
如果当前线程没有上下文映射,它将作为副作用创建。

代码示例

代码示例来源:origin: org.apache.logging.log4j/log4j-api

/**
 * Populates the Thread Context Map with the supplied key/value pairs. Any existing keys in the
 * {@link ThreadContext} will be replaced with the supplied values, and restored back to their original value when
 * the instance is closed.
 *
 * @param values The map of key/value pairs to be added
 * @return a new instance that will back out the changes when closed.
 * @since 2.8
 */
public Instance putAll(final Map<String, String> values) {
  final Map<String, String> currentValues = ThreadContext.getContext();
  ThreadContext.putAll(values);
  for (final String key : values.keySet()) {
    if (!originalValues.containsKey(key)) {
      originalValues.put(key, currentValues.get(key));
    }
  }
  return this;
}

代码示例来源:origin: org.apache.logging.log4j/log4j-api

/**
   * Restores the ThreadContext stack and map based on the values saved in the constructor.
   */
  public void restore() {
    if (restoreStack) {
      ThreadContext.setStack(immutableStack);
    }
    if (restoreContext) {
      // TODO LOG4J2-1517 Add ThreadContext.setContext(Map<String, String>)
      // Use:
      // ThreadContext.setContext(immutableContext);
      // Instead of:
      ThreadContext.clearMap();
      ThreadContext.putAll(immutableContext);
      //
      // or:
      // ThreadContext.clearMap();
      // for (Map.Entry<String, String> entry : immutableContext.entrySet()) {
      // ThreadContext.put(entry.getKey(), entry.getValue());
      // }
    }
  }
}

代码示例来源:origin: org.apache.logging.log4j/log4j-api

@Test
public void testPutAll() {
  ThreadContext.clearMap();
  //
  assertTrue(ThreadContext.isEmpty());
  assertFalse(ThreadContext.containsKey("key"));
  final int mapSize = 10;
  final Map<String, String> newMap = new HashMap<>(mapSize);
  for (int i = 1; i <= mapSize; i++) {
    newMap.put("key" + i, "value" + i);
  }
  ThreadContext.putAll(newMap);
  assertFalse(ThreadContext.isEmpty());
  for (int i = 1; i <= mapSize; i++) {
    assertTrue(ThreadContext.containsKey("key" + i));
    assertEquals("value" + i, ThreadContext.get("key" + i));
  }
}

代码示例来源:origin: zstackio/zstack

private void setThreadLoggingContext(Message msg) {
  ThreadContext.clearAll();
  if (msg instanceof APIMessage) {
    ThreadContext.put(Constants.THREAD_CONTEXT_API, msg.getId());
    ThreadContext.put(Constants.THREAD_CONTEXT_TASK_NAME, msg.getClass().getName());
  } else {
    Map<String, String> ctx = msg.getHeaderEntry(CloudBus.HEADER_TASK_CONTEXT);
    if (ctx != null) {
      ThreadContext.putAll(ctx);
    }
  }
  if (msg.getHeaders().containsKey(CloudBus.HEADER_TASK_STACK)) {
    List<String> taskStack = msg.getHeaderEntry(CloudBus.HEADER_TASK_STACK);
    ThreadContext.setStack(taskStack);
  }
}

代码示例来源:origin: zstackio/zstack

private void setThreadLoggingContext(Message msg) {
  ThreadContext.clearAll();
  if (msg instanceof APIMessage) {
    ThreadContext.put(Constants.THREAD_CONTEXT_API, msg.getId());
    ThreadContext.put(Constants.THREAD_CONTEXT_TASK_NAME, msg.getClass().getName());
  } else {
    Map<String, String> ctx = msg.getHeaderEntry(THREAD_CONTEXT);
    if (ctx != null) {
      ThreadContext.putAll(ctx);
    }
  }
  if (msg.getHeaders().containsKey(THREAD_CONTEXT_STACK)) {
    List<String> taskStack = msg.getHeaderEntry(THREAD_CONTEXT_STACK);
    ThreadContext.setStack(taskStack);
  }
  if (msg.getHeaders().containsKey(TASK_CONTEXT)) {
    TaskContext.setTaskContext(msg.getHeaderEntry(TASK_CONTEXT));
  }
}

代码示例来源:origin: zstackio/zstack

private 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());
  };
}

代码示例来源:origin: zstackio/zstack

private void setThreadContext(ProgressReportCmd cmd) {
  ThreadContext.clearAll();
  if (cmd.getThreadContextMap() != null) {
    ThreadContext.putAll(cmd.getThreadContextMap());
  }
  if (cmd.getThreadContextStack() != null) {
    ThreadContext.setStack(cmd.getThreadContextStack());
  }
}

代码示例来源:origin: org.talend.sdk.component/component-server

@Override
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
    throws IOException, ServletException {
  if (HttpServletRequest.class.isInstance(request)) {
    ThreadContext.putAll(createContext(HttpServletRequest.class.cast(request)));
  }
  chain.doFilter(request, response);
}

代码示例来源:origin: ops4j/org.ops4j.pax.logging

/**
 * Populates the Thread Context Map with the supplied key/value pairs. Any existing keys in the
 * {@link ThreadContext} will be replaced with the supplied values, and restored back to their original value when
 * the instance is closed.
 *
 * @param values The map of key/value pairs to be added
 * @return a new instance that will back out the changes when closed.
 * @since 2.8
 */
public Instance putAll(final Map<String, String> values) {
  final Map<String, String> currentValues = ThreadContext.getContext();
  ThreadContext.putAll(values);
  for (final String key : values.keySet()) {
    if (!originalValues.containsKey(key)) {
      originalValues.put(key, currentValues.get(key));
    }
  }
  return this;
}

相关文章