SpringBoot任务

异步任务:

  1. 在方法上添加@Async注解 表明这个方法是一个异步的方法
package com.king.service;

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

@Service
public class AsyncService {

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

}

  1. 在主程序上添加 开启一步注解功能 @EnableAsync
package com.king;

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

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

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

}

定时任务

两个注解:

@Scheduled(cron="") :用在方法之上

@EnableScheduling :开启基于注解的定时任务

参数

字段 允许值 允许的特殊字符
秒(Seconds) 0~59的整数 , - * / 四个字符
分(Minutes 0~59的整数 , - * / 四个字符
小时(Hours 0~23的整数 , - * / 四个字符
日期(DayofMonth 1~31的整数(但是你需要考虑你月的天数) ,- * ? / L W C 八个字符
月份(Month 1~12的整数或者 JAN-DEC , - * / 四个字符
星期(DayofWeek 1~7的整数或者 SUN-SAT (1=SUN) , - * ? / L C # 八个字符
年(可选,留空)(Year 1970~2099 , - * / 四个字符

常见例子:

  (1)0 0 2 1 * ? * 表示在每月的1日的凌晨2点调整任务

  (2)0 15 10 ? * MON-FRI 表示周一到周五每天上午10:15执行作业

  (3)0 15 10 ? 6L 2002-2006 表示2002-2006年的每个月的最后一个星期五上午10:15执行作

  (4)0 0 10,14,16 * * ? 每天上午10点,下午2点,4点

  (5)0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时

  (6)0 0 12 ? * WED 表示每个星期三中午12点

  (7)0 0 12 * * ? 每天中午12点触发

  (8)0 15 10 ? * * 每天上午10:15触发

  (9)0 15 10 * * ? 每天上午10:15触发

  (10)0 15 10 * * ? * 每天上午10:15触发

  (11)0 15 10 * * ? 2005 2005年的每天上午10:15触发

  (12)0 * 14 * * ? 在每天下午2点到下午2:59期间的每1分钟触发

  (13)0 0/5 14 * * ? 在每天下午2点到下午2:55期间的每5分钟触发

  (14)0 0/5 14,18 * * ? 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发

  (15)0 0-5 14 * * ? 在每天下午2点到下午2:05期间的每1分钟触发

  (16)0 10,44 14 ? 3 WED 每年三月的星期三的下午2:10和2:44触发

  (17)0 15 10 ? * MON-FRI 周一至周五的上午10:15触发

  (18)0 15 10 15 * ? 每月15日上午10:15触发

  (19)0 15 10 L * ? 每月最后一日的上午10:15触发

  (20)0 15 10 ? * 6L 每月的最后一个星期五上午10:15触发

  (21)0 15 10 ? * 6L 2002-2005 2002年至2005年的每月的最后一个星期五上午10:15触发

  (22)0 15 10 ? * 6#3 每月的第三个星期五上午10:15触发

实例:

package com.king.service;

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

@Service
public class ScheduleService {

    /**
     * cron表达式
     *    0 * * * * MON-FRI
     * second   秒
     * minute   分
     * hour   时
     * day of month   日
     * month    月
     * day of week   周几
     *
     */
    //@Scheduled(cron = "0 * * * * MON-FRI")
    //@Scheduled(cron = "0,1,2,3,4 * * * * MON-FRI")    特殊字符(,)   枚举
    //@Scheduled(cron = "0-4 * * * * MON-FRI")    特殊字符(-)   区间
    @Scheduled(cron = "0/4 * * * * MON-FRI")     //每4秒执行一次
    public void hello(){
        System.out.println("hello. 定时任务执行了..");
    }

}

启动类:

package com.king;

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

@EnableAsync   //开启异步注解功能
@EnableScheduling   //开启基于注解的定时任务
@SpringBootApplication
public class Springboot04TaskApplication {

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

}

邮件任务

  1. 在pom.xml文件中导入 邮件的相关依赖

    1. <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-mail</artifactId>
      </dependency>
      
  2. 在配置文件中application.properties中配置邮件相关配置

    #配置发送者的邮箱账号
    spring.mail.username=邮箱账号
    #密码
    spring.mail.password=授权码
    #发送方式  域名服务器
    spring.mail.host=smtp.qq.com
    #打开安全连接
    spring.mail.properties.mail.smtp.ssl.enable=true
    
  3. 发送消息

    package com.king;
    
    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.JavaMailSender;
    import org.springframework.mail.javamail.MimeMessageHelper;
    
    import javax.mail.MessagingException;
    import javax.mail.internet.MimeMessage;
    import java.io.File;
    
    @SpringBootTest
    class Springboot04TaskApplicationTests {
    
        @Autowired
        JavaMailSender javaMailSender;
    
        @Test
        void contextLoads() {
            /**
             * 简单邮件
             */
            SimpleMailMessage message = new SimpleMailMessage();
            //邮件设置
            message.setSubject("通知-开会");   //设置标题
            message.setText("明天11点开会");     //设置内容
            message.setTo("956847690@qq.com");      //发给谁
            message.setFrom("956847690@qq.com");     //由谁发出的
            javaMailSender.send(message);
        }
    
        @Test
        public void test2() throws MessagingException {
            /**
             * 复杂邮件  带附件
             */
            //创建一个复杂的消息邮件
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            //邮件设置
            helper.setSubject("通知-开会");   //设置标题
            helper.setText("<b style='color:red'>今晚开会</b>",true);     //设置内容
            helper.setTo("956847690@qq.com");      //发给谁
            helper.setFrom("956847690@qq.com");     //由谁发出的
    
            //上传文件
            helper.addAttachment("1.jpg",new File("E:\photos\头像图片\蜡笔小新.jpg"));
            helper.addAttachment("2.jpg",new File("E:\photos\头像图片\文艺范.jpeg"));
    
            javaMailSender.send(mimeMessage);
        }
    
    }
    
    
原文地址:https://www.cnblogs.com/KingTL/p/13073448.html