任务

异步任务

  1. 通过@Async注解告诉SpringBoot此方法是一个异步方法,SpringBoot在运行时,会自动帮我们开启一个线程池

    @Service
    public class AsyncService {
       
        @Async
        public void hello(){
       
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
       
            System.out.println("------hello-----");
        }
    }
    
  2. 在启动类上开启异步功能

    @EnableAsync
    @SpringBootApplication
    public class TasksApplication {
       
        public static void main(String[] args) {
            SpringApplication.run(TasksApplication.class, args);
        }
       
    }
    
  3. Controller层

    @RestController
    public class AsyncController {
        @Autowired
        AsyncService asyncService;
       
        @RequestMapping("/hello")
        public String hello(){
            asyncService.hello();
            return "OK";
        }
    }
    

    没开启异步功能时,在浏览器访问该接口,需要等待3秒;开启后,浏览器会直接得到返回值OK,后台则会开启另一个线程去执行hello方法

定时任务

  1. 启动类加注解@EnableScheduling;开启定时任务功能

  2. Service方法上加@Scheduled注解

    @Scheduled
    public void hello(){
        System.out.println("---hello---");
    }
    
  3. 写cron表达式

    @Scheduled(cron = "30 15 10 * * ?")
    public void hello(){
        System.out.println("---hello---");
    }
    
  4. 关于cron表达式,举几个例子:

    每隔5秒执行一次:”*/5 * * * * ?”

    每隔1分钟执行一次:”0 */1 * * * ?”

    每天23点执行一次:”0 0 23 * * ?”

    每天凌晨1点执行一次:”0 0 1 * * ?”

    每月1号凌晨1点执行一次:”0 0 1 1 * ?”

    每月最后一天23点执行一次:”0 0 23 L * ?”

    每周星期天凌晨1点实行一次:”0 0 1 ? * L”

    在26分、29分、33分执行一次:”0 26,29,33 * * * ?”

    每天的0点、13点、18点、21点都执行一次:”0 0 0,13,18,21 * * ?”

    表示在每月的1日的凌晨2点调度任务:”0 0 2 1 * ? *”

    表示周一到周五每天上午10:15执行作业:”0 15 10 ? * MON-FRI”

    表示2002-2006年的每个月的最后一个星期五上午10:15执行:”0 15 10 ? 6L 2002-2006”

  5. cron表达式必须有6项内容

邮件发送

  1. 确保要发送邮箱开启POP3/SMTP功能

  2. pom中引入邮件启动项

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
    
  3. properties中配置spring.mail.username、spring.mail.password、spring.mail.host;一些邮箱还需要开启加密验证

  4. 一个邮件发送的流程

    @SpringBootTest
    class TasksApplicationTests {
        //1、注入JavaMailSenderImpl
        @Autowired
        JavaMailSenderImpl mailSender;
       
        @Test
        void testMail() {
            //2、创建SimpleMailMessage实例
            SimpleMailMessage mailMessage = new SimpleMailMessage();
       
            //邮件主题、内容
            mailMessage.setSubject("Mail Subject");
            mailMessage.setText("Mail Text");
       
            //邮件发给谁、从哪里发;这里测试,自己给自己发
            mailMessage.setTo("371134638@qq.com");
            mailMessage.setFrom("371134638@qq.com");
       
            //3、发送邮件
            mailSender.send(mailMessage);
        }
    }
    
  5. 复杂邮件的发送

    @Test
    void testMail2() throws MessagingException {
        //创建MimeMessage用于发送复杂文件
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        //组装一个MimeMessageHelper
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
       
        //用help组装邮件
        helper.setSubject("Mail Subject");
        //开启html识别
        helper.setText("<p style='color:red'>Mail Text</p>", true);
        //增加附件
        helper.addAttachment("1.jpg",
                new File("F:/照片/Saved Pictures/5cd3a0aca8fc0.jpg"));
       
        //通过helper发送
        helper.setTo("371134638@qq.com");
        helper.setFrom("371134638@qq.com");
       
        mailSender.send(mimeMessage);
    }