Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JBoss Logging and log exceptions when calling the thread context #197

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,33 @@ 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();
}
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);
}
}

}
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();
Util.rethrow(t);
}
}

Expand Down
10 changes: 10 additions & 0 deletions core/src/main/java/io/smallrye/context/impl/Util.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.smallrye.context.impl;

/**
* @author <a href="mailto:[email protected]">Kabir Khan</a>
*/
public class Util {
public static <T extends Throwable> void rethrow(Throwable t) throws T {
throw (T) t;
}
}
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