From a3d3bd4e6c56146ea73481f459750c692ffa7952 Mon Sep 17 00:00:00 2001 From: Kabir Khan Date: Thu, 20 Aug 2020 15:50:26 +0200 Subject: [PATCH] Add JBoss Logging and log exceptions when calling the thread context providers --- core/pom.xml | 14 +++++++++ .../context/impl/ActiveContextState.java | 31 +++++++++++++++---- .../context/impl/CapturedContextState.java | 28 +++++++++++------ .../java/io/smallrye/context/impl/Util.java | 10 ++++++ .../SmallRyeContextPropagationLogger.java | 30 ++++++++++++++++++ pom.xml | 22 +++++++++++++ 6 files changed, 119 insertions(+), 16 deletions(-) create mode 100644 core/src/main/java/io/smallrye/context/impl/Util.java create mode 100644 core/src/main/java/io/smallrye/context/logging/SmallRyeContextPropagationLogger.java diff --git a/core/pom.xml b/core/pom.xml index 0c6ea2c9..f0b1a56c 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -33,6 +33,20 @@ microprofile-config-api ${version.microprofile.config} + + org.jboss.logging + jboss-logging + + + org.jboss.logging + jboss-logging-annotations + provided + + + org.jboss.logging + jboss-logging-processor + provided + org.jboss.threads jboss-threads diff --git a/core/src/main/java/io/smallrye/context/impl/ActiveContextState.java b/core/src/main/java/io/smallrye/context/impl/ActiveContextState.java index 8433b1e1..c765beac 100644 --- a/core/src/main/java/io/smallrye/context/impl/ActiveContextState.java +++ b/core/src/main/java/io/smallrye/context/impl/ActiveContextState.java @@ -1,5 +1,7 @@ package io.smallrye.context.impl; +import static io.smallrye.context.logging.SmallRyeContextPropagationLogger.ROOT_LOGGER; + import java.util.ArrayList; import java.util.List; @@ -13,16 +15,33 @@ public class ActiveContextState { private List activeContext; public ActiveContextState(SmallRyeContextManager context, List threadContext) { - activeContext = new ArrayList<>(threadContext.size()); - for (ThreadContextSnapshot threadContextSnapshot : threadContext) { - activeContext.add(threadContextSnapshot.begin()); + try { + activeContext = new ArrayList<>(threadContext.size()); + for (ThreadContextSnapshot threadContextSnapshot : threadContext) { + activeContext.add(threadContextSnapshot.begin()); + } + } catch (Throwable t) { + ROOT_LOGGER.errorBeginningThreadContextSnapshot(t.getLocalizedMessage()); + if (ROOT_LOGGER.isDebugEnabled()) { + t.printStackTrace(); + } + Util.rethrow(t); } } public void endContext() { - // restore in reverse order - for (int i = activeContext.size() - 1; i >= 0; i--) { - activeContext.get(i).endContext(); + try { + // restore in reverse order + for (int i = activeContext.size() - 1; i >= 0; i--) { + activeContext.get(i).endContext(); + } + } catch (Throwable t) { + ROOT_LOGGER.errorEndingContext(t.getLocalizedMessage()); + if (ROOT_LOGGER.isDebugEnabled()) { + t.printStackTrace(); + } + Util.rethrow(t); } } + } diff --git a/core/src/main/java/io/smallrye/context/impl/CapturedContextState.java b/core/src/main/java/io/smallrye/context/impl/CapturedContextState.java index c1941faa..5b4dd95d 100644 --- a/core/src/main/java/io/smallrye/context/impl/CapturedContextState.java +++ b/core/src/main/java/io/smallrye/context/impl/CapturedContextState.java @@ -1,5 +1,7 @@ package io.smallrye.context.impl; +import static io.smallrye.context.logging.SmallRyeContextPropagationLogger.ROOT_LOGGER; + import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -16,18 +18,24 @@ public class CapturedContextState { public CapturedContextState(SmallRyeContextManager context, ThreadContextProviderPlan plan, Map props) { - this.context = context; - for (ThreadContextProvider provider : plan.propagatedProviders) { - ThreadContextSnapshot snapshot = provider.currentContext(props); - if (snapshot != null) { - threadContext.add(snapshot); + try { + this.context = context; + for (ThreadContextProvider provider : plan.propagatedProviders) { + ThreadContextSnapshot snapshot = provider.currentContext(props); + if (snapshot != null) { + threadContext.add(snapshot); + } } - } - for (ThreadContextProvider provider : plan.clearedProviders) { - ThreadContextSnapshot snapshot = provider.clearedContext(props); - if (snapshot != null) { - threadContext.add(snapshot); + for (ThreadContextProvider provider : plan.clearedProviders) { + ThreadContextSnapshot snapshot = provider.clearedContext(props); + if (snapshot != null) { + threadContext.add(snapshot); + } } + } catch (Throwable t) { + ROOT_LOGGER.errorGettingSnapshot(t.getLocalizedMessage()); + t.printStackTrace(); + Util.rethrow(t); } } diff --git a/core/src/main/java/io/smallrye/context/impl/Util.java b/core/src/main/java/io/smallrye/context/impl/Util.java new file mode 100644 index 00000000..210b1203 --- /dev/null +++ b/core/src/main/java/io/smallrye/context/impl/Util.java @@ -0,0 +1,10 @@ +package io.smallrye.context.impl; + +/** + * @author Kabir Khan + */ +public class Util { + public static void rethrow(Throwable t) throws T { + throw (T) t; + } +} diff --git a/core/src/main/java/io/smallrye/context/logging/SmallRyeContextPropagationLogger.java b/core/src/main/java/io/smallrye/context/logging/SmallRyeContextPropagationLogger.java new file mode 100644 index 00000000..302dc664 --- /dev/null +++ b/core/src/main/java/io/smallrye/context/logging/SmallRyeContextPropagationLogger.java @@ -0,0 +1,30 @@ +package io.smallrye.context.logging; + +import static org.jboss.logging.Logger.Level.ERROR; + +import org.jboss.logging.BasicLogger; +import org.jboss.logging.Logger; +import org.jboss.logging.annotations.LogMessage; +import org.jboss.logging.annotations.Message; +import org.jboss.logging.annotations.MessageLogger; + +/** + * @author Kabir Khan + */ +@MessageLogger(projectCode = "SRCP", length = 5) +public interface SmallRyeContextPropagationLogger extends BasicLogger { + SmallRyeContextPropagationLogger ROOT_LOGGER = Logger.getMessageLogger(SmallRyeContextPropagationLogger.class, + "io.smallrye.context"); + + @LogMessage(level = ERROR) + @Message(id = 1, value = "An error occurred beginning the ThreadContextSnapshot: %s") + void errorBeginningThreadContextSnapshot(String errorMsg); + + @LogMessage(level = ERROR) + @Message(id = 2, value = "An error occurred ending the ThreadContext: %s") + void errorEndingContext(String errorMsg); + + @LogMessage(level = ERROR) + @Message(id = 3, value = "An error occurred getting the ThreadContextSnapshot: %s") + void errorGettingSnapshot(String errorMsg); +} diff --git a/pom.xml b/pom.xml index b04c2ea9..f6b7198f 100644 --- a/pom.xml +++ b/pom.xml @@ -40,6 +40,8 @@ 1.0.2 1.4 1.7.0 + 3.4.1.Final + 2.2.1.Final 3.1.1.Final 1.3 5.10.4.Final @@ -84,6 +86,26 @@ ${version.microprofile.context-propagation} + + org.jboss.logging + jboss-logging + ${version.jboss.logging} + + + + org.jboss.logging + jboss-logging-annotations + provided + ${version.jboss.logging.tools} + + + + org.jboss.logging + jboss-logging-processor + provided + ${version.jboss.logging.tools} + + io.reactivex.rxjava2 rxjava