任务
14.1异步任务
所谓异步,在某些功能实现时可能要花费一定的时间,但是为了不影响客户端的体验,选择异步执行
案例:
首先创建一个service:
@Service
public class AsyncService {
    public void hello(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("数据正在传输...");
    }
}
 
Controller:
@RestController
public class AsyncController {
    @Autowired
    AsyncService asyncService;
    @RequestMapping("/async")
    public String async(){
        asyncService.hello();
        return "ok";
    }
}
 
这样在执行/async请求时,网站会延时三秒再显示ok,后台数据也会三秒后显示数据正在传输

现在想要做到前端快速响应我们的页面,后台去慢慢的传输数据,就要用到springboot自带的功
①想办法告诉spring我们的异步方法是异步的,所以要在方法上添加注解
@Async
public void hello(){
    try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("数据正在传输...");
}
 
②去springboot主程序中开启异步注解功能
@EnableAsync
@SpringBootApplication
public class SwaggerDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(SwaggerDemoApplication.class, args);
    }
}
 
重启即可
14.2邮件任务
邮件发送,在我们的日常开发中,也非常的多,Springboot也帮我们做了支持
- 邮件发送需要引入spring-boot-start-mail
 - SpringBoot 自动配置MailSenderAutoConfiguration
 - 定义MailProperties内容,配置在application.yml中
 - 自动装配JavaMailSender
 - 测试邮件发送
 
测试:
1、引入pom依赖
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-mail</artifactId>
</dependency>
 
看它引入的依赖,可以看到 jakarta.mail
<dependency>
   <groupId>com.sun.mail</groupId>
   <artifactId>jakarta.mail</artifactId>
   <version>1.6.4</version>
   <scope>compile</scope>
</dependency>
 
2、查看自动配置类:MailSenderAutoConfiguration

ok我们点进去,看到里面存在bean,JavaMailSenderImpl

然后我们去看下配置文件
@ConfigurationProperties(
   prefix = "spring.mail"
)
public class MailProperties {
   private static final Charset DEFAULT_CHARSET;
   private String host;
   private Integer port;
   private String username;
   private String password;
   private String protocol = "smtp";
   private Charset defaultEncoding;
   private Map<String, String> properties;
   private String jndiName;
}
 
3、配置文件:
spring.mail.username=你的邮箱
spring.mail.password=你的邮箱授权码
spring.mail.host=smtp.163.com
# qq需要配置ssl,其他邮箱不需要
spring.mail.properties.mail.smtp.ssl.enable=true
 
首先你需要去邮箱网站开启POP3/SMTP

4、Spring单元测试
@SpringBootTest
class SwaggerDemoApplicationTests {
    @Autowired
    JavaMailSenderImpl mailSender;
    @Test
    public void contextLoads() {
        //邮件设置1:一个简单的邮件
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("这是一个测试邮件发送标题");
        message.setText("这是一个测试邮件发送内容");
        message.setTo("15235771906@163.com");
        message.setFrom("15235771906@163.com");
        mailSender.send(message);
    }
    
}
 
发送一个较为复杂的邮件:
@Test
public void contextLoads2() throws MessagingException {
   //邮件设置2:一个复杂的邮件
   MimeMessage mimeMessage = mailSender.createMimeMessage();
   MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
   helper.setSubject("通知-明天来狂神这听课");
   helper.setText("<b style='color:red'>今天 7:30来开会</b>",true);
   //发送附件
   helper.addAttachment("1.jpg",new File("绝对路径地址"));
   helper.addAttachment("2.jpg",new File(""));
   helper.setTo("15235771906@163.com");
   helper.setFrom("15235771906@163.com");
   mailSender.send(mimeMessage);
}
 
查看邮箱,邮件接收成功!
我们只需要使用Thymeleaf进行前后端结合即可开发自己网站邮件收发功能了!
14.3定时任务
项目开发中经常需要执行一些定时任务,比如需要在每天凌晨的时候,分析一次前一天的日志信息,Spring为我们提供了异步执行任务调度的方式,提供了两个接口。
- TaskExecutor接口
 - TaskScheduler接口
 
两个注解:
- @EnableScheduling
 - @Scheduled
 
cron表达式:

测试步骤:
1、创建一个ScheduledService
我们里面存在一个hello方法,他需要定时执行,怎么处理呢?
@Service
public class ScheduledService {
   
   //秒   分   时     日   月   周几
   //0 * * * * MON-FRI
   //注意cron表达式的用法;
   @Scheduled(cron = "0 * * * * 0-7")
   public void hello(){
       System.out.println("hello.....");
  }
}
 
2、这里写完定时任务之后,我们需要在主程序上增加@EnableScheduling 开启定时任务功能
@EnableAsync //开启异步注解功能
@EnableScheduling //开启基于注解的定时任务
@SpringBootApplication
public class SpringbootTaskApplication {
   public static void main(String[] args) {
       SpringApplication.run(SpringbootTaskApplication.class, args);
  }
}
 
3、我们来详细了解下cron表达式;
http://www.bejson.com/othertools/cron/4、常用的表达式
4.常用表达式
(1)0/2 * * * * ?   表示每2秒 执行任务
(1)0 0/2 * * * ?   表示每2分钟 执行任务
(1)0 0 2 1 * ?   表示在每月的1日的凌晨2点调整任务
(2)0 15 10 ? * MON-FRI   表示周一到周五每天上午10:15执行作业
(3)0 15 10 ? 6L 2002-2006   表示2002-2006年的每个月的最后一个星期五上午10:15执行作
(4)0 0 10,14,16 * * ?   每天上午10点,下午2点,4点
(5)0 0/30 9-17 * * ?   朝九晚五工作时间内每半小时
(6)0 0 12 ? * WED   表示每个星期三中午12点
(7)0 0 12 * * ?   每天中午12点触发
(8)0 15 10 ? * *   每天上午10:15触发
(9)0 15 10 * * ?     每天上午10:15触发
(10)0 15 10 * * ?   每天上午10:15触发
(11)0 15 10 * * ? 2005   2005年的每天上午10:15触发
(12)0 * 14 * * ?     在每天下午2点到下午2:59期间的每1分钟触发
(13)0 0/5 14 * * ?   在每天下午2点到下午2:55期间的每5分钟触发
(14)0 0/5 14,18 * * ?     在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
(15)0 0-5 14 * * ?   在每天下午2点到下午2:05期间的每1分钟触发
(16)0 10,44 14 ? 3 WED   每年三月的星期三的下午2:10和2:44触发
(17)0 15 10 ? * MON-FRI   周一至周五的上午10:15触发
(18)0 15 10 15 * ?   每月15日上午10:15触发
(19)0 15 10 L * ?   每月最后一日的上午10:15触发
(20)0 15 10 ? * 6L   每月的最后一个星期五上午10:15触发
(21)0 15 10 ? * 6L 2002-2005   2002年至2005年的每月的最后一个星期五上午10:15触发
(22)0 15 10 ? * 6#3   每月的第三个星期五上午10:15触发
 
三月的星期三的下午2:10和2:44触发
 (17)0 15 10 ? * MON-FRI 周一至周五的上午10:15触发
 (18)0 15 10 15 * ? 每月15日上午10:15触发
 (19)0 15 10 L * ? 每月最后一日的上午10:15触发
 (20)0 15 10 ? * 6L 每月的最后一个星期五上午10:15触发
 (21)0 15 10 ? * 6L 2002-2005 2002年至2005年的每月的最后一个星期五上午10:15触发
 (22)0 15 10 ? * 6#3 每月的第三个星期五上午10:15触发











![CSP/信奥赛C++刷题训练:经典例题 - 栈(2):洛谷P1981 :[NOIP2013 普及组] 表达式求值](https://i-blog.csdnimg.cn/direct/f89c03d1fa7140cd87a41e8cead94e91.png#pic_center)







