SpringBoot通过计划任务发送邮件提醒的代码详解
作者:_童年的回忆_
在实际线上项目中,有不断接受到推送方发来的数据场景,而且是不间断的发送,如果忽然间断了,应该是出问题了,需要及时检查原因,这种情况比较适合用计划任务做检查判断,出问题发邮件提醒,本文给大家介绍了SpringBoot通过计划任务发送邮件提醒,需要的朋友可以参考下
概要
在实际线上项目中,有不断接受到推送方发来的数据场景,而且是不间断的发送。如果忽然间断了,应该是出问题了,需要及时检查原因,这种情况比较适合用计划任务做检查判断,出问题发邮件提醒。
技术细节
邮件发送使用spring的JavaMailSender,先添加pom依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
接着配置application.yml,指定发送邮箱,本文使用的是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层新增发送邮件的方法:
@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());
}
}
邮件发送就完成了,接下来配置计划任务:
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分钟执行一次
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));//发送给运维
return;
}
System.out.println("半个小时之内,共入库:"+list.size()+"条数据, 监测时间:"+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通过计划任务发送邮件提醒的代码详解的文章就介绍到这了,更多相关SpringBoot发送邮件提醒内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
