通过在Spring Boot应用程序上运行的ajax调用Callable方法时,嵌入式tomcat异步处理请求并完全正常:
2017-07-02 09:12:24.283 INFO 3582 --- [nio-8090-exec-3] c.r.web.controllers.MainController : Ali was stored in redis
2017-07-02 09:12:25.671 INFO 3582 --- [ngAsyncThread-1] c.r.web.controllers.MainController : Ali is searching for a chat
2017-07-02 09:12:25.820 INFO 3582 --- [ngAsyncThread-1] c.r.web.controllers.MainController : Ali last activity date was updated in redis
2017-07-02 09:12:26.053 INFO 3582 --- [ngAsyncThread-1] c.r.web.controllers.MainController : Ali retrieving random conv null
2017-07-02 09:12:26.054 INFO 3582 --- [ngAsyncThread-1] c.r.web.controllers.MainController : Ali created my own conversation
2017-07-02 09:12:26.211 INFO 3582 --- [ngAsyncThread-1] c.r.web.controllers.MainController : Ali searching...
2017-07-02 09:12:27.927 INFO 3582 --- [ngAsyncThread-1] c.r.web.controllers.MainController : Ali searching...
2017-07-02 09:12:29.128 INFO 3582 --- [ngAsyncThread-1] c.r.web.controllers.MainController : Ali searching...
2017-07-02 09:12:29.305 INFO 3582 --- [nio-8090-exec-2] c.r.web.controllers.MainController : Zeina was stored in redis
2017-07-02 09:12:29.984 INFO 3582 --- [ngAsyncThread-2] c.r.web.controllers.MainController : Zeina is searching for a chat
2017-07-02 09:12:30.135 INFO 3582 --- [ngAsyncThread-2] c.r.web.controllers.MainController : Zeina last activity date was updated in redis
2017-07-02 09:12:30.285 INFO 3582 --- [ngAsyncThread-2] c.r.web.controllers.MainController : Zeina retrieving random conv Conversation [id=bbb17857-9049-4,person1=Person [id=9a243d57-2f0c-4,name=Ali,age=22,gender=M,ip=0:0:0:0:0:0:0:1,loginDate=Sun Jul 02 09:12:23 EEST 2017,lastActiveDate=Sun Jul 02 09:12:25 EEST 2017],person2=null,creationDate=Sun Jul 02 09:12:26 EEST 2017]
2017-07-02 09:12:30.687 INFO 3582 --- [ngAsyncThread-1] c.r.web.controllers.MainController : Ali searching...
2017-07-02 09:39:54.420 INFO 3582 --- [ngAsyncThread-3] c.r.web.controllers.MainController : Zeina is searching for a chat
但是,当我在外部tomcat服务器上部署应用程序时,一个调用挂起服务器,而下一个调用仅在第一次调用完成后处理.
2017-07-02 06:47:57.285 INFO 3324 --- [SpringAsyncThread-4] c.r.web.controllers.MainController : Zanzoun searching...
2017-07-02 06:47:58.297 INFO 3324 --- [SpringAsyncThread-4] c.r.web.controllers.MainController : Zanzoun searching...
2017-07-02 06:47:59.309 INFO 3324 --- [SpringAsyncThread-4] c.r.web.controllers.MainController : Zanzoun searching...
2017-07-02 06:48:00.323 INFO 3324 --- [SpringAsyncThread-4] c.r.web.controllers.MainController : Zanzoun searching...
2017-07-02 06:48:01.335 INFO 3324 --- [SpringAsyncThread-4] c.r.web.controllers.MainController : Zanzoun searching...
2017-07-02 06:48:02.347 INFO 3324 --- [SpringAsyncThread-4] c.r.web.controllers.MainController : Zanzoun searching...
2017-07-02 06:48:03.359 INFO 3324 --- [SpringAsyncThread-4] c.r.web.controllers.MainController : Zanzoun searching...
2017-07-02 06:48:04.388 INFO 3324 --- [SpringAsyncThread-4] c.r.web.controllers.MainController : Zanzoun remove dead conversatio
2017-07-02 06:48:04.682 INFO 3324 --- [SpringAsyncThread-5] c.r.web.controllers.MainController : AJ is searching for a chat
2017-07-02 06:48:04.695 INFO 3324 --- [SpringAsyncThread-5] c.r.web.controllers.MainController : AJ last activity date was updated in redis
2017-07-02 06:48:04.708 INFO 3324 --- [SpringAsyncThread-5] c.r.web.controllers.MainController : AJ retrieving random conv Conversation [id=f5fe39ac-91ce-4,person1=Person [id=e25d18f3-9729-4,name=Zanzoun,age=12,gender=F,ip=91.197.46.40,loginDate=Sun Jul 02 06:47:32 UTC 2017,lastActiveDate=Sun Jul 02 06:47:48 UTC 2017],creationDate=Sun Jul 02 06:47:48 UTC 2017]
2017-07-02 06:48:50.833 INFO 3324 --- [SpringAsyncThread-6] c.r.web.controllers.MainController : AJ is searching for a chat
2017-07-02 06:48:50.845 INFO 3324 --- [SpringAsyncThread-6] c.r.web.controllers.MainController : AJ last activity date was updated in redis
2017-07-02 06:48:50.860 INFO 3324 --- [SpringAsyncThread-6] c.r.web.controllers.MainController : AJ retrieving random conv null
2017-07-02 06:48:50.860 INFO 3324 --- [SpringAsyncThread-6] c.r.web.controllers.MainController : AJ created my own conversation
2017-07-02 06:48:50.872 INFO 3324 --- [SpringAsyncThread-6] c.r.web.controllers.MainController : AJ searching...
2017-07-02 06:48:51.884 INFO 3324 --- [SpringAsyncThread-6] c.r.web.controllers.MainController : AJ searching...
2017-07-02 06:48:52.896 INFO 3324 --- [SpringAsyncThread-6] c.r.web.controllers.MainController : AJ searching...
2017-07-02 06:48:53.908 INFO 3324 --- [SpringAsyncThread-6] c.r.web.controllers.MainController : AJ searching...
2017-07-02 06:48:54.921 INFO 3324 --- [SpringAsyncThread-6] c.r.web.controllers.MainController : AJ searching...
在上面的日志中,来自Zanzoun和AJ的电话同时被发送,但是在Zanzoun结束之前AJ处理没有开始.
下面我列出我的配置:
@Configuration
@EnableAsync
public class MvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("main");
registry.addViewController("/login").setViewName("login");
registry.addViewController("/admin").setViewName("admin");
}
@Override
public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
configurer.setTaskExecutor(asyncTaskExecutor());
}
@Bean
public AsyncTaskExecutor asyncTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);
executor.setMaxPoolSize(200);
executor.setQueueCapacity(500);
executor.setThreadNamePrefix("SpringAsyncThread-");
executor.initialize();
return executor;
}
}
@SpringBootApplication
@EnableScheduling
@EnableAsync
public class App extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(App.class,args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(App.class);
}
}
更新1
更新2
我已经使用外部Tomcat 8.0.21在我的Linux机器上进行了本地测试,它运行良好.但是,我在Windows中安装了相同版本的Tomcat(它实际上是一个AWS实例),问题仍然存在,所以这是一个操作系统/版本问题?
更新3
这是所要求的pom文件:
最佳答案
您是否尝试过检查实际池大小,例如,通过JMX:https://wiki.apache.org/tomcat/FAQ/Monitoring#Thread_Usage?
原文链接:https://www.f2er.com/spring/432083.html此外,您是否可以尝试安装APR Native库来验证问题不是自动选择的传入流量协议:https://tomcat.apache.org/tomcat-8.0-doc/apr.html
Windows EC2实例的配置也可能会影响观察到的行为,您是否也可以将其添加到问题中?