1. 程式人生 > 其它 >非同步、郵件、定時任務

非同步、郵件、定時任務

SpringBoot 之非同步、郵件、定時任務

14. 非同步、郵件、定時任務

14.1 非同步任務

編寫一個業務測試類

檔案路徑:com--dzj--service--AsynService.java

@Service
public class AsynService {

    @Async   //告訴spring這是一個非同步的方法
    public void hello(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("資料正在處理...");
    }
}

在主啟動器類上開啟非同步註解功能

@EnableAsync  //開啟非同步註解功能
@SpringBootApplication

編寫測試介面

@RestController
public class AsynController {

    @Autowired
    AsynService asynService;

    @RequestMapping("/hello")
    public String hello(){
        asynService.hello();//停止三秒才執行,頁面轉圈~
        return "OK";
    }
}

14.2 郵件任務

匯入相關依賴

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

在郵箱設定中開啟POP3/SMTP服務

點選我已傳送,獲得授權碼

編寫相關配置檔案

application.yaml

spring:
  mail:
    username: [email protected]
    password:             #填寫上面的授權碼作為密碼
    host: smtp.qq.com
    properties:
      mail:
        smtp:
          ssl:
            enable: true  # 開啟加密驗證

在測試類中測試郵件傳送

package com.dzj;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@SpringBootTest
class Springboot09AsynchronousApplicationTests {

    @Autowired
    JavaMailSender mailSender;

    @Test
    void contextLoads() {

        //一個簡單郵件的傳送
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setSubject("支付寶到賬80萬元");  //郵件主題
        mailMessage.setText("專案收入");            //郵件正文
        mailMessage.setTo("[email protected]");         //收件人
        mailMessage.setFrom("[email protected]");       //發件人
        mailSender.send(mailMessage);
    }

    @Test
    void contextLoads2() throws MessagingException {
        //一個複雜郵件的傳送,組裝
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        //正文
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
        helper.setSubject("支付寶到賬100萬元");  //郵件主題
        helper.setText("<p style='color:red'>工資收入</p>",true);  //郵件正文
        //附件
        helper.addAttachment("1.jpg",new File("E:\\Good Study\\dengzj.jpg"));
        helper.addAttachment("2.jpg",new File("E:\\Good Study\\dengzj.jpg"));
        helper.setTo("[email protected]");     //收件人
        helper.setFrom("[email protected]");   //發件人
        mailSender.send(mimeMessage);
    }

}

執行測試!

14.3 定時任務

檔案路徑:com--dzj--service--ScheduledService.java

編寫業務測試類

package com.dzj.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
public class ScheduledSercvice {

    @Autowired
    JavaMailSender mailSender;
    /*
        在一個特定的時間執行這個方法
        cron  表示式~ 秒 分 時 日 月 周幾
        cron="10 20 10 * * ?" 每天的10點20分10秒 執行一次
        cron="10 0/5 10,18 * * ?"  每天10點和18點,從第零分鐘開始,每隔五分鐘,第10秒執行一次
        cron="0 15 10 ? * 1-6"   每個月的週一到週六  10點15分執行一次
        cron="0/2 * * * * ?" 每隔兩秒執行一次
     */

    @Scheduled(cron="0/10 * * * * ?")
    public void hello(){
        System.out.println("你被執行了。。。");
    }

    @Scheduled(cron="0/10 * * * * ?")
    public void sendMail(){
        //一個簡單郵件的傳送
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setSubject("支付寶到賬80萬元");
        mailMessage.setText("專案收入");
        mailMessage.setTo("[email protected]");
        mailMessage.setFrom("[email protected]");
        mailSender.send(mailMessage);
        System.out.println("郵件已傳送");
    }
}

在主啟動器類上開啟定時註解功能

@EnableScheduling //開啟定時註解功能
@SpringBootApplication

執行測試!

本文來自部落格園,作者:小公羊,轉載請註明原文連結:https://www.cnblogs.com/aadzj/p/15636856.html