ppkarwasz commented on code in PR #2442:
URL: https://github.com/apache/logging-log4j2/pull/2442#discussion_r1562164031


##########
log4j-api/src/main/java/org/apache/logging/log4j/spi/ExtendedThreadContext.java:
##########
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.logging.log4j.spi;
+
+import org.apache.logging.log4j.util.ProviderUtil;
+
+/**
+ * Provides advanced access method to the {@link ThreadContextMap}.
+ * @since 2.24.0
+ */
+public final class ExtendedThreadContext {
+
+    private ExtendedThreadContext() {}
+
+    /**
+     * Saves the context data of the current thread
+     * <p>
+     *     Thread-safety note: The returned object can safely be passed off to 
another thread: future changes in the
+     *     underlying context data will not be reflected in the returned 
object.
+     * </p>
+     * @return An opaque representation of the context data.
+     * @see #restoreMap
+     */
+    public static Object saveMap() {
+        return getThreadContextMap().save();

Review Comment:
   The main application I had in mind for this method is 
[`ThreadLocalAccessor#getValue`](https://javadoc.io/doc/io.micrometer/context-propagation/latest/io/micrometer/context/ThreadLocalAccessor.html)
 used in Spring Reactor.
   
   When working with the reactive stack (cf. [Spring's 
documentation](https://spring.io/reactive)) the processing of a web request is 
represented as a pipeline of 
[`CoreSubscriber`](https://projectreactor.io/docs/core/release/api/reactor/core/CoreSubscriber.html)
 objects, where each subscriber processes the output of the previous stage. The 
`onX` methods may be called on any thread, so the context data is stored **in** 
the `CoreSubscriber` object. As far as I know they don't use `ThreadLocal`.
   
   The context propagation offered by Micrometer is also supposed to work in 
other stacks. For example the Netty-based [Spring Webflux 
WebClient](https://docs.spring.io/spring-framework/reference/web/webflux-webclient.html)
 is supposed to:
   
   * propagate the context data through Netty's outbound handlers,
   * store it in Netty's channel before the request hits the wire,
   * restore it when an answer is received,
   * propagate the context data through Netty's inbound handlers.
   
   As far as I understand the propagation is restricted to the bounds of the 
JVM, so the **opaque object** in the context is not a problem. This object 
serves a single purpose: supply the object to the `restoreMap` method of the 
`ThreadContextMap` that created it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@logging.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to