springboot异步调用demo
程序员文章站
2022-05-03 17:39:41
...
第一步建立一个类
package com.zys.async;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;
@Component
@EnableAsync //开启异步调用
public class AsyncDemo {
@Async
public void sendSms(){
System.out.println("####sendSms#### 2");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("####sendSms#### 3");
}
}
第二步:测试即可
@Autowired
private AsyncDemo asyncDemo;
在controller 测试即可
asyncDemo.sendSms();
package com.zys.async;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;
@Component
@EnableAsync //开启异步调用
public class AsyncDemo {
@Async
public void sendSms(){
System.out.println("####sendSms#### 2");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("####sendSms#### 3");
}
}
第二步:测试即可
@Autowired
private AsyncDemo asyncDemo;
在controller 测试即可
asyncDemo.sendSms();