美文网首页Java 杂谈JavaJava学习之路
Springboot与任务(异步任务、定时任务、邮件任务)

Springboot与任务(异步任务、定时任务、邮件任务)

作者: 椰子奶糖 | 来源:发表于2019-07-17 16:41 被阅读28次

    SpringBoot与任务

    异步任务

    • 注解@Async //告诉spring这是一个异步方法,spring会自己开一个线程池对其进行调用

    • 注解@EnableAsync //加在主类上,表示开启异步注解功能

      • 测试:

      • service

          package com.atguigu.task.service;
        
          import org.springframework.scheduling.annotation.Async;
          import org.springframework.stereotype.Service;
        
          /**
           * Created by CHEN on 2019/7/17.
           */
          @Service
          public class AsynService {
        
        
              //告诉Spring这是异步方法,它会自己开一个线程池进行调用
              @Async
              public void hello(){
        
                  try {
                      Thread.sleep(3000);
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
                  System.out.println("处理数据中");
              }
        
          }
        
      • controller

             package com.atguigu.task.controller;
        
             import com.atguigu.task.service.AsynService;
             import com.sun.net.httpserver.Authenticator;
             import org.springframework.beans.factory.annotation.Autowired;
             import org.springframework.web.bind.annotation.GetMapping;
             import org.springframework.web.bind.annotation.RestController;
        
             /**
              * Created by CHEN on 2019/7/17.
              */
             @RestController
             public class AsyncController {
        
                 @Autowired
                 AsynService asynService;
        
        
                 @GetMapping("/hello")
                 public String hello() {
        
                     asynService.hello();
                     return "success";
                 }
        
        
             }
        
    • 总的来说就是加注解,告诉spring这是异步,让它来帮你搞定实现异步的代码。


    定时任务

    • @Scheduled(cron = "* * * * * * ")//告诉spring这是定时任务,规则写在cron中
    • @EnableScheduling//开启定时任务
      • 测试:
      • service
            package com.atguigu.task.service;
    
            import org.springframework.scheduling.annotation.Scheduled;
            import org.springframework.stereotype.Service;
    
            import javax.xml.transform.Source;
    
            /**
             * Created by CHEN on 2019/7/17.
             */
            @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 * * * * MON-SAT")
                //@Scheduled(cron = "0,1,2,3,4 * * * * MON-SAT")
                //@Scheduled(cron = "0-4 * * * * MON-SAT")
                //@Scheduled(cron = "0/4 * * * * MON-SAT")  //每4秒执行一次
    
                @Scheduled(cron = "* * * * * * ")//这个表示每秒执行一次
                public void hello(){
                    System.out.println("hello");
                }
            }
    
    • cron表达式:

    • image.png
    • image.png

    邮件任务

    • 邮件发送需要引入spring-boot-starter-mail

    • Spring Boot 自动配置MailSenderAutoConfiguration

    • 定义MailProperties内容,配置在application.yml中

    • 自动装配JavaMailSende

    • 授权码的获取

    • 授权码的获取
    • 测试简单邮件

        //配置
        spring.mail.username=***********
        spring.mail.password=授权码
        //qq邮箱服务器地址
        spring.mail.host=smtp.qq.com
    
             @Autowired
            JavaMailSenderImpl mailSender;
    
    
            @Test
            public void contextLoads() {
                SimpleMailMessage message = new SimpleMailMessage();
                message.setSubject("邮件标题");
                message.setText("邮件内容");
                message.setTo("接收者邮箱");
                message.setFrom("******发送者邮箱");
                mailSender.send(message);
            }
    
    • 发送结果
    • 如果报530(ssl)错误则在配置中开启即可:

      • spring.mail.properties.mail.smtp.ssl.enable=true
    • 测试复杂邮件
        @Test
            public void contextLoads2() throws MessagingException {
                //复杂邮件
                MimeMessage message = mailSender.createMimeMessage();
                //是否上传附件
                MimeMessageHelper helper=new MimeMessageHelper(message,true);
    
                helper.setSubject("邮件标题");
                
                //setText有一个隐藏属性,html,布尔值,true表示支持html
                helper.setText("<b style='color:red'>兼容html内容</b>",true);
                //可以上传附件,理论上任何格式
                helper.addAttachment("QAQ.png",new File("QAQ.png"));
                helper.setTo("目的邮箱");
                helper.setFrom("发送者邮箱");
    
                mailSender.send(message);
            }
    
    发送结果

    相关文章

      网友评论

        本文标题:Springboot与任务(异步任务、定时任务、邮件任务)

        本文链接:https://www.haomeiwen.com/subject/pohclctx.html