jenkins启动SringBoot偶然报错!!!
jenkins启动SpringBoot偶尔会报错!
启动SpringBoot的项目的时候。
1、环境
- springboot ;
- jdk 1.8 ;
- jenkins部署 。
2、代码
1.使用了AsyncTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;
import java.util.concurrent.Future;
@Component
public class AsyncTask {
@Autowired
private AsyncTaskService asyncTaskService;
private final static String CALL_BACK = "success";
@Async
public Future<String> addUserGroupReadCount(Integer groupId,Integer count) throws Exception {
asyncTaskService.addUserGroupReadCount(groupId,count);
return new AsyncResult<String>(CALL_BACK);
}
}
2.设置了AsyncTaskConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.lang.reflect.Method;
import java.util.concurrent.Executor;
@EnableAsync
@Configuration
@ComponentScan("com.meyoung.cloudguide.task")
public class AsyncTaskConfig implements AsyncConfigurer {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(10);
taskExecutor.setMaxPoolSize(500);
taskExecutor.setQueueCapacity(25);
taskExecutor.setAwaitTerminationSeconds(60);
taskExecutor.setThreadNamePrefix("MeYoungAsync-Manage-");
taskExecutor.initialize();
return taskExecutor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return new SpringAsyncExceptionHandler();
}
class SpringAsyncExceptionHandler implements AsyncUncaughtExceptionHandler {
@Override
public void handleUncaughtException(Throwable throwable, Method method, Object... obj) {
logger.error("Exception occurs in async method", throwable.getMessage());
}
}
}
3、错误日志
重启jenkins时候,偶尔报错。
注意:我本地居然没问题,正式环境启动也没问题,目前报错的就是在测试环境上面。
日志图片:

看不清楚的请看这里
14:41:24 Process leaked file descriptors. See https://jenkins.io/redirect/troubleshooting/process-leaked-file-descriptors for more information
14:41:26 14:41:26.327 logback [main] WARN o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext -
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'sendMsgController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException:
Error creating bean with name 'asyncTask': Bean with name 'asyncTask' has been injected into other beans [newsReplyService,questionService,questionReplyService] in its raw version as part of a circular reference,
but has eventually been wrapped. This means that said other beans do not use the final version of the bean.
This is often the result of over-eager type matching - consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.
14:41:26 14:41:26.356 logback [main] INFO o.a.catalina.core.StandardService - Stopping service [Tomcat]
14:41:26 14:41:26.401 logback [main] INFO o.s.b.a.l.ConditionEvaluationReportLoggingListener -
14:41:26
14:41:26 Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
14:41:26 14:41:26.409 logback [main] ERROR o.s.boot.SpringApplication - Application run failed
14:41:26 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sendMsgController': Injection of resource dependencies failed;
nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'asyncTask':
Bean with name 'asyncTask' has been injected into other beans [newsReplyService,questionService,questionReplyService] in its raw version as part of a circular reference,
but has eventually been wrapped. This means that said other beans do not use the final version of the bean.
This is often the result of over-eager type matching - consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.
14:41:26 at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:321)
14:41:26 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1341)
14:41:26 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:572)
14:41:26 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495)
14:41:26 at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
14:41:26 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
14:41:26 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
14:41:26 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
14:41:26 at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:759)
14:41:26 at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869)
14:41:26 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
14:41:26 at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
14:41:26 at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762)
14:41:26 at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:398)
14:41:26 at org.springframework.boot.SpringApplication.run(SpringApplication.java:330)
14:41:26 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1258)
14:41:26 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246)
14:41:26 at com.meyoung.cloudguide.CloudguideApplication.main(CloudguideApplication.java:20)
14:41:26 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
14:41:26 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
14:41:26 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
14:41:26 at java.lang.reflect.Method.invoke(Method.java:498)
14:41:26 at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
14:41:26 at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
14:41:26 at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
14:41:26 at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
14:41:26 Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'asyncTask': Bean with name 'asyncTask' has been injected into other beans [newsReplyService,questionService,questionReplyService] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.
14:41:26 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:602)
14:41:26 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495)
14:41:26 at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
14:41:26 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
14:41:26 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
14:41:26 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204)
14:41:26 at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:514)
14:41:26 at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:485)
14:41:26 at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:619)
14:41:26 at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:177)
14:41:26 at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91)
14:41:26 at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:318)
14:41:26 ... 25 common frames omitted
14:41:26 Finished: SUCCESS
4、解决办法
分析日志:
从日志中很明显的看出来了问题是循环注入引起的错误。
业务层(serviceImpl)是可以引用其他接口层(service)方法这是没毛病的。
原来多线程调度任务时候是有问题,多线程的调用的类循环注入A,而A又引用了B,B又引用了A,这样问题就来了。
最后解决办法
asyncTask 中只引用一个service接口,对应的实现层(serviceImpl)中不再引用其他接口层,调用sql是直接引用dao层。这样以后编写都要求形成这样的规范,也就没有再出现偶尔启动报错的问题了。
来源:CSDN
作者:杭州·晨光
链接:https://blog.csdn.net/zjh735939302/article/details/86576312