springboot系列九,springboot整合邮件服务、整合定时任务调度

一、整合邮件服务

   如果要进行邮件的整合处理,那么你一定需要有一个邮件服务器,实际上 java 本身提供有一套 JavaMail 组件以实现邮件服务器的搭建,但是这个搭建的服务器意义不大,因为你现在搭建完成了,向一些大型的站点发送一封邮件,若干小时你就会被拉黑, 如果不想拉黑彼此之间就做一个白名单即可。

   要发送邮件,首先要知道邮件服务器,相当于我们是往对方的邮件服务器发送了一个请求。再一个,要有一个发件人。所以我们先用自己的账号密码登陆邮件服务器,再用登陆的邮件服务器给目标邮箱发送一个邮件。下面以163邮件为例。

1、添加依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

2、添加yml

spring:  
  mail:
    host: smtp.163.com
    port: 25
    username: xxxxxxx@163.com
    password: xxxxxxx
    properties:
      mail.smtp.auth: true
      mail.smtp.starttls.enable: true
      mail.smtp.starttls.required: true

3、发送示例

    @Resource
    private JavaMailSender javaMailSender ;
    @Test
    public void testSendMail() {
        SimpleMailMessage message = new SimpleMailMessage() ;    // 要发送的消息内容
        message.setFrom("18842688753@163.com");
        message.setTo("1065754909@qq.com");
        message.setSubject("测试邮件)");
        message.setText("好好学习,天天向上");
        this.javaMailSender.send(message);
    }

二、整合定时任务

  所有的系统开发里面定时调度绝对是一个核心的话题,对于定时调度的实现在实际开发之中可以使用:TimerTask、Quartz、 SpringTask 配置,实际上这里面最简单的配置就是 Spring 自己所提供的 Task 处理。

1、配置定时任务组件

MyTask.java

package com.example.demo.task;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class MyTask {
    @Scheduled(fixedRate = 2000) // 采用间隔调度,每2秒执行一次
    public void runJobA() { // 定义一个要执行的任务
        System.out.println("【*** MyTaskA - 间隔调度 ***】"
                + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")
                .format(new Date()));
    }
    @Scheduled(cron = "* * * * * ?") // 每秒调用一次
    public void runJobB() {
        System.err.println("【*** MyTaskB - 间隔调度 ***】"
                + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")
                .format(new Date()));
    }

}

2、启动类开启定时任务

@SpringBootApplication
@Import({SmsConfig.class})
@MapperScan("com.example.*.dao")
@EnableScheduling    // 启用间隔调度
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

当前任务调度只是串行任务调度,只有一个线程在工作,也就是说所有的任务是一个一个执行的处理方式,那么如果现在有一个任务所花费的时间特别的长,则其它的任务都会积压,实际开发之中很明显这种处理是不可能存在的。

如果要想启用并行的调度处理,则一定要准备出一个线程调度池,进行一个线程调度的配置类:

3、配置线程池

package com.example.demo.task;

import java.util.concurrent.Executors;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

@Configuration // 定时调度的配置类一定要实现指定的父接口
public class SchedulerConfig implements SchedulingConfigurer {
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { // 开启一个线程调度池
        taskRegistrar.setScheduler(Executors.newScheduledThreadPool(10));
    }

}

 注:如果觉得定时任务cron表达式每次初始化时已经确定了,维护起来不方便,可以考虑使用spring+quartz,把定时任务持久化到数据库中。参考:

Quartz实现数据库动态配置定时任务

原文地址:https://www.cnblogs.com/wangzhuxing/p/10189709.html