激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - 詳解Springboot中的異步、定時、郵件任務(wù)

詳解Springboot中的異步、定時、郵件任務(wù)

2022-03-09 00:46Rk.. Java教程

這篇文章主要介紹了Springboot中的異步、定時、郵件任務(wù),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧

一、異步任務(wù)

1、編寫一個類AsyncService

異步處理還是非常常用的,比如我們在網(wǎng)站上發(fā)送郵件,后臺會去發(fā)送郵件,此時前臺會造成響應(yīng)不動,直到郵件發(fā)送完畢,響應(yīng)才會成功,所以我們一般會采用多線程的方式去處理這些任務(wù)。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.rk.service;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class AsyncService {
    public void hello(){
        try {
            System.out.println("數(shù)據(jù)處理中~");
            Thread.sleep(3000);//停止三秒
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

2、編寫一個AsyncController類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.rk.controller;
import com.rk.service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AsyncController {
    @Autowired
    AsyncService asyncService;
    @GetMapping("/hello")
    public String hello(){
        asyncService.hello();
        return "success";
    }
}

現(xiàn)在啟動項目進行測試,三秒后才會出現(xiàn)success,現(xiàn)在還不是異步

3、開啟異步

?
1
2
3
4
5
6
7
8
9
@Async//告訴spring這是一個異步方法
   public void hello(){
       try {
           System.out.println("數(shù)據(jù)處理中~");
           Thread.sleep(3000);//停止三秒
       } catch (InterruptedException e) {
           e.printStackTrace();
       }
   }

 

?
1
2
3
4
5
6
7
@EnableAsync//開啟異步注解功能
@SpringBootApplication
public class Springboot09TestApplication {
    public static void main(String[] args) {
        SpringApplication.run(Springboot09TestApplication.class, args);
    }
}

二、郵件任務(wù)

1、引入依賴

?
1
2
3
4
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-mail</artifactId>
 </dependency>

2、配置mail

?
1
2
3
4
5
6
7
8
#用戶名
spring.mail.username=1624603357@qq.com
#密碼
spring.mail.password=yblyxhvmnsurbbci
#發(fā)送郵件服務(wù)器
spring.mail.host=smtp.qq.com
#開啟加密驗證 ssl
spring.mail.properties.mail.smtp.ssl.enable=true

3、測試

簡單郵件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Autowired
JavaMailSenderImpl mailSender;
 
 
 @Test
void contextLoads() {
    SimpleMailMessage mailMessage = new SimpleMailMessage();
    mailMessage.setSubject("你好,rk");//郵件標(biāo)題
    mailMessage.setText("測試郵件");//郵件內(nèi)柔
    mailMessage.setTo("r1624603357@126.com");//收件人郵箱
    mailMessage.setFrom("1624603357@qq.com");//發(fā)件人郵箱
    mailSender.send(mailMessage);
 
}

詳解Springboot中的異步、定時、郵件任務(wù)

復(fù)雜郵件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@Test
void contextLoads2() throws MessagingException {
    //一個復(fù)雜的郵件
    MimeMessage mimeMessage = mailSender.createMimeMessage();
 
    //組裝
    MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
 
    //正文
    helper.setSubject("你好,rk");
    helper.setText("<h1 style='color:red'>測試郵件</h1>",true);
 
    //附件
    helper.addAttachment("1.png",new File("D:\\QLDownloadGame\\2\\1.png"));
    helper.addAttachment("rk.docx",new File("E:\\桌面\\rk.docx"));
 
    // 發(fā)/收件人
    helper.setTo("r1624603357@126.com");
    helper.setFrom("1624603357@qq.com");
 
    //發(fā)送
    mailSender.send(mimeMessage);
 
}

詳解Springboot中的異步、定時、郵件任務(wù)

三、定時任務(wù)

1、編寫一個ScheduledService類

?
1
2
3
4
5
6
7
8
9
10
11
@Service
public class ScheduledService {
    //秒 分 時 日 月 周幾
    //0 * * * * MON-FRI
    //注意cron表達(dá)式的用法;   每天20:28 0秒執(zhí)行該方法
    @Scheduled(cron = "0 28 20 * * 0-7")
    public void hello(){
        System.out.println("現(xiàn)在是20:28");
        System.out.println("hello.....");
    }
}

項目啟動后每天20:28:00執(zhí)行hello方法

2、添加注解

?
1
2
3
4
5
6
7
8
@EnableAsync//開啟異步注解功能
@EnableScheduling//開啟定時功能注解
@SpringBootApplication
public class Springboot09TestApplication {
    public static void main(String[] args) {
        SpringApplication.run(Springboot09TestApplication.class, args);
    }
}

cron表達(dá)式練習(xí)

/*
【0 0/5 14,18 * * ?】每天14點整和18點整,每隔5分鐘執(zhí)行一次
【0 15 10 ? * 1-6】每個月的周一-周六10:15分執(zhí)行一次
【0 0 2 ? * 6L】每個月的最后一個周六凌晨2點執(zhí)行一次
【0 0 2 LW * ?】每個月的最后一個工作日凌晨2點執(zhí)行一次
【0 0 2-4 ? * 1#1】每個月的第一個周一凌晨2點到4點期間,每個整點都執(zhí)行一次
*/

到此這篇關(guān)于Springboot的異步、定時、郵件任務(wù)的文章就介紹到這了,更多相關(guān)Springboot異步定時郵件任務(wù)內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!

原文鏈接:https://blog.csdn.net/m0_46979453/article/details/121082374

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 999久久久国产999久久久 | 欧美一级爱爱 | 亚洲精品成人久久 | 色播av在线 | av在线视 | 国产色91 | 日韩深夜视频 | 免费久久久久久 | 亚洲最大中文字幕 | 亚洲成人欧美在线 | 12av毛片| 九九热精品免费视频 | 国产精品亚洲一区二区三区在线观看 | 亚洲成人第一区 | 午夜爽爽爽男女免费观看hd | 日韩毛片在线看 | 九九热在线视频免费观看 | 成人午夜在线免费观看 | 色中色在线播放 | 亚州精品国产 | 免费欧美精品 | 国产亚洲综合一区二区 | 久久草在线观看视频 | 欧美日韩免费在线观看视频 | 午夜视频亚洲 | 亚洲卡通动漫在线观看 | 俄罗斯16一20sex牲色另类 | 黄色成人短视频 | 在线无码 | 国产精品视频久久久 | 亚洲福利视频52 | 久操福利视频 | 在线中文字幕观看 | 欧美18一12sex性处hd | 中日韩免费视频 | 香蕉秀| 久久精品亚洲一区 | 精品国产乱码久久久久久预案 | 婷婷亚洲一区二区三区 | 国产精品成人免费一区久久羞羞 | 中文字幕在线播放第一页 |