Spring Boot从0开始学的个人笔记10 --任务

一、异步处理

  • @Async:写在service的方法前,表示这个方法要用到异步处理
  • @EnableAsync:写在spring boot启动类前,开启异步处理的功能

AsynService:

@Service
public class AsynService {
    
    

    @Async
    public void hello(){
    
    
        try {
    
    
            Thread.sleep(3000);
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
        System.out.println("数据处理中");
    }
}

AsynController:

@RestController
public class AsynController {
    
    
    @Autowired
    AsynService asynService;

    @GetMapping("/hello")
    public String hello(){
    
    
        asynService.hello();
        return "success";
    }
}

SpringBootTaskApplication:

@EnableAsync
@SpringBootApplication
public class SpringBootTaskApplication {
    
    

    public static void main(String[] args) {
    
    
        SpringApplication.run(SpringBootTaskApplication.class, args);
    }

}

原本,没+异步处理,每次访问http://localhost:8080/hello,都要等3秒才出结果。加了这个,秒数结果

二、定时任务

  • @EnableScheduling:写在spring boot启动类中前,开启定时任务功能
  • @Scheduled:用在service类中的方法前,用来设置定时任务
    • @Scheduled(cron = "xxx")这个xxx是有语法规则的
    • cron的属性值,规定为字符串,这个字符串,规定有6个时间,分别是秒、分、时、day of month、月、day of week,每个时间段之间用空格分开。比如
      "0 * * * * 1-7",表示周一到周日,任何时间段的时候,秒为0的时候,执行一次函数

在这里插入图片描述
在这里插入图片描述

2、一些例子

  • @Scheduled(cron = "0,1,2,3,4 * * * * 1-5"):周一到周五,秒钟为0,1,2,3,4的时候,执行一次代码
  • @Scheduled(cron = "0-5 * * * * 1,2,3,4,5"):同上
  • @Scheduled(cron = "0/4 * * * * 1,2,3,4,5"):从周一到周五,秒钟为0的时候开始,每隔4秒执行一次
  • 0 0/5 14,18 ? * ?:每天14点,18点,每隔5分钟执行一次
  • 0 0 2 ? * 6L:每个月的最后一个星期六凌晨2点执行一次

三、发邮箱

spring boot自带发邮箱功能,没想到吧

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

1、简单的邮件

application.yml:

spring:
  mail:
    username: [email protected]
    password: xxxxxxxxxxxxxxx(这个密码,下面有说是什么)
    host: smtp.qq.com
    profiles:
      mail:
        smtp:
          ssl:
            enable: true
  • 密码不是你的QQ密码,而是
    在这里插入图片描述
    是这个授权码

测试类中测试:

@SpringBootTest
class SpringBootTaskApplicationTests {
    
    

    @Autowired
    JavaMailSenderImpl mailSender;

    //简单的发送邮件
    @Test
    void contextLoads() {
    
    
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("今晚打老虎");//发送邮件标题
        message.setText("多谢乌蝇哥");//正文
        message.setTo("[email protected]");//发给谁
        message.setFrom("[email protected]");//发送者
        this.mailSender.send(message);
    }

}

在这里插入图片描述

2、复杂的邮件

    //复杂的邮件
    @Test
    public void test02() throws Exception {
    
    
        MimeMessage mimeMessage = this.mailSender.createMimeMessage();//要用MimeMessage类型
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);//然后用helper操作
        helper.setSubject("今晚打老虎");//标题
        helper.setText("<b style='color:red'>多谢乌蝇哥</b>", true);//内容,第二个参数为true就开启html格式
        helper.setTo("[email protected]");//发给谁
        helper.setFrom("[email protected]");//发送者
        helper.addAttachment("1.jpg", new File("D:\\壁纸\\59e06a3543101.png"));
        helper.addAttachment("2.jpg", new File("D:\\壁纸\\336-160G2110S6.png"));
        this.mailSender.send(mimeMessage);
    }

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/yi742891270/article/details/107793865