SpringBoot之异步任务、定时任务、邮件任务

SpringBoot与任务

一、异步任务:

  1. 开启异步注解:

    @EnableAsync  //开启异步注解功能
    

  2. 使用@Async,诉Spring这是一个异步方法

    @Async
    public void hello(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("处理数据中...");
    }
    //直接输入,程序不会顺序执行
    

二、定时任务:

  1. 开启基于注解的定时任务

    @EnableScheduling //开启基于注解的定时任务
    
  2. 使用@Scheduled来规定什么时间执行:

    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Service;
    
    @Service
    public class ScheduledService {
    
        /**
         * second(秒), minute(分), hour(时), day of month(日), month(月), day of week(周几).
         * 0 * * * * MON-FRI周一到周五
         *  【0 0/5 14,18 * * ?】 每天14点整,和18点整,每隔5分钟执行一次
         *  【0 15 10 ? * 1-6】 每个月的周一至周六10:15分执行一次
         *  【0 0 2 ? * 6L】每个月的最后一个周六凌晨2点执行一次
         *  【0 0 2 LW * ?】每个月的最后一个工作日凌晨2点执行一次
         *  【0 0 2-4 ? * 1#1】每个月的第一个周一凌晨2点到4点期间,每个整点都执行一次;
         */
       
        @Scheduled(cron = "0/4 * * * * MON-SAT")  //
        public void hello(){
            System.out.println("hello ... ");
        }
    }
    
    
  3. cron表达式规则:

在这里插入图片描述

三、邮件任务:

  1. 添加依赖:

    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
    
  2. 配置资源文件:(发送者的信息)

    spring.mail.username=********@qq.com
    spring.mail.password=*******
    spring.mail.host=smtp.qq.com
    spring.mail.properties.mail.smtp.ssl.enable=true
    

    .password不是邮箱的登录密码,而是一个授权码。QQ邮箱的授权码可在 设置—账户—开启服务下进行生成,如下图所示:
    在这里插入图片描述

  3. 测试:(简单的邮件)

    @Test
    public void contextLoads() {
       SimpleMailMessage message = new SimpleMailMessage();
       //邮件设置
       message.setSubject("通知-今晚开会");
       message.setText("今晚7:30开会");
    
       message.setTo("15911613834@163.com");//可设置多个
       message.setFrom("940404240@qq.com");
    
       mailSender.send(message);
    }
    
  4. 测试发送复杂的邮件

    @Test
    public void test02() throws  Exception{
       //1、创建一个复杂的消息邮件
       MimeMessage mimeMessage = mailSender.createMimeMessage();
       MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
    
       //邮件设置
       helper.setSubject("通知-今晚开会");
       helper.setText("<b style='color:red'>今天 7:30 开会</b>",true);
    
       helper.setTo("17512080612@163.com");
       helper.setFrom("534096094@qq.com");
    
       //上传文件
       helper.addAttachment("1.jpg",new File("C:\\Users\\lfy\\Pictures\\Saved Pictures\\1.jpg"));
       helper.addAttachment("2.jpg",new File("C:\\Users\\lfy\\Pictures\\Saved Pictures\\2.jpg"));
    
       mailSender.send(mimeMessage);
    
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值