-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Java Agent 使用方式 与 显式的使用方式 混用时,在tomcat下出现失效情况 #231
Comments
tomcat版本是8.0.22 |
private static final TransmittableThreadLocal<String> TRANSMITTABLE_CONTEXT = new TransmittableThreadLocal();
public void test() {
TRANSMITTABLE_CONTEXT.set("Test");
ExecutorService e = Executors.newFixedThreadPool(1);
e.submit(() -> log.info("init thread"));
e.submit(() -> {
final String test = TRANSMITTABLE_CONTEXT.get();
log.info("TTL:{}", test);
});
} 这是我的demo |
你找一下 有了结果反馈一下 :") @codingPao
方便 其它人 也了解和解决类似的问题 :") PS: 相关的Issue与解决:
|
目前能想到得有两个解决方式:
如果调整
但是这样调整得代价就是修改了整个 public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
/**
* 忽略一部分
*/
boolean delegateLoad = delegate || filter(name, true);
/**
* 忽略一部分
*/
}
protected boolean filter(String name, boolean isClassName) {
if (name == null)
return false;
char ch;
if (name.startsWith("javax")) {
/* 5 == length("javax") */
if (name.length() == 5) {
return false;
}
ch = name.charAt(5);
if (isClassName && ch == '.') {
/* 6 == length("javax.") */
if (name.startsWith("servlet.jsp.jstl.", 6)) {
return false;
}
if (name.startsWith("el.", 6) ||
name.startsWith("servlet.", 6) ||
name.startsWith("websocket.", 6) ||
name.startsWith("security.auth.message.", 6)) {
return true;
}
} else if (!isClassName && ch == '/') {
/* 6 == length("javax/") */
if (name.startsWith("servlet/jsp/jstl/", 6)) {
return false;
}
if (name.startsWith("el/", 6) ||
name.startsWith("servlet/", 6) ||
name.startsWith("websocket/", 6) ||
name.startsWith("security/auth/message/", 6)) {
return true;
}
}
} else if (name.startsWith("org")) {
/* 3 == length("org") */
if (name.length() == 3) {
return false;
}
ch = name.charAt(3);
if (isClassName && ch == '.') {
/* 4 == length("org.") */
if (name.startsWith("apache.", 4)) {
/* 11 == length("org.apache.") */
if (name.startsWith("tomcat.jdbc.", 11)) {
return false;
}
if (name.startsWith("el.", 11) ||
name.startsWith("catalina.", 11) ||
name.startsWith("jasper.", 11) ||
name.startsWith("juli.", 11) ||
name.startsWith("tomcat.", 11) ||
name.startsWith("naming.", 11) ||
name.startsWith("coyote.", 11)) {
return true;
}
}
} else if (!isClassName && ch == '/') {
/* 4 == length("org/") */
if (name.startsWith("apache/", 4)) {
/* 11 == length("org/apache/") */
if (name.startsWith("tomcat/jdbc/", 11)) {
return false;
}
if (name.startsWith("el/", 11) ||
name.startsWith("catalina/", 11) ||
name.startsWith("jasper/", 11) ||
name.startsWith("juli/", 11) ||
name.startsWith("tomcat/", 11) ||
name.startsWith("naming/", 11) ||
name.startsWith("coyote/", 11)) {
return true;
}
}
}
}
return false;
} 基于以上可以看出 因为我们目前研发了一个基于 |
@codingPao 那这个 Issue 先 close 了,后续有什么欢迎继续讨论。 |
@oldratlee 能分享下这个修复方案的源码在哪个项目中能看到吗 |
@codingPao 可以交流 回答一下 😃❤️ |
定位了一下发现原因是:
tomcat
的WebappClassLoader
打破了双亲委托机制com.alibaba.ttl.TransmittableThreadLocal.holder
会在WebappClassLoader
中初始化一份;在做TTL操作时,TTL的引用由这个holder
持有。submit
时,TtlRunnable
虽然走了增强,但是copy
操作的是由BootstrapClassLoader
初始化的com.alibaba.ttl.TransmittableThreadLocal.holder
进行copy
,没有copy
到预期的TTL。The text was updated successfully, but these errors were encountered: