CompletableFuture与fegin一起使用出现的bug

一、 使用场景:

         需要批量rpc调用远程服务之后,拿到结果处理,使用socketIo异步推送的方式把处理后的结果推送给前端。

CompletableFuture.supplyAsync(() -> remotePushMessageService.pushWebMessage(pushMessageVo));

二、出现的问题

在idea中未发现,这个问题,但是在服务器部署时,使用java -jar启动jar包出现以下异常:

 [ForkJoinPool.commonPool-worker-3] WARNo.s.c.a.AnnotationConfigApplicationContext
[refresh,592] -Exception encountered during context initialization -cancellin
refresh attempt: org.springframework.beans.factory.BeanDefinitcionStoreException: Failed to parse configuration class [org.springframework.cloud.loadbalancer.annotation.LoadBala
ncerClientConfiguration

CompletableFuture使用的是ForkJoinPool线程池,与fegin一起使用会有 Failed to paarse configuration class [org.springframework.cloudloadbalancer.annotation. LoadBalancerClientConfig异常,这个异常是一直都有的,去年spring刚解决

1. 如果我们在IDEA中运行应用程序,默认是系统类加载器加载Spring和应用程序类,因此不容易重现此问题。

2. 如果我们使用JAR命令行运行应用程序,则spring将使用org.springframework.boot.loader . launchedurlclassloader来加载这些类。从Java 9开始,ForkJoinPool使用系统类加载器作为上下文类加载器来创建线程,如果这时线程切换到使用系统类加载器,launchedurlclassloader将无法加载Spring和JAR中的其他类。

三、解决方式

1. 在配置类里或者被@Configuration注解的类加上下面配置(中等

@Bean
	@ConditionalOnMissingBean
	public LoadBalancerClientFactory loadBalancerClientFactory(LoadBalancerClientsProperties properties) {
		return new LoadBalancerClientFactory(properties) {
			@Override public AnnotationConfigApplicationContext createContext(String name) {
				ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
				Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
				AnnotationConfigApplicationContext context = (AnnotationConfigApplicationContext) super.createContext(name);
				Thread.currentThread().setContextClassLoader(originalClassLoader);
				return context;
			}
		};
	}

2. 不用CompletableFuture默认的ForkJoinPool线程池,使用自定义的线程池(下等

   2.1 自定义一个线程池

@Configuration
public class TaskExecutorConfig {

  @Bean("asyncPool")
  public Executor asyncPool() {

    return Executors.newFixedThreadPool(10);
  }
}

----------------------------------------上面写成一个配置类即可-------------------------------

然后在你使用的时候通过注入的方式

CompletableFuture.runAsync(
  () -> {
                your code here...
            },
  asyncPool
);

3. 创建了ApplicationContextInitializer实现(并添加到spring.factories中),以手动将类加载器设置为启动应用程序的上下文类加载器。(上等

public class ClassLoaderApplicationContextInitializer
        implements ApplicationContextInitializer<ConfigurableApplicationContext> {

    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        applicationContext.setClassLoader(applicationContext.getClassLoader());
    }
}

然后在yml文件里添加

context:
  initializer:
    classes: com.xx.xx.init.ClassLoaderApplicationContextInitializer
  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘个Java

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值