diff --git a/src/site/xdoc/manual/thread-context.xml b/src/site/xdoc/manual/thread-context.xml index 66ad61c..87c718d 100644 --- a/src/site/xdoc/manual/thread-context.xml +++ b/src/site/xdoc/manual/thread-context.xml @@ -97,6 +97,37 @@ logger.debug("Message 2"); . . ThreadContext.clear(); + +
When placing items on the stack or map, it's necessary to remove then again when appropriate. To assist with + this, the CloseableThreadContext implements the AutoCloseable + interface. This allows items to be pushed to the stack or put in the map, and removed when the close() method is called - + or automatically as part of a try-with-resources. For example, to temporarily push something on to the stack and then remove it; +
+// Add to the ThreadContext stack for this try block only;
+try (final CloseableThreadContext ctc = CloseableThreadContext.push(UUID.randomUUID().toString())) {
+
+ logger.debug("Message 1");
+.
+.
+ logger.debug("Message 2");
+.
+.
+}
+ or to temporarily put something in the map;
+
+// Add to the ThreadContext map for this try block only;
+try (final CloseableThreadContext ctc = CloseableThreadContext.put("id", UUID.randomUUID().toString(), "loginId", session.getAttribute("loginId"))) {
+
+ logger.debug("Message 1");
+.
+.
+ logger.debug("Message 2");
+.
+.
+}
+
+ The Stack and the Map are managed per thread and are based on ThreadLocal by default. The Map can be configured to use an