国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

springboot 與異步任務(wù),定時任務(wù),郵件任務(wù)

這篇具有很好參考價值的文章主要介紹了springboot 與異步任務(wù),定時任務(wù),郵件任務(wù)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

異步任務(wù)

在Java應(yīng)用中,絕大多數(shù)情況下都是通過同步的方式來實現(xiàn)交互處理的;但是在處理與第三方系統(tǒng)交互的時候,容易造成響應(yīng)遲緩的情況,之前大部分都是使用多線程來完成此類任務(wù),其實,在Spring 3.x之后,就已經(jīng)內(nèi)置了@Async來完美解決這個問題。

SpringBoot 實現(xiàn)比較簡單
主啟動類:添加 注釋:@EnableAsync

@EnableScheduling
@EnableAsync
@MapperScan("com.hrp.**.dao")
@SpringBootApplication
public class EcsApplication {
    public static void main(String[] args) {
        SpringApplication.run(EcsApplication.class, args);
    }

}

業(yè)務(wù)方法添加 @Async

 @Async
    @Override
    public void TestAsync() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("-------------");
    }

controller調(diào)用

@RequestMapping("myFreeMark")
    public String myFreeMark(Map<String,Object> map){
        map.put("name","zhangsan");
        map.put("mydate",new Date());
        asyncServer.TestAsync();
        System.out.println("==================FreemarkerController=======myFreeMark=====");
        return "myFreeMark";
    }

springboot 與異步任務(wù),定時任務(wù),郵件任務(wù),# SpringBoot,spring boot,后端,java
訪問看到控制臺打印順序可以知道TestAsync方法異步調(diào)用

定時任務(wù)

項目開發(fā)中經(jīng)常需要執(zhí)行一些定時任務(wù),比如需要在每天凌晨時候,分析一次前
一天的日志信息。Spring為我們提供了異步執(zhí)行任務(wù)調(diào)度的方式,提供TaskExecutor 、TaskScheduler 接口。

主啟動類:增加@EnableScheduling

@EnableScheduling
@EnableAsync
@MapperScan("com.hrp.**.dao")
@SpringBootApplication
public class EcsApplication {
    public static void main(String[] args) {
        SpringApplication.run(EcsApplication.class, args);
    }

}

任務(wù)類:類增加@Service或者@Compont注釋方法增加@Scheduled注解

@Service
public class BackUpMysqlTask {

    /**
     * Seconds : 可出現(xiàn)", - * /"四個字符,有效范圍為0-59的整數(shù)
     * Minutes : 可出現(xiàn)", - * /"四個字符,有效范圍為0-59的整數(shù)
     * Hours : 可出現(xiàn)", - * /"四個字符,有效范圍為0-23的整數(shù)
     * DayofMonth : 可出現(xiàn)", - * / ? L W C"八個字符,有效范圍為0-31的整數(shù)
     * Month : 可出現(xiàn)", - * /"四個字符,有效范圍為1-12的整數(shù)或JAN-DEc
     * DayofWeek : 可出現(xiàn)", - * / ? L C #"四個字符,有效范圍為1-7的整數(shù)或SUN-SAT兩個范圍。1表示星期天,2表示星期一, 依次類推
     * Year : 可出現(xiàn)", - * /"四個字符,有效范圍為1970-2099年
     */
    @Scheduled(cron = "0 * * * * MON-FRI")
    public void backUpMysql() {
        System.out.println("===============");
    }
}

我們可以觀察到控制臺不斷的再打印
這里要講解cron

springboot 與異步任務(wù),定時任務(wù),郵件任務(wù),# SpringBoot,spring boot,后端,java
springboot 與異步任務(wù),定時任務(wù),郵件任務(wù),# SpringBoot,spring boot,后端,java

 /**
     * Seconds : 可出現(xiàn)", - * /"四個字符,有效范圍為0-59的整數(shù)
     * Minutes : 可出現(xiàn)", - * /"四個字符,有效范圍為0-59的整數(shù)
     * Hours : 可出現(xiàn)", - * /"四個字符,有效范圍為0-23的整數(shù)
     * DayofMonth : 可出現(xiàn)", - * / ? L W C"八個字符,有效范圍為0-31的整數(shù)
     * Month : 可出現(xiàn)", - * /"四個字符,有效范圍為1-12的整數(shù)或JAN-DEc
     * DayofWeek : 可出現(xiàn)", - * / ? L C #"四個字符,有效范圍為1-7的整數(shù)或SUN-SAT兩個范圍。1表示星期天,2表示星期一, 依次類推
     * Year : 可出現(xiàn)", - * /"四個字符,有效范圍為1970-2099年
     */

下面簡單舉幾個例子:

“0 0 12 * * ?” 每天中午十二點觸發(fā)
“0 15 10 ? * *” 每天早上10:15觸發(fā)
“0 15 10 * * ?” 每天早上10:15觸發(fā)
“0 15 10 * * ? *” 每天早上10:15觸發(fā)
“0 15 10 * * ? 2005” 2005年的每天早上10:15觸發(fā)
“0 * 14 * * ?” 每天從下午2點開始到2點59分每分鐘一次觸發(fā)
“0 0/5 14 * * ?” 每天從下午2點開始到2:55分結(jié)束每5分鐘一次觸發(fā)
“0 0/5 14,18 * * ?” 每天的下午2點至2:55和6點至6點55分兩個時間段內(nèi)每5分鐘一次觸發(fā)
“0 0-5 14 * * ?” 每天14:00至14:05每分鐘一次觸發(fā)
“0 10,44 14 ? 3 WED” 三月的每周三的14:10和14:44觸發(fā)
“0 15 10 ? * MON-FRI” 每個周一、周二、周三、周四、周五的10:15觸發(fā)

郵件任務(wù)

準(zhǔn)備工作

做過郵件的都大家都知道
springboot 與異步任務(wù),定時任務(wù),郵件任務(wù),# SpringBoot,spring boot,后端,java
所以我們要是使用qq郵箱發(fā)送必須有登錄qq郵箱的權(quán)限
springboot 與異步任務(wù),定時任務(wù),郵件任務(wù),# SpringBoot,spring boot,后端,java
開啟smtp服務(wù),發(fā)送短信我們就可以獲取一個授權(quán)碼,自己拷貝下來下圖的授權(quán)碼記錄下來
springboot 與異步任務(wù),定時任務(wù),郵件任務(wù),# SpringBoot,spring boot,后端,java
springboot 與異步任務(wù),定時任務(wù),郵件任務(wù),# SpringBoot,spring boot,后端,java

開始

添加依賴

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

配置

  mail:
    host: smtp.qq.com  其他郵箱需要修改
    username: 郵箱賬戶
    password: 授權(quán)碼
    properties:
      mail:
        smtp:
          ssl:
            enable: true

測試代碼

 @Autowired
    private JavaMailSender javaMailSender;
    @Test
    void contextLoads() {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setText("ddd");
        simpleMailMessage.setSubject("主題");
        simpleMailMessage.setTo("");
        simpleMailMessage.setFrom("");
        javaMailSender.send(simpleMailMessage);
    }

我們可以查收到郵件

上面是普通的郵件

發(fā)送html內(nèi)容

 @Test
    public void testSend() throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
        messageHelper.setSubject("標(biāo)題");
        messageHelper.setTo("@dhcc.com.cn");
        messageHelper.setFrom("@qq.com");
        messageHelper.setText("<h1>標(biāo)題</h1><br/><p>這是內(nèi)容</p>", true);
        javaMailSender.send(messageHelper.getMimeMessage());
    }

這里需要注意的是,setText的時候需要傳一個布爾值進(jìn)去,表名需要使用HTML樣式。

最后代碼附件文章來源地址http://www.zghlxwxcb.cn/news/detail-692154.html

package com.hrp.msage.service;

import javax.mail.MessagingException;

/**
 * ecs
 *
 * @Title: com.hrp.msage.service
 * @Date: 2020/7/29 13:48
 * @Author: wfg
 * @Description:
 * @Version:
 */
public interface MailService {
    /**
     * 簡單文本郵件
     * @param to 接收者郵件
     * @param subject 郵件主題
     * @param contnet 郵件內(nèi)容
     */
    public void sendSimpleMail(String to, String subject, String contnet);
    /**
     * HTML 文本郵件
     * @param to 接收者郵件
     * @param subject 郵件主題
     * @param contnet HTML內(nèi)容
     * @throws MessagingException
     */
    public void sendHtmlMail(String to, String subject, String contnet) throws MessagingException;
    /**
     * 附件郵件
     * @param to 接收者郵件
     * @param subject 郵件主題
     * @param contnet HTML內(nèi)容
     * @param filePath 附件路徑
     * @throws MessagingException
     */
    public void sendAttachmentsMail(String to, String subject, String contnet,
                                    String filePath) throws MessagingException;
    /**
     * 圖片郵件
     * @param to 接收者郵件
     * @param subject 郵件主題
     * @param contnet HTML內(nèi)容
     * @param rscPath 圖片路徑
     * @param rscId 圖片ID
     * @throws MessagingException
     */
    public void sendInlinkResourceMail(String to, String subject, String contnet,
                                       String rscPath, String rscId);
}

package com.hrp.msage.serviceImpl;

import com.hrp.msage.service.MailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

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

/**
 * ecs
 *
 * @Title: com.hrp.msage.serviceImpl
 * @Date: 2020/7/29 13:48
 * @Author: wfg
 * @Description:
 * @Version:
 */
@Service("mailService")
public class MailServiceImpl implements MailService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Value("${spring.mail.username}")
    private String from;

    @Autowired
    private JavaMailSender mailSender;

    /**
     * 簡單文本郵件
     * @param to 接收者郵件
     * @param subject 郵件主題
     * @param contnet 郵件內(nèi)容
     */
    @Override
    public void sendSimpleMail(String to, String subject, String contnet){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo(to);
        message.setSubject(subject);
        message.setText(contnet);
        message.setFrom(from);
        mailSender.send(message);
    }
    /**
     * HTML 文本郵件
     * @param to 接收者郵件
     * @param subject 郵件主題
     * @param contnet HTML內(nèi)容
     * @throws MessagingException
     */
    @Override
    public void sendHtmlMail(String to, String subject, String contnet) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(contnet, true);
        helper.setFrom(from);

        mailSender.send(message);
    }

    /**
     * 附件郵件
     * @param to 接收者郵件
     * @param subject 郵件主題
     * @param contnet HTML內(nèi)容
     * @param filePath 附件路徑
     * @throws MessagingException
     */
    @Override
    public void sendAttachmentsMail(String to, String subject, String contnet,
                                    String filePath) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(contnet, true);
        helper.setFrom(from);

        FileSystemResource file = new FileSystemResource(new File(filePath));
        String fileName = file.getFilename();
        helper.addAttachment(fileName, file);

        mailSender.send(message);
    }

    /**
     * 圖片郵件
     * @param to 接收者郵件
     * @param subject 郵件主題
     * @param contnet HTML內(nèi)容
     * @param rscPath 圖片路徑
     * @param rscId 圖片ID
     * @throws MessagingException
     */
    @Override
    public void sendInlinkResourceMail(String to, String subject, String contnet,
                                       String rscPath, String rscId) {
        logger.info("發(fā)送靜態(tài)郵件開始: {},{},{},{},{}", to, subject, contnet, rscPath, rscId);

        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = null;

        try {

            helper = new MimeMessageHelper(message, true);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(contnet, true);
            helper.setFrom(from);

            FileSystemResource res = new FileSystemResource(new File(rscPath));
            helper.addInline(rscId, res);
            mailSender.send(message);
            logger.info("發(fā)送靜態(tài)郵件成功!");

        } catch (MessagingException e) {
            logger.info("發(fā)送靜態(tài)郵件失敗: ", e);
        }

    }



}

package com.hrp;

import com.hrp.msage.service.MailService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.mail.MessagingException;

/**
 * ecs
 *
 * @Title: com.hrp
 * @Date: 2020/7/29 13:57
 * @Author: wfg
 * @Description:
 * @Version:
 */
@SpringBootTest
public class MailServiceTest {


    @Autowired
    private MailService mailService;

//    @Resource
//    private TemplateEngine templateEngine;

    @Test
    public void sendSimpleMail() {
        mailService.sendSimpleMail("wufagang@dhcc.com.cn","測試spring boot imail-主題","測試spring boot imail - 內(nèi)容");
    }

    @Test
    public void sendHtmlMail() throws MessagingException {

        String content = "<html>\n" +
                "<body>\n" +
                "<h3>hello world</h3>\n" +
                "<h1>html</h1>\n" +
                "<body>\n" +
                "</html>\n";

        mailService.sendHtmlMail("wufagang@dhcc.com.cn","這是一封HTML郵件",content);
    }

    @Test
    public void sendAttachmentsMail() throws MessagingException {
        String filePath = "D:\\projects\\20200727\\ecs\\src\\main\\resources\\system.properties";
        String content = "<html>\n" +
                "<body>\n" +
                "<h3>hello world</h3>\n" +
                "<h1>html</h1>\n" +
                "<h1>附件傳輸</h1>\n" +
                "<body>\n" +
                "</html>\n";
        mailService.sendAttachmentsMail("wufagang@dhcc.com.cn","這是一封HTML郵件",content, filePath);
    }

    @Test
    public void sendInlinkResourceMail() throws MessagingException {
        //TODO 改為本地圖片目錄
        String imgPath = "D:\\projects\\20200727\\ecs\\src\\main\\resources\\imag\\IMG_20200625_104833.jpg";
        String rscId = "admxj001";
        String content = "<html>" +
                "<body>" +
                "<h3>hello world</h3>" +
                "<h1>html</h1>" +
                "<h1>圖片郵件</h1>" +
                "<img src='cid:"+rscId+"'></img>" +
                "<body>" +
                "</html>";

        mailService.sendInlinkResourceMail("wufagang@dhcc.com.cn","這是一封圖片郵件",content, imgPath, rscId);
    }

    @Test
    public void testTemplateMailTest() throws MessagingException {
//        Context context = new Context();
//        context.setVariable("id","ispringboot");
//
//        String emailContent = templateEngine.process("emailTeplate", context);
//        mailService.sendHtmlMail("ispringboot@163.com","這是一封HTML模板郵件",emailContent);

    }
}

到了這里,關(guān)于springboot 與異步任務(wù),定時任務(wù),郵件任務(wù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 【SpringBoot篇】Spring_Task定時任務(wù)框架

    【SpringBoot篇】Spring_Task定時任務(wù)框架

    Spring Task 是 Spring 框架提供的一種任務(wù)調(diào)度和異步處理的解決方案。 可以按照約定的時間自動執(zhí)行某個代碼邏輯 它可以幫助開發(fā)者在 Spring 應(yīng)用中輕松地實現(xiàn)定時任務(wù)、異步任務(wù)等功能,提高應(yīng)用的效率和可維護(hù)性。 Spring Task 的主要特點包括: 簡單易用:Spring Task 提供了簡

    2024年02月04日
    瀏覽(24)
  • springboot 發(fā)送郵件,以及郵件工具類 并且解決spring-boot-starter-mail 發(fā)送郵件附件亂碼或者文件錯亂

    1、設(shè)置系統(tǒng)值 System.setProperty(“mail.mime.splitlongparameters”, “false”); 2、 在創(chuàng)建對象的時候定義編碼格式(utf-8): MimeMessageHelper helper = new MimeMessageHelper(mes, true, “utf-8”); 3、 其次,在添加附件的時候,附件名是需要定義編碼的 helper.addAttachment(MimeUtility.encodeWord(附件名,“utf-8”

    2024年02月15日
    瀏覽(33)
  • Spring Boot異步任務(wù)、異步消息

    Spring Boot異步任務(wù)、異步消息

    目錄 1.異步任務(wù) 1.1.概述 1.2.使用 2.異步消息 2.1.概述 2.2.使用 舉一個例子,我現(xiàn)在有一個網(wǎng)上商城,客戶在界面點擊下單后,后臺需要完成兩步: 1.創(chuàng)建客戶訂單 2.發(fā)短信通知客戶訂單號 這里面第2步是個高耗時的操作,如果全部擠在一條主線程里做,效果就會是客戶點了一

    2023年04月22日
    瀏覽(33)
  • Spring Schedule:Spring boot整合Spring Schedule實戰(zhàn)講解定時發(fā)送郵件的功能

    Spring Schedule:Spring boot整合Spring Schedule實戰(zhàn)講解定時發(fā)送郵件的功能

    ???? 歡迎光臨,終于等到你啦 ???? ??我是 蘇澤 ,一位對技術(shù)充滿熱情的探索者和分享者。???? ??持續(xù)更新的專欄 《Spring 狂野之旅:從入門到入魔》 ?? 本專欄帶你從Spring入門到入魔 ? 這是蘇澤的個人主頁可以看到我其他的內(nèi)容哦???? 努力的蘇澤 http://suzee.blog.

    2024年03月14日
    瀏覽(20)
  • java中定時任務(wù) schedule 分布式下沒有鎖住 時間不同步 執(zhí)行滯后 相對時間 系統(tǒng)時間 spring springboot

    java.util.Timer計時器可以進(jìn)行:管理任務(wù)延遲執(zhí)行(“如1000ms后執(zhí)行任務(wù)”),及周期性執(zhí)行(“如每500ms執(zhí)行一次該任務(wù)”)。 但是,Timer存在一些缺陷,應(yīng)考慮使用ScheduledThreadPoolExecutor代替,Timer對調(diào)度的支持是基于絕對時間,而不是相對時間的,由此任務(wù)對系統(tǒng)時鐘的改變是敏感

    2024年02月10日
    瀏覽(27)
  • Spring Boot定時任務(wù)

    Spring Boot定時任務(wù)

    目錄 1.概述 2.Spring Boot定時任務(wù) 2.1.快速使用 2.2.cron表達(dá)式 3.業(yè)務(wù)示例 3.1.業(yè)務(wù)描述 3.2.業(yè)務(wù)實現(xiàn) 4.實現(xiàn)原理 5.自定義線程池 在某些業(yè)務(wù)場景中,需要定時執(zhí)行一些任務(wù),有可能是定時統(tǒng)計然后生成報表,有可能是定時發(fā)起一個任務(wù)。最近在工作中就正好遇見一個定時發(fā)起問卷

    2024年02月07日
    瀏覽(28)
  • Spring boot開啟定時任務(wù)

    ?? 使用@Scheduled 注解很方便,但缺點是當(dāng)我們調(diào)整了執(zhí)行周期的時候,需要重啟應(yīng)用才能生效,這多少有些不方便。為了達(dá)到實時生效的效果,那么可以使用接口來完成定時任務(wù),統(tǒng)一將定時器信息存放在數(shù)據(jù)庫中。 1.?在mysql中執(zhí)行一下腳本插入定時任務(wù): 2. Mapper層 3.?

    2024年02月10日
    瀏覽(23)
  • spring-boot定時任務(wù)

    spring-boot定時任務(wù)

    定時任務(wù)規(guī)則:0? *? *? * ? *? * 表示任意月的任意周的每天的每時的每分的0秒開始一次任務(wù)。 任務(wù)加在方法上? 開始一次任務(wù) 表示 啟動?一次方法。 0/5 *? *? 5? *? 4? 表示 每月的最后一周的第五天的任意時任意分的0秒開始 每隔5秒啟動一次任務(wù)。 定時任務(wù)表達(dá)式 還有很

    2024年01月21日
    瀏覽(31)
  • 解密Spring Boot的定時任務(wù)

    大家好!歡迎來到本篇博客,今天我們將深入探討Spring Boot中的定時任務(wù),以及它在單線程和多線程環(huán)境下的運(yùn)行機(jī)制。本文將詳細(xì)解析定時任務(wù)的工作原理,并附帶實際案例進(jìn)行演示。 1. Spring Boot定時任務(wù)的基本概念 Spring Boot的定時任務(wù)是基于Quartz Scheduler實現(xiàn)的,它允許您

    2024年01月19日
    瀏覽(36)
  • Spring Boot 面試題——定時任務(wù)

    (1)定時任務(wù)是一種 在指定的時間點或時間間隔內(nèi)自動觸發(fā)執(zhí)行的任務(wù) 。它能夠周期性地執(zhí)行一些重復(fù)性、時間敏感或計劃性的操作,而無需人工干預(yù)。定時任務(wù)的需求主要有以下幾個方面: 自動化 :定時任務(wù)可以實現(xiàn)某些操作的自動化,無需人工手動執(zhí)行。這可以提高

    2024年02月08日
    瀏覽(20)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包