To summarize the problem, i have a common class in a bundle that is being used all around the project. This class is an Aspect class which puts values into ThreadContext(log4j2) in order for our logging to have corresponding session details into the logs.
I have found out that ThreadContext works like a ThreadLocal variable. And since OSGi have different classloaders for each bundle, callers/users of the ThreadContext values are not able to see these values at all.
References:
Effect of ThreadLocals and side-by-side classloading https://stackoverflow.com/a/34738856/8136561
What i expect is to still have a bundle with the common code in it but the other bundles would still be able to see the values put into the ThreadContext. I'm not sure if this is even possible.
Edit: Added Sample Code
Bundle1 (Common Code)
@Configurable
@Aspect
public class AspectLogger {
@Before("within(@org.springframework.stereotype.Controller *)")
public void beforeControllerAdvice(JoinPoint joinPoint) {
Object[] paramValues = null;
paramValues = joinPoint.getArgs();
Object request = null;
for (Object arg : paramValues) {
if (arg instanceof RenderRequest) {
request = arg;
} else if (arg instanceof ResourceRequest) {
request = arg;
} else if (arg instanceof ActionRequest) {
request = arg;
}
}
if (request != null) {
String transactionID = UUID.randomUUID().toString();
ThreadContext.put("transactionID", transactionID);
}
}
}
Bundle 2,3...n (Using this Aspect as a Bean)
<bean id="aspectLogger" class="shared.bundle.common.AspectLogger" />
Log4j2 configuration using this value for logger appenders (Problem values are empty in the logs)
<PatternLayout>
<Pattern>%-5p | %d{yyyy-MM-dd HH:mm:ss} | [%t] %c{2} (%F:%L) [transactionID: %X{transactionID}] - %m%n</Pattern>
</PatternLayout>
From Debugger point of view, i can see that the controller's thread and aspect's thread is basically the same. But i am still not getting the values, i even tried ThreadContext.get("transactionID")
in the @Controller
level but it is empty.