SpringBoot与任务

时间:2020-03-24
本文章向大家介绍SpringBoot与任务,主要包括SpringBoot与任务使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

一、异步任务:不需要任何的依赖和配置,只需要加两个注解而已

1.在启动类上开启异步任务注解

package com.atguigu;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;

@EnableAsync//开启异步注解功能
@SpringBootApplication
public class SpringbootTaskApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootTaskApplication.class, args);
    }

}

2.在需要执行异步任务的方法上标上@Async注解,通常写在业务层的方法上

package com.atguigu.service;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class AsyncService {
    @Async//告诉spring这个方法是异步的
    public void hello(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.err.println("数据处理中");
    }
}

二、定时任务:不需要引入任何依赖也不需要任何配置,只需要两个注解

1.先在在启动类上开启定时任务注解

package com.atguigu;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling//开启定时任务注解
@SpringBootApplication
public class SpringbootTaskApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootTaskApplication.class, args);
    }

}

2.在需要执行的定时任务的方法上标上@Scheduled注解,并写上cron表达式,通常写在业务层的方法上

package com.atguigu.service;

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

@Service
public class ScheduleService {
    /**
     * second(秒), minute(分), hour(时), day of month(日), month(月), day of week(周几).
     * 0 * * * * MON-FRI
     *  【0 0/5 14,18 * * ?】 每天14点整,和18点整,每隔5分钟执行一次
     *  【0 15 10 ? * 1-6】 每个月的周一至周六10:15分执行一次
     *  【0 0 2 ? * 6L】每个月的最后一个周六凌晨2点执行一次
     *  【0 0 2 LW * ?】每个月的最后一个工作日凌晨2点执行一次
     *  【0 0 2-4 ? * 1#1】每个月的第一个周一凌晨2点到4点期间,每个整点都执行一次;
     */
    // @Scheduled(cron = "0 * * * * MON-SAT")
    //@Scheduled(cron = "0,1,2,3,4 * * * * MON-SAT")
    // @Scheduled(cron = "0-4 * * * * MON-SAT")
    @Scheduled(cron = "0/4 * * * * MON-SAT")  //每4秒执行一次
    public void hello(){
        System.err.println("hello.......................");
    }
}

  注:cron表达式语法:

字段 允许值 允许的特殊字符
秒 0-59 , - * /
分 0-59 , - * /
小时 0-23 , - * /
日期 1-31 , - * ? / L W C
月份 1-12 , - * /
星期 0-7或SUN-SAT 0,7是SUN , - * ? / L C #

三、邮件任务:

1.先导入邮件的starter

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

2.在application.properties文件中配置邮件属性

#发件人的邮箱名称
spring.mail.username=3524381995@qq.com
#发件人的识别码,不是邮箱密码
spring.mail.password=qsdlcqlyhvowcjga
#使用qq邮箱发送
spring.mail.host=smtp.qq.com
#如果报安全连接错误,就加上这个配置
spring.mail.properties.mail.ssl.enable=true

3.测试代码

package com.atguigu;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@SpringBootTest
class SpringbootTaskApplicationTests {
    @Autowired
    JavaMailSenderImpl javaMailSender;
    @Test
    void Test01() {//简单邮件发送,不添加附件就可以用这一个
        //1.创建一个简单的消息邮件
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        //邮件设置
        simpleMailMessage.setSubject("通知-今晚开会");//设置邮件标题
        simpleMailMessage.setText("今晚7点半开会");//设置邮件内容
        simpleMailMessage.setTo("3373902716@qq.com");//设置收件人邮箱
        simpleMailMessage.setFrom("3524381995@qq.com");//设置发件人邮箱
        javaMailSender.send(simpleMailMessage);
    }
    @Test
    public void Test02() throws MessagingException {//复杂邮件发送,添加附件可以用这一个,也可以设置邮件内容样式
        //1.创建一个复杂的消息邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setSubject("通知-今晚开会");//设置邮件标题
        helper.setText("今晚7点半开会");//设置邮件内容,也可以在这里面设置html格式
        helper.setTo("3373902716@qq.com");//设置收件人邮箱
        helper.setFrom("3524381995@qq.com");//设置发件人邮箱
        //上传文件
        helper.addAttachment("文件名称",new File("文件路径"));
        javaMailSender.send(mimeMessage);
    }

}

  


原文地址:https://www.cnblogs.com/wxl123/p/12561822.html