Spring Boot笔记-Spring Boot与任务(十二)

新建Spring Boot项目,加入Spring Web依赖。

1.异步任务

编写AsyncController.java,AsyncService.java。

package com.atguigu.task.controller;

import com.atguigu.task.service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AsyncController {
    @Autowired
    AsyncService asyncService;

    @GetMapping("/async")
    public String async() {
        asyncService.async();
        return "success";
    }
}
package com.atguigu.task.service;

import org.springframework.stereotype.Service;

@Service
public class AsyncService {
    public void async() {
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("正在处理……");
    }
}

启动项目,访问http://localhost:8080/async,我们等待了10秒钟后,看到了页面上的success。此时,我希望async()方法能在后台运行,访问http://localhost:8080/async的时候,立刻返回success,应该如何实现呢?

  1. 在主启动类上加上@EnableAsync注解,告诉Spring Boot开启异步功能。
  2. 在需要异步执行的方法上,加上@Async注解,此时,这个方法就是异步执行了,后台会重启一个线程来跑这个任务。

配置好之后,重启项目,访问http://localhost:8080/async,可以看到页面立刻出现了success,等了10秒后,在控制台打印出了信息。

2.定时任务

要想开启定时任务,需要在Spring Boot的主程序上加上@EnableScheduling注解。

package com.atguigu.task.service;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
public class ScheduleService {
    @Scheduled(cron = "0 * * * * *")
    public void schedule() {
        System.out.println("定时任务执行了……");
    }
}

重点就在cron表达式上。cron表达式由6部分组成,空格分隔,分别代表秒、分、时、日期、月份、星期。

字段允许值允许的特殊字符
0-59, - * /
0-59, - * /
0-23, - * /
日期1-31, - * ? / L W C
月份1-12, - * /
星期0-7或SUN-SAT,其中0和7都代表SUN, - * ? / L C #
特殊字符含义
,枚举
-区间
*任意
/步长
?日期、星期冲突匹配
L最后
W工作日
C和Calendar联系后计算过的值
#星期,4#2代表第二个星期四

 3.邮件任务

加入mail的starter。添加yml配置,如果提示发送失败,开启ssl,即yml的最后一行。

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

查看MailProperties.java文件,可以通过指定spring.mail前缀来指定邮箱的参数,默认的protocol是smtp。这里以QQ邮箱向163邮箱发送为例。首先登陆QQ邮箱,点击设置,找到“账户”标签,找到下面的“POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务”,我们把SMTP服务的开启,需要发送短信,此时,我们可以获取到一个授权码,将其复制下来,一会儿要用。这里我就用*代替了。

spring:
  mail:
    username: 872452093@qq.com
    password: ****************
    host: smtp.qq.com
    # properties: {"mail.smtp.ssl.enable": "true"}

编写测试类测试效果。

package com.atguigu.task;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMailMessage;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.internet.MimeMessage;
import java.io.File;

@SpringBootTest
class SpringBootTask04ApplicationTests {
    @Autowired
    JavaMailSenderImpl javaMailSender;

    // 发送简单邮件
    @Test
    public void sendMail() {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setSubject("通知-明天放假");
        simpleMailMessage.setText("明天加班233");
        simpleMailMessage.setTo("shyw1995@163.com");
        simpleMailMessage.setFrom("872452093@qq.com");
        javaMailSender.send(simpleMailMessage);
    }

    // 发送复杂邮件
    @Test
    public void sendMimeMail() throws Exception {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
        mimeMessageHelper.setSubject("通知-明天放假");
        mimeMessageHelper.setText("<b style='color: red'>明天加班233</b>", true);
        mimeMessageHelper.setTo("shyw1995@163.com");
        mimeMessageHelper.setFrom("872452093@qq.com");
        File file = new File("C:\\Users\\WangShaoYang\\Desktop\\CR400AF.jpg");
        mimeMessageHelper.addAttachment("CR400AF.jpg", file);
        javaMailSender.send(mimeMessage);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值