SpringBoot 任務

1、異步任務

在Java應用中,絕大多數狀況下都是經過同步的方式來實現交互處理的;可是在處理與第三方系統交互的時候,容易形成響應遲緩的狀況,以前大部分都是使用多線程來完成此類任務,其實,在Spring 3.x以後,就已經內置了@Async來完美解決這個問題。html

1.啓動器上添加 @EnableAsync 註解spring

@EnableScheduling //開啓註解的定時任務
@SpringBootApplication
public class Springboot04TaskApplication {

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

}

2.service 添加註解安全

@Service
public class AsyncService {
    
    //告訴Spring這是一個異步任務
    @Async
    public void hello(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("處理數據中...");
    }
}

3.controller多線程

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

訪問效果:app

若果沒有開啓異步,訪問 /hello 3秒後,控制檯打印 「處理數據中...」以及返回 「Success」。
開啓後:訪問 /hello 直接返回 「Success」,3秒後控制檯打印返回 「Success」。

2、定時任務

1.啓動器上添加 @EnableScheduling 註解
2.須要執行的方法添加 @Scheduled(cron="")異步

@Service
public class ScheduledService {
    /**
     *    second(秒) , minute(分 ), hour(時), day of month(日), month(月) ,day of week(周幾 ).
     *
     *    0 * * * * MON-FRI
     *    週一到週五,每個月,每日,每時,每分,整秒啓動一次  (週一到週五每分鐘啓動 一次)
     *    * * * * * 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 * * * *  SUN-SAT")
//    @Scheduled(cron = "0,1,2,3,4 * * * *  SUN-SAT") //    枚舉    0,1,2,3,4這些秒會運行
//    @Scheduled(cron = "0-4 * * * *  SUN-SAT") //    區間   0-4 秒都會運行
    @Scheduled(cron = "0/4 * * * *  SUN-SAT") //    步長 每隔4秒啓動一次
    public void hello(){
        System.out.println("hello service  ......");
    }
}

3.cron表達式async

clipboard.png

3、郵件任務

1.配置spring-boot

添加依賴ui

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

yml配置spa

spring:
  mail:
    host: smtp.qq.com
    username: 626532990@qq.com
    password: qfrbjksqyqmgbdgc    # 設置密碼,該處的密碼是QQ郵箱開啓SMTP的受權碼而非QQ密碼
    properties:
      mail:
        smtp:
          auth: true   # 須要驗證登陸名和密碼
        starttls:
          enable: true    # 須要TLS認證 保證發送郵件安全驗證
          required: true

2.發送簡單郵件

@Autowired
    private JavaMailSenderImpl mailSender;

    @Value("${spring.mail.username}")
    public String sendFrom;

    @Test
    public void sendSimpleEmail(){

        SimpleMailMessage message = new SimpleMailMessage();
        //郵件設置
        message.setSubject("通知:今晚通宵敲代碼");
        message.setText("今天19:30加班");

        message.setTo("481345827@qq.com");
        message.setFrom(sendFrom);

        mailSender.send(message);
    }

clipboard.png

clipboard.png

3.發送複雜郵件(帶附件)

@Test
    public void sendComplexEmail() throws Exception{
        //1. 建立一個複雜的消息郵件
        MimeMessage mimeMessage = mailSender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);//打開上傳文件
        //郵件設置
        helper.setSubject("通知:今晚通宵敲代碼(帶附件)");
        helper.setText("<b style='color:red'>今天19:30加班</b>",true); //這段內容是html

        helper.setTo("481345827@qq.com");
        helper.setFrom(sendFrom);

        //上傳文件‪E:\圖片\Camera Roll\73378512_p0.jpg
        helper.addAttachment("1.jpg",new File("E:\\圖片\\Camera Roll\\69073572_p0.jpg"));
        helper.addAttachment("2.jpg",new File("E:\\圖片\\Camera Roll\\73378512_p0.jpg"));

        mailSender.send(mimeMessage);
    }

clipboard.png

clipboard.png

相關文章
相關標籤/搜索