Java 如何与 spring 异步运行方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9809410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I run a method Asynchronously with spring?
提问by Grammin
The following code is suppose to work Asynchronously but instead it waits for the Async part to finish and then goes. How can I make the blah()
method run Asynchronously?
下面的代码假设异步工作,但它等待异步部分完成然后去。如何使blah()
方法异步运行?
spring.xml
弹簧文件
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<!-- Activates @Scheduled and @Async annotations for scheduling -->
<task:annotation-driven />
<bean id="test"
class="com.spring.test.Test">
</beans>
Test.java
测试.java
@Path("/test")
public class Test
{
@GET
@Path("/test")
@Produces("text/plain")
public String tester()
{
return "Running...";
}
@GET
@Path("/triggerNew")
@Produces("text/plain")
public String triggerNew()
{
System.out.println("BEFORE " + new Date() + " BEFORE");
new Process().blah();
System.out.println("AFTER " + new Date() + " AFTER");
return "TRIGGERED";
}
}
Process.java
进程.java
@Component
public class Process
{
@Async
public void blah()
{
try
{
Thread.currentThread().sleep(5000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println("NEW THREAD " + new Date() + " NEW THREAD");
}
}
采纳答案by skaffman
@Async
only works when you annotate Spring-managed beans, not arbitrary classes. You need to define Process
as a Spring bean, and then inject it into your controller class, e.g.
@Async
仅当您注释 Spring 管理的 bean 时才有效,而不是任意类。您需要定义Process
为一个Spring bean,然后将其注入到您的控制器类中,例如
<bean id="test" class="com.spring.test.Test">
<property name="process">
<bean class="com.spring.test.Process"/>
</property>
</bean>
public class Test {
private Process process;
public void setProcess(Process process) {
this.process = process;
}
...
public String triggerNew() {
process.blah();
}
}
回答by vacuum
Alternatively you can execute your task manually with TaskExecutor. Just define executor in the context:
或者,您可以使用 TaskExecutor 手动执行您的任务。只需在上下文中定义执行程序:
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"/>
Than you can execute yor task:
比你可以执行你的任务:
taskExecutor.execute(new Process ());
But in this case your Process class must implement Runnable interface
但在这种情况下,您的 Process 类必须实现 Runnable 接口