springboot整合shiro关于任务-入门3

任务

  • 异步任务~

  • 邮件发送~

  • 定时任务~timer

1.异步任务

写一个AsyncService类,目的:睡眠三秒

package com.kuang.springboot09test.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("数据正在处理");
    }
}

写个AsyncController类

package com.kuang.springboot09test.controller;

import com.kuang.springboot09test.service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AsynController {
    @Autowired
    AsyncService asyncService;

    @RequestMapping("/hello")
    public String hello() {
        asyncService.hello();//停止三秒,转圈
        return "ok";
    }
}

在Springboot09TestApplication主配置类中,开启异步注解功能

package com.kuang.springboot09test;

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

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

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

}

重点:

开启异步任务功能只有两步

1. 在方法上加上@Async,告诉Spring这是异步的方法

2. 在主配置类加上@EnableAsync,开启异步注解功能

2.邮件发送

导入pom依赖

<!--javax,email-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

在application.properties,添加qq邮箱的配置

spring.mail.username=319244776@qq.com
spring.mail.password=xwtyhwsemovybjih
spring.mail.host=smtp.qq.com
#开启加密验证
spring.mail.properties.mail.smtp.ssl.enable=true

测试类测试

package com.kuang.springboot09test;

import com.jayway.jsonpath.internal.function.numeric.Min;
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.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

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

@SpringBootTest
class Springboot09TestApplicationTests {

    @Autowired
    JavaMailSenderImpl mailSender;

    @Test
    void contextLoads() throws MessagingException {
        //一个简单的邮件
        //SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        //simpleMailMessage.setSubject("洋洋,你要加油");
        //simpleMailMessage.setText("谢谢你的爱");
        //simpleMailMessage.setTo("319244776@qq.com");
        //simpleMailMessage.setFrom("319244776@qq.com");
        //mailSender.send(simpleMailMessage);

        //一个复杂的邮件
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        //组装
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        helper.setSubject("洋洋,你要开心");
        helper.setText("<p style='color:red;'>谢谢你的喜欢</p>", true);
        //附件
        helper.addAttachment("美女.jpg", new File("E:\\图片\\美女.jpg"));
        helper.addAttachment("ee8fb39dd1654e32b9a0e9a403fe4bf.jpg", new File("E:\\图片\\ee8fb39dd1654e32b9a0e9a403fe4bf.jpg"));
        helper.setTo("319244776@qq.com");
        helper.setFrom("319244776@qq.com");
        mailSender.send(mimeMessage);
    }

}

3.定时任务

TaskScheduler    任务调度者
TaskExecutor    任务执行者
    
@EnableScheduling //开启定时功能的注解
@Scheduled(cron = "0 17 19 * * ?") //作为一个触发源添加到一个方法中
首先要了解cron表达式
 
字段 允许值 允许的特殊字符
秒(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 , - * /    四个字符

常用的cron表达式实例
“0 0 10,14,16 * * ?” 每天上午10点,下午2点,4点
“0 0/30 9-17 * * ?” 朝九晚五工作时间内每半小时
“0 0 12 ? * WED” 表示每个星期三中午12点
“0 0 12 * * ?” 每天中午12点触发
“0 15 10 ? * *” 每天上午10:15触发
“0 15 10 * * ?” 每天上午10:15触发
“0 15 10 * * ? " 每天上午10:15触发
“0 15 10 * * ? 2005” 2005年的每天上午10:15触发
“0 * 14 * * ?” 在每天下午2点到下午2:59期间的每1分钟触发
“0 0/5 14 * * ?” 在每天下午2点到下午2:55期间的每5分钟触发
“0 0/5 14,18 * * ?” 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
“0 0-5 14 * * ?” 在每天下午2点到下午2:05期间的每1分钟触发
“0 10,44 14 ? 3 WED” 每年三月的星期三的下午2:10和2:44触发
“0 15 10 ? * MON-FRI” 周一至周五的上午10:15触发
“0 15 10 15 * ?” 每月15日上午10:15触发
“0 15 10 L * ?” 每月最后一日的上午10:15触发
“0 15 10 ? * 6L” 每月的最后一个星期五上午10:15触发
“0 15 10 ? * 6L 2002-2005” 2002年至2005年的每月的最后一个星期五上午10:15触发
“0 15 10 ? * 6#3” 每月的第三个星期五上午10:15触发
"/5 * * * * ?” 每隔5秒执行一次
“0 */1 * * * ?” 每隔1分钟执行一次
“0 0 23 * * ?” 每天23点执行一次
“0 0 1 * * ?” 每天凌晨1点执行一次
“0 0 1 1 * ?” 每月1号凌晨1点执行一次
“0 0 23 L * ?” 每月最后一天23点执行一次
“0 0 1 ? * L” 每周星期天凌晨1点实行一次
“0 26,29,33 * * * ?” 在26分、29分、33分执行一次
“0 0 0,13,18,21 * * ?” 每天的0点、13点、18点、21点都执行一次


ScheduledService类
package com.kuang.springboot09test.service;

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

@Service
public class ScheduledService {
    //在一个特定的时间执行这个方法~ Timer
    //cron 表达式
    //秒 分 时 日 月 周几
    /*
        30 17 19 * * ?      每天的19点17分30秒执行
        30 0/5 10,18 * * ?  每天的10点和18点,每隔5分钟执行一次
     */
    @Scheduled(cron = "30 17 19 * * ?")
    public void hello(){
        System.out.println("hello,你被执行了");
    }
}

主配置类添加开始定时功能的注解

package com.kuang.springboot09test;

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

@SpringBootApplication

@EnableAsync //开启异步注解功能
@EnableScheduling //开启定时功能的注解
public class Springboot09TestApplication {

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

}

 

posted @ 2021-12-05 19:44  蔡地像徐坤  阅读(51)  评论(0编辑  收藏  举报