简体   繁体   English

在 springboot 中实现 runnable 以服务新请求

[英]Implement runnable to serve a new request in springboot

I have a usecase in a spring boot application, where in we get a request, we send an acknowledgement back and then start a new executor task in background which will do some processing and send back some result.我在 spring 启动应用程序中有一个用例,在我们收到请求时,我们发回确认,然后在后台启动一个新的执行程序任务,该任务将进行一些处理并发回一些结果。

Now I am having some doubts while creating the runnable task.现在我在创建可运行任务时有一些疑问。 I want for every request a new instance of this runnable task is submitted to the executor service.我希望每个请求都将这个可运行任务的新实例提交给执行程序服务。 Could some clarify if keeping the scope as "prototype" should resolve my purpose or the scope should be "request".是否可以澄清将 scope 保留为“原型”应该解决我的目的,或者 scope 应该是“请求”。 And if the latter is correct, is the default context in spring boot is web-aware?如果后者是正确的,spring 引导中的默认上下文是否支持网络?

Also I need to pass in some parameters in the runnable task.我还需要在可运行任务中传递一些参数。 Any pointers would be appreciated for both the above problems.对于上述两个问题,任何指针都将不胜感激。

TA助教

Spring can manage threads for you using the @Async annotation. Spring 可以使用@Async注解为您管理线程。 This can be much simpler than managing them yourself if you are already using Spring.如果您已经在使用 Spring,这比自己管理它们要简单得多。

You can read about it here: https://www.baeldung.com/spring-async你可以在这里阅读: https://www.baeldung.com/spring-async

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM