[英]Why is my Spring @Async bean method not being executed asychronously?
I have a Springboot application and I'm trying to execute an asynchronous method on a bean class inside a controller method. 我有一个Springboot应用程序,正在尝试在控制器方法内的bean类上执行异步方法。 The problem is that my @Async method is not being executed asynchronously.
问题是我的@Async方法没有异步执行。 Execution is halted until the method completes.
在该方法完成之前,执行将停止。
Can anyone tell me what I'm missing? 谁能告诉我我所缺少的吗?
Here is my application class: 这是我的应用程序类:
@SpringBootApplication
@EnableAsync
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
@Override
public void customize(Connector connector) {
connector.setPort(9000);
connector.setAsyncTimeout(60000);
}
});
return factory;
}
}
Here is my bean class: 这是我的bean类:
public class LongProcess {
@Async
public Future<String> call() {
try {
System.out.println("Sleeping now...");
Thread.sleep(10000);
return new AsyncResult<String>("Hey");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}
My configuration class: 我的配置类:
@Configuration
@EnableAsync
public class LongProcessConfiguration implements AsyncConfigurer {
@Bean
public LongProcess longProcessBean() {
return new LongProcess();
}
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setMaxPoolSize(10);
taskExecutor.setThreadNamePrefix("LULExecutor-");
taskExecutor.initialize();
return taskExecutor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return new SimpleAsyncUncaughtExceptionHandler();
}
}
My controller method: 我的控制器方法:
@RequestMapping("/utilities/longProcess")
public String longProcess() {
System.out.println("Starting long process...");
CsvFileDifferConfiguration context = new CsvFileDifferConfiguration();
LongProcess process = context.longProcessBean();
Future<String> result = process.call();
System.out.println("Done!");
return "{success: 1}";
}
This request unfortunately does not return immediately (I don't care about the result). 不幸的是,此请求不会立即返回(我不在乎结果)。 The method is called successfully, but not in the background.
该方法已成功调用,但未在后台调用。 Any idea what I might be missing?
知道我可能会缺少什么吗?
As a test, if I change the controller method to wait for the result, the wait block is never entered: 作为测试,如果更改控制器方法以等待结果,则永远不会输入wait块:
@RequestMapping("/utilities/longProcess")
public String longProcess() throws InterruptedException {
System.out.println("Starting long process...");
CsvFileDifferConfiguration context = new CsvFileDifferConfiguration();
LongProcess process = context.longProcessBean();
Future<String> result = process.call();
while (!(result.isDone())) {
Thread.sleep(1); //10-millisecond pause between each check
System.out.println("Waiting for Long Process...");
}
System.out.println("Done!");
return "{success: 1}";
}
You have a mistake for the CDI usage. 您对CDI的使用有误。 If you manage your object using Spring Container you have to get deal just with
ApplicationContext
or its abilities like @Autowired
. 如果使用Spring Container管理对象,则必须处理
ApplicationContext
或其类似@Autowired
。
The code 编码
CsvFileDifferConfiguration context = new CsvFileDifferConfiguration();
is wrong. 是错的。
Since you define your LongProcess
as a @Bean
you can just inject it to your @Controller
: 由于您将
LongProcess
定义为@Bean
,因此可以将其注入到@Controller
:
@Autowired
privete LongProcess process;
and use it as before. 并像以前一样使用它。
Using objects directly (eg new
) loses the dependency injection
features. 直接使用对象(例如
new
)会丢失dependency injection
功能。
Read more Spring Docs, please. 请阅读更多Spring Docs。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.