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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
springboot @ConfigurationProperties和@PropertySource的區(qū)別
這篇文章主要介紹了springboot @ConfigurationProperties和@PropertySource的區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06java線程中斷?interrupt?和?LockSupport解析
這篇文章主要為大家介紹了java線程中斷?interrupt?和?LockSupport示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02Java中通過ZipOutputStream類如何將多個文件打成zip
ZipOutputStream?是Java中用于創(chuàng)建ZIP文件的類,它是?java.util.zip?包中的一部分,通過使用?ZipOutputStream?,可以將多個文件壓縮到一個ZIP文件中,這篇文章主要介紹了Java中(ZipOutputStream)如何將多個文件打成zip,需要的朋友可以參考下2023-09-09Spring項目中swagger用法與swagger-ui使用
這篇文章主要介紹了Spring項目中swagger用法與swagger-ui使用,通過圖文并茂的形式給大家介紹了編寫springboot項目的方法及導入spring-fox依賴的代碼詳解,需要的朋友可以參考下2021-05-05