SpringBoot-任务

任务

1. 异步任务

1. 在需要异步的方法上添加注解

package com.wang.service;

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

@Service
public class AsyncService {

    //告诉Spring这是异步的方法(多线程)
    @Async
    public void hello() {

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("数据正在处理......");

    }
}

@Async

2. 在main方法上开启异步功能

package com.wang;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;

@SpringBootApplication
//开启异步方法的注解
@EnableAsync
public class Springboot07TaskApplication {

    public static void main(String[] args) {
        SpringApplication.run(Springboot07TaskApplication.class, args);
    }

}

@EnableAsync

效果: 跳转不会受到异步任务的影响

2. 邮件任务

1. 添加依赖

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

2. 配置文件

spring:
  mail:
    username: 715180879@qq.com
    password: XXXXXX
    host: smtp.qq.com
    #开启加密验证(QQ邮箱要求) mail.smtp.ssl.enable: true
    properties:
      mail:
        smtp:
          ssl:
            enable: true

3. 邮件发送

在SpringBoot中, 邮件发送已经被封装成JavaMailSenderImpl

1. 简单的邮件发送

@SpringBootTest
class Springboot07TaskApplicationTests {

    //在SpringBoot中JavaMailSenderImpl封装了邮件发送的方法, 自动装配即可使用
    @Autowired
    JavaMailSenderImpl mailSender;

    @Test
    void contextLoads() {

        //一个简单的邮件 (只有文字)
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();

        //标题
        simpleMailMessage.setSubject("Hello My QQ Mail!");
        //正文
        simpleMailMessage.setText("这里是正文!");
        //设置收发地址
        simpleMailMessage.setTo("715180879@qq.com");
        simpleMailMessage.setFrom("715180879@qq.com");

        //发送邮件
        mailSender.send(simpleMailMessage);
    }

}

2. 复杂邮件的发送

@SpringBootTest
class Springboot07TaskApplicationTests {

    //在SpringBoot中JavaMailSenderImpl封装了邮件发送的方法, 自动装配即可使用
    @Autowired
    JavaMailSenderImpl mailSender;

    @Test
    void contextLoads2() throws MessagingException {

        //一个复杂的邮件, 可以通过mailSender的createMimeMessage方法创建一个MimeMessage
        MimeMessage message = mailSender.createMimeMessage();

        //组装, 利用SpringBoot提供的 MimeMessageHelper, boolean可以设置是否支持多文件
        MimeMessageHelper helper = new MimeMessageHelper(message,true);

        helper.setSubject("Hello My QQ Mail! Again!");
        //设置True支持Html标签
        helper.setText("<p style='color:red'>这里是正文!</p>",true);

        //附件, 使用绝对路径
        helper.addAttachment("pic.jpg", new File("C:\Users\Wang\Pictures\Saved Pictures\pic.jpg"));

        //设置收发地址
        helper.setTo("715180879@qq.com");
        helper.setFrom("715180879@qq.com");

        mailSender.send(message);
    }

}

3. 邮件发送功能的封装与测试

@SpringBootTest
class Springboot07TaskApplicationTests {

    //在SpringBoot中JavaMailSenderImpl封装了邮件发送的方法, 自动装配即可使用
    @Autowired
    JavaMailSenderImpl mailSender;

    /**
     *
     * @param html : whether enable use html in text
     * @param multipart : whether enable send multiFiles
     * @param Subject : title of the mail
     * @param text : content of the mail
     * @param fileName : name for the attachment files
     * @param filePath : path for the attachment files, use abstract path
     * @param to : send the mail to someone
     * @param from : the mail from someone
     * @author Wang Sky
     */
    public void sendMail(Boolean html, Boolean multipart, String Subject, String text, String[] fileName,
                         String[] filePath, String to, String from) throws MessagingException {
        //一个复杂的邮件, 可以通过mailSender的createMimeMessage方法创建一个MimeMessage
        MimeMessage message = mailSender.createMimeMessage();

        //组装, 利用SpringBoot提供的 MimeMessageHelper, boolean可以设置是否支持多文件
        MimeMessageHelper helper = new MimeMessageHelper(message,multipart);

        helper.setSubject(Subject);
        //设置True支持Html标签
        helper.setText(text, html);

        //附件, 使用绝对路径
        for (int i = 0; i < fileName.length; i++) {
            helper.addAttachment(fileName[i], new File(filePath[i]));
        }

        //设置收发地址
        helper.setTo(to);
        helper.setFrom(from);

        mailSender.send(message);
    }

    @Test
    void contextLoads3() throws MessagingException {

        String subject = "Hello My QQ Mail! Again!";
        String text = "<p style='color:red'>这里是正文!</p>";
        String fileName1 = "pic.jpg";
        String filePath1 = "C:\Users\Wang\Pictures\Saved Pictures\pic.jpg";
        String[] fileName = {fileName1};
        String[] filePath = {filePath1};
        String to = "715180879@qq.com";
        String from = "715180879@qq.com";

        sendMail(true, true, subject, text, fileName, filePath, to, from);
    }

}

3. 定时任务

两个核心接口

  • TaskScheduler 任务调度
  • TaskExecutor 任务执行

核心注解

  • @EnableScheduling 放在main方法上
  • @Scheduled 表示什么时候执行, 在定时任务的方法上

利用cron表达式设定执行时间

package com.wang.service;

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

@Service
public class ScheduleService {

    //在一个特定的时间执行这个方法
    //cron表达式   sec min hour day month week     每天的10:43:00执行一次
    @Scheduled(cron = "0 43 10 * * ?")
    public void hello() {
        System.out.println("hello, 你被执行了!");
    }
}
原文地址:https://www.cnblogs.com/wang-sky/p/13738274.html