最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot通過計(jì)劃任務(wù)發(fā)送郵件提醒的代碼詳解

 更新時(shí)間:2024年11月18日 09:18:36   作者:_童年的回憶_  
在實(shí)際線上項(xiàng)目中,有不斷接受到推送方發(fā)來的數(shù)據(jù)場景,而且是不間斷的發(fā)送,如果忽然間斷了,應(yīng)該是出問題了,需要及時(shí)檢查原因,這種情況比較適合用計(jì)劃任務(wù)做檢查判斷,出問題發(fā)郵件提醒,本文給大家介紹了SpringBoot通過計(jì)劃任務(wù)發(fā)送郵件提醒,需要的朋友可以參考下

概要

在實(shí)際線上項(xiàng)目中,有不斷接受到推送方發(fā)來的數(shù)據(jù)場景,而且是不間斷的發(fā)送。如果忽然間斷了,應(yīng)該是出問題了,需要及時(shí)檢查原因,這種情況比較適合用計(jì)劃任務(wù)做檢查判斷,出問題發(fā)郵件提醒。

技術(shù)細(xì)節(jié)

郵件發(fā)送使用spring的JavaMailSender,先添加pom依賴:

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

接著配置application.yml,指定發(fā)送郵箱,本文使用的是zoho郵箱:

spring:
  mail:
    host: smtp.zoho.com
    username: noreply@xxx.top
    password: xxxxxx
    port: 465
    protocol: smtp
    default-encoding: utf-8
    properties:
      mail:
        smtp:
          auth: true
          starttls:
            enable: true
            required: true
          ssl:
            enable: true
          socketFactory:
            port: 465
            class: javax.net.ssl.SSLSocketFactory

然后在service層新增發(fā)送郵件的方法:

    @Autowired
    private JavaMailSender javaMailSender;
    @Override
    public void sendWarningMail(String to, String datetime) {
        String content = "xxxx已經(jīng)有半個(gè)小時(shí)沒有獲取到推送數(shù)據(jù)了,檢測時(shí)間: <span style='color: red;'>" + datetime + "</span>。";
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
            mimeMessageHelper.setTo(to);
            mimeMessageHelper.setFrom("noreply@xxxx.top");
            mimeMessageHelper.setText(content,true);
            mimeMessageHelper.setSubject("xxxx-預(yù)警提醒");
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            System.out.println(e.getMessage());
        }
    }

郵件發(fā)送就完成了,接下來配置計(jì)劃任務(wù):

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.yunheng.pricepush.domain.ToutiaoPush;
import com.yunheng.pricepush.service.ToutiaoPushService;
import com.yunheng.pricepush.utility.RedisUtils;
import com.yunheng.pricepush.utility.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
@Slf4j
@EnableScheduling
public class QSConsumer
{
    private RedisUtils redisUtils() {
        return SpringUtils.getBean(RedisUtils.class);//SpringUtils與RedisUtils上一篇博文有介紹
    }
    @Autowired
    private ToutiaoPushService toutiaoPushService;

    @Async("priceExecutor")
    @Scheduled(fixedDelay = 60000) //1分鐘執(zhí)行一次
    public void checkTask() {
        Date d = new Date();
        SimpleDateFormat hour = new SimpleDateFormat("HH");
        int h = Integer.parseInt(hour.format(d));
        if(h < 8) return;//晚上12點(diǎn)到早晨8點(diǎn)不檢查
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long timestamp = new Date().getTime() / 1000;//抓取最近半個(gè)小時(shí)內(nèi)的數(shù)據(jù)
        List<ToutiaoPush> list = toutiaoPushService.findByTimestamp(timestamp - (60*30));
        if(list.isEmpty()) {
            toutiaoPushService.sendWarningMail("xxx@163.com", sdf.format(d));//發(fā)送給運(yùn)維
            return;
        }
        System.out.println("半個(gè)小時(shí)之內(nèi),共入庫:"+list.size()+"條數(shù)據(jù), 監(jiān)測時(shí)間:"+sdf.format(d));
    }
}

Application入口類:

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
@ServletComponentScan
@MapperScan("com.yunheng.pricepush.mapper")
@EnableAsync(proxyTargetClass = true)//打開異步任務(wù)開關(guān)
public class PromotionApplication {

    public static void main(String[] args) {
        final ApplicationContext applicationContext = SpringApplication.run(PromotionApplication.class, args);
    }
}

小結(jié)

這樣就達(dá)到了計(jì)劃任務(wù)檢查的效果,還是比較實(shí)用的。

到此這篇關(guān)于SpringBoot通過計(jì)劃任務(wù)發(fā)送郵件提醒的代碼詳解的文章就介紹到這了,更多相關(guān)SpringBoot發(fā)送郵件提醒內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring手動(dòng)獲取bean的四種方式

    Spring手動(dòng)獲取bean的四種方式

    本文主要介紹了Spring手動(dòng)獲取bean的四種方式,包括BeanFactoryPostProcessor接口,ApplicationContextAware接口,注解 @PostConstruct 初始化時(shí)獲取,啟動(dòng)類ApplicationContext獲取這四種方法,感興趣的可以了解一下
    2024-01-01
  • java8 集合 多字段 分組 統(tǒng)計(jì)個(gè)數(shù)代碼

    java8 集合 多字段 分組 統(tǒng)計(jì)個(gè)數(shù)代碼

    這篇文章主要介紹了java8 集合 多字段 分組 統(tǒng)計(jì)個(gè)數(shù)代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • Java基礎(chǔ)入門之switch怎么使用枚舉

    Java基礎(chǔ)入門之switch怎么使用枚舉

    在Java開發(fā)中,switch語句是一種常用的流控制語句,而當(dāng)使用枚舉類型作為條件時(shí),我們常常會(huì)遇到報(bào)錯(cuò)問題,那么該如何解決呢,下面就來詳細(xì)講講
    2023-06-06
  • Java之打印String對(duì)象的地址

    Java之打印String對(duì)象的地址

    這篇文章主要介紹了Java之打印String對(duì)象的地址,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Spring注解驅(qū)動(dòng)開發(fā)實(shí)現(xiàn)屬性賦值

    Spring注解驅(qū)動(dòng)開發(fā)實(shí)現(xiàn)屬性賦值

    這篇文章主要介紹了Spring注解驅(qū)動(dòng)開發(fā)實(shí)現(xiàn)屬性賦值,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • SpringBoot+MyBatis實(shí)現(xiàn)動(dòng)態(tài)字段更新的三種方法

    SpringBoot+MyBatis實(shí)現(xiàn)動(dòng)態(tài)字段更新的三種方法

    字段更新是指在數(shù)據(jù)庫表中修改特定列的值的操作,這種操作可以通過多種方式進(jìn)行,具體取決于業(yè)務(wù)需求和技術(shù)環(huán)境,本文給大家介紹了在Spring Boot和MyBatis中,實(shí)現(xiàn)動(dòng)態(tài)更新不固定字段的三種方法,需要的朋友可以參考下
    2025-04-04
  • spring boot + mybatis實(shí)現(xiàn)動(dòng)態(tài)切換數(shù)據(jù)源實(shí)例代碼

    spring boot + mybatis實(shí)現(xiàn)動(dòng)態(tài)切換數(shù)據(jù)源實(shí)例代碼

    這篇文章主要給大家介紹了關(guān)于spring boot + mybatis實(shí)現(xiàn)動(dòng)態(tài)切換數(shù)據(jù)源的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-10-10
  • MyBatis配置文件元素示例詳解

    MyBatis配置文件元素示例詳解

    在MyBatis框架的核心配置文件中,<configuration>元素是配置文件的根元素,其他元素都要在<contiguration>元素內(nèi)配置,這篇文章主要介紹了MyBatis配置文件元素,需要的朋友可以參考下
    2023-06-06
  • Java中BigDecimal類與int、Integer使用總結(jié)

    Java中BigDecimal類與int、Integer使用總結(jié)

    這篇文章主要給大家介紹了關(guān)于Java中BigDecimal類與int、Integer使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • 完美解決Server?returned?HTTP?response?code:403?for?URL報(bào)錯(cuò)問題

    完美解決Server?returned?HTTP?response?code:403?for?URL報(bào)錯(cuò)問題

    在調(diào)用某個(gè)接口的時(shí)候,突然就遇到了Server?returned?HTTP?response?code:?403?for?URL報(bào)錯(cuò)這個(gè)報(bào)錯(cuò),導(dǎo)致獲取不到接口的數(shù)據(jù),下面小編給大家分享解決Server?returned?HTTP?response?code:403?for?URL報(bào)錯(cuò)問題,感興趣的朋友一起看看吧
    2023-03-03

最新評(píng)論

通化市| 禄丰县| 固原市| 民权县| 同仁县| 比如县| 新野县| 美姑县| 宜都市| 龙岩市| 平定县| 柘荣县| 镇安县| 分宜县| 黄陵县| 乡城县| 兴安盟| 集贤县| 广饶县| 沾益县| 宜兰县| 宣城市| 河曲县| 荣成市| 英德市| 泾阳县| 景宁| 石狮市| 辉南县| 略阳县| 县级市| 临夏县| 诏安县| 正镶白旗| 包头市| 临沭县| 前郭尔| 瑞昌市| 南郑县| 迁西县| 通城县|