【Java】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);
    }
}

小结

这样就达到了计划任务检查的效果,还是比较实用的。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值