diff --git a/.gitignore b/.gitignore index 4835f9fe..4542a690 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ -# Build / maven artifacts -*.class +# Build / Maven artifacts target/ pom.xml.versionsBackup @@ -11,10 +10,14 @@ pom.xml.versionsBackup *.war *.ear -# virtual machine crash logs, see https://www.java.com/en/download/help/error_hotspot.html +# Virtual machine crash logs, see https://www.java.com/en/download/help/error_hotspot.html hs_err_pid* # IntelliJ project files .idea/ *.iml +# Eclipse project files +.settings/ +.classpath +.project diff --git a/.mvn/javadoc/package-list b/.mvn/javadoc/package-list index 2e406bae..9cd6e9f5 100644 --- a/.mvn/javadoc/package-list +++ b/.mvn/javadoc/package-list @@ -10,6 +10,7 @@ nl.talsmasoftware.context.futures nl.talsmasoftware.context.metrics nl.talsmasoftware.context.locale nl.talsmasoftware.context.mdc +nl.talsmasoftware.context.slf4j.mdc nl.talsmasoftware.context.opentracing nl.talsmasoftware.context.servletrequest nl.talsmasoftware.context.springsecurity diff --git a/context-propagation/src/main/java/nl/talsmasoftware/context/ContextManager.java b/context-propagation/src/main/java/nl/talsmasoftware/context/ContextManager.java index c032e0fc..d6f52007 100644 --- a/context-propagation/src/main/java/nl/talsmasoftware/context/ContextManager.java +++ b/context-propagation/src/main/java/nl/talsmasoftware/context/ContextManager.java @@ -35,6 +35,7 @@ * and {@linkplain ContextManagers#onDeactivate(Class, Object, Object)} methods * to notify the appropriate context observers. * + * @param type of the context value * @author Sjoerd Talsma */ public interface ContextManager { @@ -46,7 +47,7 @@ public interface ContextManager { * * @param value The value to initialize a new context for. * @return The new active context containing the specified value - * which should be closed by the caller at the end of its lifecycle. + * which should be closed by the caller at the end of its lifecycle from the same thread. */ Context initializeNewContext(T value); diff --git a/context-propagation/src/main/java/nl/talsmasoftware/context/ContextSnapshot.java b/context-propagation/src/main/java/nl/talsmasoftware/context/ContextSnapshot.java index d0ff496d..98528516 100644 --- a/context-propagation/src/main/java/nl/talsmasoftware/context/ContextSnapshot.java +++ b/context-propagation/src/main/java/nl/talsmasoftware/context/ContextSnapshot.java @@ -49,7 +49,7 @@ public interface ContextSnapshot { * Using the {@linkplain nl.talsmasoftware.context.executors.ContextAwareExecutorService ContextAwareExecutorService} * is a safe way to propagate context snapshots without having to worry about closing them. * - * @return A new reactivationcontext with the snapshot values that will be valid until closed + * @return A new reactivation context with the snapshot values that will be valid until closed * (or new values are registered). */ Context reactivate(); diff --git a/context-propagation/src/main/java/nl/talsmasoftware/context/clearable/Clearable.java b/context-propagation/src/main/java/nl/talsmasoftware/context/clearable/Clearable.java index 754275fb..925cedad 100644 --- a/context-propagation/src/main/java/nl/talsmasoftware/context/clearable/Clearable.java +++ b/context-propagation/src/main/java/nl/talsmasoftware/context/clearable/Clearable.java @@ -16,7 +16,7 @@ package nl.talsmasoftware.context.clearable; /** - * Interface that marks an to be 'clearable'. + * Interface that marks an object to be 'clearable'. *

* Context managers that support clearing the active context for the current thread * should implement the {@link Clearable} interface. @@ -26,6 +26,7 @@ * This operation is intended to only be used when re-using threads (e.g. when returning them to a thread-pool). * * @author Sjoerd Talsma + * @see ClearableContextManager */ public interface Clearable { diff --git a/context-propagation/src/main/java/nl/talsmasoftware/context/delegation/CallMappingExecutorService.java b/context-propagation/src/main/java/nl/talsmasoftware/context/delegation/CallMappingExecutorService.java index 1473b4bf..fb7ac685 100644 --- a/context-propagation/src/main/java/nl/talsmasoftware/context/delegation/CallMappingExecutorService.java +++ b/context-propagation/src/main/java/nl/talsmasoftware/context/delegation/CallMappingExecutorService.java @@ -55,6 +55,7 @@ protected CallMappingExecutorService(ExecutorService delegate) { * @return The mapped callable object. * @see #map(Callable) */ + @Override protected final Callable wrap(Callable callable) { return map(callable); } diff --git a/context-propagation/src/main/java/nl/talsmasoftware/context/executors/ContextAwareExecutorService.java b/context-propagation/src/main/java/nl/talsmasoftware/context/executors/ContextAwareExecutorService.java index e399a706..f64dabed 100644 --- a/context-propagation/src/main/java/nl/talsmasoftware/context/executors/ContextAwareExecutorService.java +++ b/context-propagation/src/main/java/nl/talsmasoftware/context/executors/ContextAwareExecutorService.java @@ -39,7 +39,7 @@ * @author Sjoerd Talsma */ public class ContextAwareExecutorService extends CallMappingExecutorService { - private final Logger logger = Logger.getLogger(getClass().getName()); + private static final Logger LOGGER = Logger.getLogger(ContextAwareExecutorService.class.getName()); public ContextAwareExecutorService(ExecutorService delegate) { super(delegate); @@ -77,12 +77,12 @@ public V call() throws Exception { * @param context context to be closed * @param exception exception if any occurred */ - private void tryClose(Context context, Exception exception) throws Exception { + private static void tryClose(Context context, Exception exception) throws Exception { if (context != null) try { context.close(); } catch (RuntimeException closeEx) { if (exception != null) { - logger.log(Level.WARNING, "Exception closing context after failed operation: " + closeEx.getMessage(), closeEx); + LOGGER.log(Level.WARNING, "Exception closing context after failed operation: " + closeEx.getMessage(), closeEx); throw exception; } throw closeEx; diff --git a/context-propagation/src/main/java/nl/talsmasoftware/context/threadlocal/AbstractThreadLocalContext.java b/context-propagation/src/main/java/nl/talsmasoftware/context/threadlocal/AbstractThreadLocalContext.java index 9e95a170..ee41304a 100644 --- a/context-propagation/src/main/java/nl/talsmasoftware/context/threadlocal/AbstractThreadLocalContext.java +++ b/context-propagation/src/main/java/nl/talsmasoftware/context/threadlocal/AbstractThreadLocalContext.java @@ -34,6 +34,8 @@ * @author Sjoerd Talsma */ public abstract class AbstractThreadLocalContext implements Context { + private static final Logger LOGGER = Logger.getLogger(AbstractThreadLocalContext.class.getName()); + private static final AtomicBoolean DEPRECATED_CONSTRUCTOR_WARNING = new AtomicBoolean(true); /** * The constant of ThreadLocal context instances per subclass name so different types don't get mixed. @@ -43,7 +45,6 @@ public abstract class AbstractThreadLocalContext implements Context { @SuppressWarnings("unchecked") private final ThreadLocal> sharedThreadLocalContext = threadLocalInstanceOf((Class) getClass()); - private final Logger logger = Logger.getLogger(getClass().getName()); private final AtomicBoolean closed = new AtomicBoolean(false); private final Class> contextManagerType; @@ -73,7 +74,7 @@ public abstract class AbstractThreadLocalContext implements Context { @Deprecated protected AbstractThreadLocalContext(T newValue) { this(null, newValue); - logger.log(DEPRECATED_CONSTRUCTOR_WARNING.compareAndSet(true, false) ? Level.WARNING : Level.FINE, + LOGGER.log(DEPRECATED_CONSTRUCTOR_WARNING.compareAndSet(true, false) ? Level.WARNING : Level.FINE, "Initialized new {0} without context manager type. " + "This makes it impossible to register ContextObservers for it. " + "Please fix {1} by specifying a ContextManager type " + @@ -96,7 +97,7 @@ protected AbstractThreadLocalContext(Class> this.parentContext = sharedThreadLocalContext.get(); this.value = newValue; this.sharedThreadLocalContext.set(this); - logger.log(Level.FINEST, "Initialized new {0}.", this); + LOGGER.log(Level.FINEST, "Initialized new {0}.", this); ContextManagers.onActivate(contextManagerType, value, parentContext == null ? null : parentContext.getValue()); } @@ -147,7 +148,7 @@ public T getValue() { public void close() { final boolean observe = closed.compareAndSet(false, true); final Context restored = this.unwindIfNecessary(); // Remove this context created in the same thread. - logger.log(Level.FINEST, "Closed {0}.", this); + LOGGER.log(Level.FINEST, "Closed {0}.", this); if (observe) { ContextManagers.onDeactivate(contextManagerType, this.value, restored == null ? null : restored.getValue()); } @@ -159,6 +160,7 @@ public void close() { * * @return String representing this context class and either the current value or the fact that it was closed. */ + @Override public String toString() { return getClass().getSimpleName() + (isClosed() ? "{closed}" : "{value=" + value + '}'); } diff --git a/readme.md b/readme.md index 1b4489a8..89f0f326 100644 --- a/readme.md +++ b/readme.md @@ -81,7 +81,7 @@ private static final ExecutorService THREADPOOL = The following `ThreadLocal`-based contexts are currently supported out of the box by this context-propagation library: -- [Slf4J MDC (Mapped Diagnostic Context)][mdc propagation] +- [SLF4J MDC (Mapped Diagnostic Context)][slf4j mdc propagation] - [OpenTracing Span contexts][opentracing span propagation] - [Spring Security Context] - [Locale context][locale context] @@ -167,14 +167,13 @@ configure various timers in the global default metric registry of your applicati [servletrequest propagation]: servletrequest-propagation - [mdc propagation]: slf4j-propagation + [slf4j mdc propagation]: slf4j-propagation [locale context]: locale-context [spring security context]: spring-security-context [opentracing span propagation]: opentracing-span-propagation [context propagation metrics]: context-propagation-metrics [context propagation micrometer]: context-propagation-micrometer - [default constructor]: https://en.wikipedia.org/wiki/Nullary_constructor [micrometer]: https://micrometer.io - [ContextAwareExecutorService]: https://javadoc.io/page/nl.talsmasoftware.context/context-propagation/latest/nl/talsmasoftware/context/executors/ContextAwareExecutorService.html + [ContextAwareExecutorService]: https://javadoc.io/doc/nl.talsmasoftware.context/context-propagation/latest/nl/talsmasoftware/context/executors/ContextAwareExecutorService.html [ContextAwareCompletableFuture]: context-propagation-java8#contextawarecompletablefuture diff --git a/slf4j-propagation/README.md b/slf4j-propagation/README.md index 789073b7..a78c6a11 100644 --- a/slf4j-propagation/README.md +++ b/slf4j-propagation/README.md @@ -1,10 +1,10 @@ [![Maven Version][maven-img]][maven] -# Slf4J MDC propagation library +# SLF4J MDC propagation library Adding the `slf4j-propagation` jar to your classpath -is all that is needed to let the [Mapped Diagnostic Context (MDC)][MDC] -from the [Simple Logging Facade for Java (SLF4J)][Slf4J] +is all that is needed to let the [Mapped Diagnostic Context (MDC)][mdc] +from the [Simple Logging Facade for Java (SLF4J)][slf4j] be automatically included into the `ContextSnapshot`. ## How to use this library diff --git a/slf4j-propagation/src/main/java/nl/talsmasoftware/context/slf4j/mdc/Slf4jMdcManager.java b/slf4j-propagation/src/main/java/nl/talsmasoftware/context/slf4j/mdc/Slf4jMdcManager.java index dece2a97..c960acac 100644 --- a/slf4j-propagation/src/main/java/nl/talsmasoftware/context/slf4j/mdc/Slf4jMdcManager.java +++ b/slf4j-propagation/src/main/java/nl/talsmasoftware/context/slf4j/mdc/Slf4jMdcManager.java @@ -25,9 +25,9 @@ import java.util.concurrent.atomic.AtomicBoolean; /** - * Manager to propagate the {@link MDC} content from one thread to another. + * Manager to propagate the SLF4J {@link MDC} content from one thread to another. *

- * As {@link MDC} already manages its own threadlocal state, + * As {@link MDC} already manages its own thread-local state, * getting the active context is 100% delegated to the MDC.
* This means that closing the resulting context from {@link #getActiveContext()} will have no side-effects, * as it is not ours to manage. diff --git a/slf4j-propagation/src/main/java/nl/talsmasoftware/context/slf4j/mdc/package-info.java b/slf4j-propagation/src/main/java/nl/talsmasoftware/context/slf4j/mdc/package-info.java index 718fd566..f4807094 100644 --- a/slf4j-propagation/src/main/java/nl/talsmasoftware/context/slf4j/mdc/package-info.java +++ b/slf4j-propagation/src/main/java/nl/talsmasoftware/context/slf4j/mdc/package-info.java @@ -14,7 +14,7 @@ * limitations under the License. */ /** - * Propagate the {@link org.slf4j.MDC Slf4J MDC} content from one thread to another. + * Propagate the {@linkplain org.slf4j.MDC SLF4J MDC} content from one thread to another. * *

* This context manager maintains no threadlocal state of its own, diff --git a/slf4j-propagation/src/main/resources/META-INF/services/nl.talsmasoftware.context.ContextManager b/slf4j-propagation/src/main/resources/META-INF/services/nl.talsmasoftware.context.ContextManager index 55d1c463..feb52ec1 100644 --- a/slf4j-propagation/src/main/resources/META-INF/services/nl.talsmasoftware.context.ContextManager +++ b/slf4j-propagation/src/main/resources/META-INF/services/nl.talsmasoftware.context.ContextManager @@ -1 +1 @@ -nl.talsmasoftware.context.slf4j.mdc.Slf4jMdcManager \ No newline at end of file +nl.talsmasoftware.context.slf4j.mdc.Slf4jMdcManager diff --git a/slf4j-propagation/src/test/java/nl/talsmasoftware/context/slf4j/mdc/Slf4jMdcManagerTest.java b/slf4j-propagation/src/test/java/nl/talsmasoftware/context/slf4j/mdc/Slf4jMdcManagerTest.java index 406617d6..aaa3bf03 100644 --- a/slf4j-propagation/src/test/java/nl/talsmasoftware/context/slf4j/mdc/Slf4jMdcManagerTest.java +++ b/slf4j-propagation/src/test/java/nl/talsmasoftware/context/slf4j/mdc/Slf4jMdcManagerTest.java @@ -52,7 +52,6 @@ public void setupThreadpool() { @AfterEach public void shutdownThreadpool() { threadpool.shutdown(); - threadpool = null; } @BeforeEach @@ -112,7 +111,6 @@ public void testSlf4jMdcContextToString() { @Test public void testClearActiveContexts() { - Slf4jMdcManager mgr = new Slf4jMdcManager(); MDC.put("dummy", "value"); // Test no-op for MdcManager ContextManagers.clearActiveContexts();