Python 官方文档:入门教程 => 点击学习
02: 定时任务 03: 邮件任务 一、SpringBoot--异步任务 1.1 什么是同步和异步 同步是阻塞模式,异步是非阻塞模式。 同步就是指一个进
02: 定时任务
03: 邮件任务
AsyncService.java
package com.tian.asyncdemo.service;
import org.springframework.stereotype.Service;
@Service
public class AsyncService {
public void hello() {
try {
System.out.println("数据正在处理");
Thread.sleep(3000);
System.out.println("数据处理完成");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
AsyncController.java
package com.tian.asyncdemo.controller;
import com.tian.asyncdemo.service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.WEB.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AsyncController {
@Autowired
AsyncService asyncService;
@RequestMapping("/hello")
public String hello() {
asyncService.hello();
return "OK";
}
}
运行结果:
在Service的方法中使用@Async说这是一个异步方法,并在主入口上使用@EnableAsync开启异步支持
AsyncService.java
@Service
public class AsyncService {
@Async
public void hello() {
try {
System.out.println("数据正在处理");
Thread.sleep(3000);
System.out.println("数据处理完成");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
主入口上使用@EnableAsync开启异步支持
再次测试:
到此这篇关于Springboot任务之异步任务的使用详解的文章就介绍到这了,更多相关SpringBoot异步任务内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: Springboot任务之异步任务的使用详解
本文链接: https://www.lsjlt.com/news/127755.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0