• SpringBoot任务详解


    SpringBoot任务详解

    • 异步任务
    • 定时任务
    • 邮件发送

    1、异步任务

    1. 写一个service

    AsyncService.java

    package com.qian.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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    @Async 告诉spring这是一个异步的方法

    1. 在启动里加开启异步注解
    package com.qian;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.scheduling.annotation.EnableAsync;
    
    //开启异步注解功能
    @EnableAsync
    @SpringBootApplication
    public class SpringbootTestApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringbootTestApplication.class, args);
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    @EnableAsync 开启异步注解功能

    1. controller测试
    package com.qian.controller;
    
    import com.qian.service.AsyncService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController  //返回字符串,用RestController
    public class AsyncController {
        @Autowired
        AsyncService asyncService;
        @RequestMapping("/hello")
        public String hello(){
            asyncService.hello();//停止三秒
            return "OK";
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    //页面会即时刷新返回ok,但是后端会等待3秒后才输出“数据正在处理中”

    2、邮件发送

    1. 相比于之前的Javaweb,springboot在邮件发送功能上同样简化了很多

    2. 首先导入相关依赖

    
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-mailartifactId>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. 写mail配置
    #加密后的密码,从qq邮箱开启POP3/SMTP服务中拿到
    spring.mail.password=fiqktykwcdpybdjc   
    spring.mail.username=1831213271@qq.com
    spring.mail.host=smtp.qq.com
    # 开启加密验证(QQ邮箱有的)
    spring.mail.properties.mail.smtp.ssl.enable=true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    1. 写实现功能
    @SpringBootTest
    class SpringbootTestApplicationTests {
        @Autowired
        JavaMailSenderImpl mailSender;
    
        @Test
        void contextLoads() {
            //一个简单的邮件发送
            SimpleMailMessage mailMessage = new SimpleMailMessage();
            mailMessage.setSubject("小狂神你好呀~"); //发送主题(标题)
            mailMessage.setText("谢谢你");  //发送内容
            mailMessage.setTo("1831213271@qq.com");  //发送给谁
            mailMessage.setFrom("1831213271@qq.com");  //谁发送
            mailSender.send(mailMessage);
        }
    
        @Test
        void contextLoads2() throws MessagingException {
            //一个复杂的邮件发送
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            //组装~
            //正文
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
            helper.setSubject("小狂神你好呀~plus");
            helper.setText("

    谢谢你的课程~

    "
    ,true); //附件 helper.addAttachment("3.jpg",new File("D:\\我的文件\\我的素材\\图片\\3.jpg")); helper.addAttachment("1.jpeg",new File("D:\\我的文件\\我的素材\\图片\\1.jpeg")); helper.setTo("1831213271@qq.com"); //发送给谁 helper.setFrom("1831213271@qq.com"); //谁发送 mailSender.send(mimeMessage); }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    然后邮箱就可以收到啦~

    3、定时执行任务

    1. 先在启动程序上加上注解
    package com.qian;
    
    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 SpringbootTestApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringbootTestApplication.class, args);
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    @EnableScheduling // 开启定时功能的注解

    1. 写一个定时程序
    package com.qian.service;
    
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Service;
    
    @Service
    public class ScheduledService {
        //在一个特定的时间执行这个方法!  Timer
    
    
        //cron表达式
        //秒 分 时 日 月 周几到周几  0-7意思是周日到周日就是每天
        //0 15 10 ? * 1-6 每个月的周一到周六的10:15执行
        @Scheduled(cron = "0/2 * * * * ?")  //每天的15:24:00执行
        public void hello(){
            System.out.println("hello,你被执行了");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    **@Scheduled(cron = “0/2 * * * * ?”) ** cron表达式

  • 相关阅读:
    NPW(监控片的)的要点精讲
    解决MySQL数据库拒绝远程计算机连接问题
    Python中`*args`和`**kwargs`的用法
    企业在数字化转型中可以实现的7个目标
    基于51单片机电子微波炉控制系统(源程序+仿真+原理图+全套资料)
    web课程设计网页规划与设计 :DW旅游主题网页设计——凤阳智慧旅游官方-地方旅游网站模板html源码HTML+CSS+JavaScript
    测试岗电话面试——必问题型
    数据链路层 随机接入-CSMA/CA协议
    (02)Cartographer源码无死角解析-(21) MapBuilder→AddTrajectoryBuilder()
    计算机二级WPS 选择题(模拟和解析七)
  • 原文地址:https://blog.csdn.net/m0_56116754/article/details/126195984