Java – how to run methods asynchronously using spring?
•
Java
The following code assumes asynchronous work, but waits for the async part to complete and then continues How do I make the blah () method run asynchronously?
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
@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
@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");
}
}
Solution
@Async is only valid when annotating spring managed beans instead of arbitrary classes You need to define process as a spring bean and inject it into your controller class, such as
<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();
}
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
