SpringBoot通过计划任务发送邮件提醒的代码详解

 更新时间:2024年11月18日 09:18:36   作者:_童年的回忆_  
在实际线上项目中,有不断接受到推送方发来的数据场景,而且是不间断的发送,如果忽然间断了,应该是出问题了,需要及时检查原因,这种情况比较适合用计划任务做检查判断,出问题发邮件提醒,本文给大家介绍了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发送邮件提醒内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Spring手动获取bean的四种方式

    Spring手动获取bean的四种方式

    本文主要介绍了Spring手动获取bean的四种方式,包括BeanFactoryPostProcessor接口,ApplicationContextAware接口,注解 @PostConstruct 初始化时获取,启动类ApplicationContext获取这四种方法,感兴趣的可以了解一下
    2024-01-01
  • java8 集合 多字段 分组 统计个数代码

    java8 集合 多字段 分组 统计个数代码

    这篇文章主要介绍了java8 集合 多字段 分组 统计个数代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-08-08
  • Java基础入门之switch怎么使用枚举

    Java基础入门之switch怎么使用枚举

    在Java开发中,switch语句是一种常用的流控制语句,而当使用枚举类型作为条件时,我们常常会遇到报错问题,那么该如何解决呢,下面就来详细讲讲
    2023-06-06
  • Java之打印String对象的地址

    Java之打印String对象的地址

    这篇文章主要介绍了Java之打印String对象的地址,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-09-09
  • Spring注解驱动开发实现属性赋值

    Spring注解驱动开发实现属性赋值

    这篇文章主要介绍了Spring注解驱动开发实现属性赋值,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04
  • SpringBoot+MyBatis实现动态字段更新的三种方法

    SpringBoot+MyBatis实现动态字段更新的三种方法

    字段更新是指在数据库表中修改特定列的值的操作,这种操作可以通过多种方式进行,具体取决于业务需求和技术环境,本文给大家介绍了在Spring Boot和MyBatis中,实现动态更新不固定字段的三种方法,需要的朋友可以参考下
    2025-04-04
  • spring boot + mybatis实现动态切换数据源实例代码

    spring boot + mybatis实现动态切换数据源实例代码

    这篇文章主要给大家介绍了关于spring boot + mybatis实现动态切换数据源的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-10-10
  • MyBatis配置文件元素示例详解

    MyBatis配置文件元素示例详解

    在MyBatis框架的核心配置文件中,<configuration>元素是配置文件的根元素,其他元素都要在<contiguration>元素内配置,这篇文章主要介绍了MyBatis配置文件元素,需要的朋友可以参考下
    2023-06-06
  • Java中BigDecimal类与int、Integer使用总结

    Java中BigDecimal类与int、Integer使用总结

    这篇文章主要给大家介绍了关于Java中BigDecimal类与int、Integer使用的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Java具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-07-07
  • 完美解决Server returned HTTP response code:403 for URL报错问题

    完美解决Server returned HTTP response code:403 for URL报错问题

    在调用某个接口的时候,突然就遇到了Server returned HTTP response code: 403 for URL报错这个报错,导致获取不到接口的数据,下面小编给大家分享解决Server returned HTTP response code:403 for URL报错问题,感兴趣的朋友一起看看吧
    2023-03-03

最新评论