欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

概要

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

技術細節(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已經有半個小時沒有獲取到推送數據了,檢測時間: <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-預警提醒");
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            System.out.println(e.getMessage());
        }
    }

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

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點到早晨8點不檢查
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long timestamp = new Date().getTime() / 1000;//抓取最近半個小時內的數據
        List<ToutiaoPush> list = toutiaoPushService.findByTimestamp(timestamp - (60*30));
        if(list.isEmpty()) {
            toutiaoPushService.sendWarningMail("xxx@163.com", sdf.format(d));//發(fā)送給運維
            return;
        }
        System.out.println("半個小時之內,共入庫:"+list.size()+"條數據, 監(jiān)測時間:"+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)//打開異步任務開關
public class PromotionApplication {

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

小結

這樣就達到了計劃任務檢查的效果,還是比較實用的。

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

相關文章

  • Java中switch的三種用法方式小結

    Java中switch的三種用法方式小結

    這篇文章主要介紹了Java中switch的三種用法方式小結,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-04-04
  • SpringBoot AOP處理請求日志打印功能代碼實例

    SpringBoot AOP處理請求日志打印功能代碼實例

    這篇文章主要介紹了SpringBoot AOP處理請求日志打印功能代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-03-03
  • 一篇文章帶你入門java代理模式

    一篇文章帶你入門java代理模式

    這篇文章主要介紹了Java代理模式,結合實例形式詳細分析了java基本數據類型、數據類型轉換、算術運算符、邏輯運算符等相關原理與操作技巧,需要的朋友可以參考下
    2021-08-08
  • 在springboot中實現個別bean懶加載的操作

    在springboot中實現個別bean懶加載的操作

    這篇文章主要介紹了在springboot中實現個別bean懶加載的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • Java圖片處理的簡易指南

    Java圖片處理的簡易指南

    圖像處理是各類應用程序的重要組成部分,Java作為一種多功能且強大的編程語言,提供了豐富的庫和框架來高效地處理圖像處理任務,本文將帶您了解Java圖像處理的基本概念、工具以及實踐示例,幫助您掌握Java圖像處理技術,需要的朋友可以參考下
    2024-09-09
  • springboot @ConfigurationProperties和@PropertySource的區(qū)別

    springboot @ConfigurationProperties和@PropertySource的區(qū)別

    這篇文章主要介紹了springboot @ConfigurationProperties和@PropertySource的區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • java線程中斷?interrupt?和?LockSupport解析

    java線程中斷?interrupt?和?LockSupport解析

    這篇文章主要為大家介紹了java線程中斷?interrupt?和?LockSupport示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-02-02
  • Java中通過ZipOutputStream類如何將多個文件打成zip

    Java中通過ZipOutputStream類如何將多個文件打成zip

    ZipOutputStream?是Java中用于創(chuàng)建ZIP文件的類,它是?java.util.zip?包中的一部分,通過使用?ZipOutputStream?,可以將多個文件壓縮到一個ZIP文件中,這篇文章主要介紹了Java中(ZipOutputStream)如何將多個文件打成zip,需要的朋友可以參考下
    2023-09-09
  • Spring項目中swagger用法與swagger-ui使用

    Spring項目中swagger用法與swagger-ui使用

    這篇文章主要介紹了Spring項目中swagger用法與swagger-ui使用,通過圖文并茂的形式給大家介紹了編寫springboot項目的方法及導入spring-fox依賴的代碼詳解,需要的朋友可以參考下
    2021-05-05
  • java 如何計算同比增長工具類

    java 如何計算同比增長工具類

    這篇文章主要介紹了java 如何計算同比增長工具類的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07

最新評論