当前位置:网站首页>Be careful of this hole in transmittable thread local
Be careful of this hole in transmittable thread local
2022-06-30 08:20:00 【InfoQ】
Abnormal scene
-javaagent:/path/to/transmittable-thread-local-2.12.1.jarorg.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor' available: expected single matching bean but found 3: executor1,executor2,executor3
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1200)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveBean(DefaultListableBeanFactory.java:420)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:349)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:342)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1127)
……
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:771)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:88)
……
AbstractApplicationContext.getBeanThreadPoolTaskExecutorNoUniqueBeanDefinitionExceptionTroubleshoot problems
public static void doSth(Object subtag, Object extra, long time) {
ApplicationContextContainer.getBean(ThreadPoolTaskExecutor.class)
.execute(() -> {
// Some business code
});
}
@Component
public class ApplicationContextContainer implements ApplicationContextAware {
private static ApplicationContext applicationContext;
public static <T> T getBean(Class<T> clazz) {
return applicationContext.getBean(clazz);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ApplicationContextContainer.applicationContext = applicationContext;
}
}
applicationContext.getBeanThreadPoolTaskExecutor@Configuration
public class ExecutorConfig {
@Bean(value = "executor1")
public Executor executor1() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
// Some initialization methods
taskExecutor.initialize();
return taskExecutor;
}
@Bean(value = "executor2")
public Executor executor2() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
// Some initialization methods
taskExecutor.initialize();
return TtlExecutors.getTtlExecutor(taskExecutor);
}
@Bean(value = "executor3")
public Executor executor3() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
// Some initialization methods
taskExecutor.initialize();
return TtlExecutors.getTtlExecutor(taskExecutor);
}
}
ExecutorThreadPoolTaskExecutorTtlExecutors.getTtlExecutorThreadPoolTaskExecutorTtlExecutors.getTtlExecutorpublic static Executor getTtlExecutor(@Nullable Executor executor) {
if (TtlAgent.isTtlAgentLoaded() || null == executor || executor instanceof TtlEnhanced) {
return executor;
}
return new ExecutorTtlWrapper(executor, true);
}
TtlExecutors.getTtlExecutorThreadPoolTaskExecutorif- The code has not been changed , Well done before , Why did you make a mistake ;
- This place is easy to use , Why is an error reported on the server .
Location problem
TtlExecutors.getTtlExecutorif- TtlAgent.isTtlAgentLoaded(): This is judgment ttlAgentLoaded identification , This will be discussed later ;
- null == executor: Input parameter is null, Obviously not in line with ;
- executor instanceof TtlEnhanced: The input parameters are
TtlEnhancedtype , The input isThreadPoolTaskExecutortype , Do not conform to the .
public static boolean isTtlAgentLoaded() {
return ttlAgentLoaded;
}
ttlAgentLoadedpublic final class TtlAgent {
public static void premain(final String agentArgs, @NonNull final Instrumentation inst) {
kvs = splitCommaColonStringToKV(agentArgs);
Logger.setLoggerImplType(getLogImplTypeFromAgentArgs(kvs));
final Logger logger = Logger.getLogger(TtlAgent.class);
try {
logger.info("[TtlAgent.premain] begin, agentArgs: " + agentArgs + ", Instrumentation: " + inst);
final boolean disableInheritableForThreadPool = isDisableInheritableForThreadPool();
// Omit irrelevant code
ttlAgentLoaded = true;
} catch (Exception e) {
String msg = "Fail to load TtlAgent , cause: " + e.toString();
logger.log(Level.SEVERE, msg, e);
throw new IllegalStateException(msg, e);
}
}
// Omit irrelevant code
}
premainExecutorConfigThreadPoolTaskExecutorExecutorTtlWrapperapplicationContext.getBean(clazz)-javaagent:/path/to/transmittable-thread-local-2.12.1.jartransmittable-thread-localExecutorConfigThreadPoolTaskExecutorapplicationContext.getBean(clazz)ThreadPoolTaskExecutorNoUniqueBeanDefinitionException-javaagent:/path/to/transmittable-thread-local-2.12.1.jarsolve the problem
applicationContext.getBean(beanName, clazz)public static void doSth(Object subtag, Object extra, long time) {
ApplicationContextContainer.getBean("executor1", ThreadPoolTaskExecutor.class)
.execute(() -> {
// Some business code
});
}
Recommended reading
- One article mastery Java8 Stream in Collectors Of 24 Operations
- One article mastery Java8 Of Optional Of 6 Kind of operation
- Use Lambda Expression to achieve super sorting function
- Java8 Time library of (1): Introduce Java8 Time classes and common API
- Java8 Time library of (2):Date And LocalDate or LocalDateTime Interconversion
- Java8 Time library of (3): Start using Java8 Time class in
- Java8 Time library of (4): Check whether the date string is legal
- Java8 New features
- Java9 New features
- Java10 New features
- Java11 Access control optimization based on nested relationship in
- Java11 New features
- Java12 New features
- Java13 New features
- Java14 New features
- Java15 New features
- Java16 New features
- Java17 New features
- From small worker to expert Java An advanced journey
边栏推荐
- Do you know the IP protocol?
- Sword finger offer II 076 The kth largest number in the array (use heap to solve TOPK problem)
- 在浏览器输入url到页面展示出来
- Dlib database face
- 国债逆回购绝对安全吗 网上怎么开户
- Construction of module 5 of actual combat Battalion
- Redis设计与实现(二)| 数据库(删除策略&过期淘汰策略)
- Is it difficult to jump job ByteDance? With these skills, you can easily pass
- Game 280 problem2: minimum operands to turn an array into an alternating array
- 我们如何拿到自己满意的薪资呢?这些套路还是需要掌握的
猜你喜欢

Acreems energy efficiency management platform escorts the power safety of high-rise residential areas

1. Problems related to OpenGL window and environment configuration

增强for循环的增删操作 & 迭代器删除集合元素

Using typera+picgo to realize automatic uploading of markdown document pictures

一次cpu 跌底排查

【NVMe2.0b 14-1】Abort、Asynchronous Event Request、Capacity Management command

End-to-end 3D Point Cloud Instance Segmentation without Detection

Swagger use

【NVMe2.0b 14】NVMe Admin Command Set

电流探头电路分析
随机推荐
Deep learning - embedding matrix and learning word embedding andword2vec
1162 Postfix Expression
Deep learning vocabulary representation
增强for循环的增删操作 & 迭代器删除集合元素
【NVMe2.0b 14-1】Abort、Asynchronous Event Request、Capacity Management command
Graffiti Wi Fi & ble SoC development slide strip
Sword finger offer II 074 Merge interval (sort, array)
Implementation of remote monitoring by camera in Experiment 5
Opencv4.2.0+vs2015 configuration
Gilbert Strang's course notes on linear algebra - Lesson 2
QT event cycle
Construction of module 5 of actual combat Battalion
Go 数据类型篇之字符串及底层字符类型
[nvme2.0b 14 - 5] commande de téléchargement / commande du logiciel
Camera
Rendering engine development
2021-02-18
Gilbert Strang's course notes on linear algebra - Lesson 1
跳槽字节跳动很难嘛?掌握这些技巧,你也能轻松通过
奇迹MU服务器租用选择 真实好用 稳定不卡 还能防入侵