SpringBoot通過(guò)計(jì)劃任務(wù)發(fā)送郵件提醒的代碼詳解
概要
在實(shí)際線上項(xiàng)目中,有不斷接受到推送方發(fā)來(lái)的數(shù)據(jù)場(chǎng)景,而且是不間斷的發(fā)送。如果忽然間斷了,應(yīng)該是出問(wèn)題了,需要及時(shí)檢查原因,這種情況比較適合用計(jì)劃任務(wù)做檢查判斷,出問(wèn)題發(fā)郵件提醒。
技術(shù)細(xì)節(jié)
郵件發(fā)送使用spring的JavaMailSender,先添加pom依賴(lài):
<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í)沒(méi)有獲取到推送數(shù)據(jù)了,檢測(cè)時(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ā)送就完成了,接下來(lái)配置計(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),共入庫(kù):"+list.size()+"條數(shù)據(jù), 監(jiān)測(cè)時(shí)間:"+sdf.format(d));
}
}
Application入口類(lèi):
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)//打開(kāi)異步任務(wù)開(kāi)關(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通過(guò)計(jì)劃任務(wù)發(fā)送郵件提醒的代碼詳解的文章就介紹到這了,更多相關(guān)SpringBoot發(fā)送郵件提醒內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot AOP處理請(qǐng)求日志打印功能代碼實(shí)例
這篇文章主要介紹了SpringBoot AOP處理請(qǐng)求日志打印功能代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
在springboot中實(shí)現(xiàn)個(gè)別bean懶加載的操作
這篇文章主要介紹了在springboot中實(shí)現(xiàn)個(gè)別bean懶加載的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
springboot @ConfigurationProperties和@PropertySource的區(qū)別
這篇文章主要介紹了springboot @ConfigurationProperties和@PropertySource的區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
java線程中斷?interrupt?和?LockSupport解析
這篇文章主要為大家介紹了java線程中斷?interrupt?和?LockSupport示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Java中通過(guò)ZipOutputStream類(lèi)如何將多個(gè)文件打成zip
ZipOutputStream?是Java中用于創(chuàng)建ZIP文件的類(lèi),它是?java.util.zip?包中的一部分,通過(guò)使用?ZipOutputStream?,可以將多個(gè)文件壓縮到一個(gè)ZIP文件中,這篇文章主要介紹了Java中(ZipOutputStream)如何將多個(gè)文件打成zip,需要的朋友可以參考下2023-09-09
Spring項(xiàng)目中swagger用法與swagger-ui使用
這篇文章主要介紹了Spring項(xiàng)目中swagger用法與swagger-ui使用,通過(guò)圖文并茂的形式給大家介紹了編寫(xiě)springboot項(xiàng)目的方法及導(dǎo)入spring-fox依賴(lài)的代碼詳解,需要的朋友可以參考下2021-05-05
java 如何計(jì)算同比增長(zhǎng)工具類(lèi)
這篇文章主要介紹了java 如何計(jì)算同比增長(zhǎng)工具類(lèi)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07

