使用@Async实现异步调用
生活随笔
收集整理的這篇文章主要介紹了
使用@Async实现异步调用
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
package com.learn.service;import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;@Service
public class User01Service {@Asyncpublic void sedSms() {System.out.println("##sedSms##開(kāi)始執(zhí)行.. 2");for (int i = 0; i < 5; i++) {try {Thread.sleep(1000);} catch (Exception e) {// TODO: handle exception}System.out.println("i:" + i);}System.out.println("##sedSms##結(jié)束執(zhí)行.. 3");}}
package com.learn.controller;import com.learn.service.User01Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;@RestController
public class IndexController {@Autowiredprivate User01Service user01Service;@ResponseBody@RequestMapping("/sedSms")public String sedSms() {System.out.println("###indexController### 1");user01Service.sedSms();System.out.println("###indexController### 4");return "success";}}
package com.learn;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;@EnableAsync
@SpringBootApplication
public class SpringbootQuick2Application {public static void main(String[] args) {SpringApplication.run(SpringbootQuick2Application.class, args);}
}
###indexController### 1
2019-10-27 01:36:58.866 INFO 13208 --- [nio-8080-exec-1]
###indexController### 4
2019-10-27 01:36:58.867 INFO 13208 --- [nio-8080-exec-1] com.learn.app.WebLogAspect : RESPONSE : success
##sedSms##開(kāi)始執(zhí)行.. 2
i:0
i:1
i:2
i:3
i:4
##sedSms##結(jié)束執(zhí)行.. 3
?
總結(jié)
以上是生活随笔為你收集整理的使用@Async实现异步调用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 谷歌浏览器中安装JsonView扩展程序
- 下一篇: apollo分布式配置中心原理