异步任务
程序员文章站
2022-06-12 20:35:35
...
Spring Boot异步任务
- 启动程序加上@EnableAsync注解
- 方法上面加上@Async注解
代码:
@EnableAsync
@SpringBootApplication
public class Springboot4TaskApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot4TaskApplication.class, args);
}
}
@RestController
public class AsyncController
@Autowired
AsyncService asyncService;
@GetMapping("/hello")
public String hello(){
asyncService.hello();
return "success";
}
}
@Service
public class AsyncService {
@Async
public void hello(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("format data ....");
}
}
上一篇: 异步任务
下一篇: 催人泪下的14个最出人意料的微小说