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)文章
java8 集合 多字段 分組 統(tǒng)計(jì)個(gè)數(shù)代碼
這篇文章主要介紹了java8 集合 多字段 分組 統(tǒng)計(jì)個(gè)數(shù)代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08
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)字段更新的三種方法
字段更新是指在數(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í)例代碼
這篇文章主要給大家介紹了關(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
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ò)問題
在調(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

