Springboot 开启异步任务Async,邮件发送任务,定时任务

异步任务

1.主启动类开启异步注解

 

2.service目录下开启异步任务注解

复制代码
@Service
public class AsyncService {
    @Async//异步任务注解的标志
    public  void  hello(){
        try {
            Thread.sleep(3000);
        }
        catch (InterruptedException e){
            e.printStackTrace();
        }
        System.out.println("数据正在处理中...");
    }
}
复制代码

 

3.controller目录下的方法调用service目录下的方法

复制代码
@RestController
public class AsyncController {
    @Autowired
    AsyncService asyncService;
    @RequestMapping("/h")
    public String hello(){
        asyncService.hello();//停止3秒
        return  "ok";
    }
}
复制代码

 

4.运行结果

 

 

Springboot邮件发送简单任务

1.导入依赖并配置properties文件

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-mail -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>2.6.3</version>
</dependency>

 

 

2.测试代码

 

 

3.运行结果

 4.实现复杂的邮件发送

代码

 运行结果:

 定时任务

 1.主启动类开启定时任务的注解

 

2.测试

 运行结果:

 

posted on   醒醒起来  阅读(111)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示