Skip to content

Commit

Permalink
Add JBoss Logging and log exceptions when calling the thread context
Browse files Browse the repository at this point in the history
providers
  • Loading branch information
kabir committed Aug 21, 2020
1 parent 62aa0eb commit 3e3ad48
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 16 deletions.
14 changes: 14 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@
<artifactId>microprofile-config-api</artifactId>
<version>${version.microprofile.config}</version>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging-annotations</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging-processor</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.threads</groupId>
<artifactId>jboss-threads</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -13,16 +15,36 @@ public class ActiveContextState {
private List<ThreadContextController> activeContext;

public ActiveContextState(SmallRyeContextManager context, List<ThreadContextSnapshot> 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();
}
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();
}
rethrow(t);
}
}

public static <T extends Throwable> void rethrow(Throwable t) throws T {
throw (T)t;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,18 +18,24 @@ public class CapturedContextState {

public CapturedContextState(SmallRyeContextManager context, ThreadContextProviderPlan plan,
Map<String, String> 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();
ActiveContextState.rethrow(t);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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 <a href="mailto:[email protected]">Kabir Khan</a>
*/
@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);
}
22 changes: 22 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
<version.microprofile.context-propagation>1.0.2</version.microprofile.context-propagation>
<version.microprofile.config>1.4</version.microprofile.config>
<version.smallrye.config>1.7.0</version.smallrye.config>
<version.jboss.logging>3.4.1.Final</version.jboss.logging>
<version.jboss.logging.tools>2.2.1.Final</version.jboss.logging.tools>
<version.jboss.threads>3.1.1.Final</version.jboss.threads>
<version.jta>1.3</version.jta>
<version.narayana>5.10.4.Final</version.narayana>
Expand Down Expand Up @@ -84,6 +86,26 @@
<version>${version.microprofile.context-propagation}</version>
</dependency>

<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<version>${version.jboss.logging}</version>
</dependency>

<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging-annotations</artifactId>
<scope>provided</scope>
<version>${version.jboss.logging.tools}</version>
</dependency>

<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging-processor</artifactId>
<scope>provided</scope>
<version>${version.jboss.logging.tools}</version>
</dependency>

<dependency>
<groupId>io.reactivex.rxjava2</groupId>
<artifactId>rxjava</artifactId>
Expand Down

0 comments on commit 3e3ad48

Please sign in to comment.