• springboot之异步任务、邮件任务、定时任务


    springboot之异步任务

    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.5.5version>
            <relativePath/> 
        parent>
        <groupId>com.examplegroupId>
        <artifactId>springboot-taskartifactId>
        <version>0.0.1-SNAPSHOTversion>
        <name>springboot-taskname>
        <description>Demo project for Spring Bootdescription>
        <properties>
            <java.version>1.8java.version>
        properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
    
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
                <scope>testscope>
            dependency>
        dependencies>
    
    
    project>
    
    
    • 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
    • 33
    • 34
    package com.example.task;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.scheduling.annotation.EnableAsync;
    
    @SpringBootApplication
    @EnableAsync // 开启异步注解功能
    public class SpringbootTaskApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringbootTaskApplication.class, args);
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    package com.example.task.service;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class AsyncController {
    
        @Autowired
        AsyncService asyncService;
    
        @GetMapping("hello")
        public String hello(){
            asyncService.hello(); // 停止3s
            return "ok";
        }
    }
    
    
    
    
    package com.example.task.service;
    
    import org.springframework.scheduling.annotation.Async;
    import org.springframework.stereotype.Service;
    
    @Service
    public class AsyncService {
    
        // 告诉spring这是一个异步的方法
        // 如果不是异步任务,controller会等待3s,才返回ok
        // 异步任务的话,controller会直接返回ok,service会正常执行
        @Async
        public void hello(){
    
            System.out.println("========数据开始处理============");
    
            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
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    springboot之邮件发送

    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.5.5version>
            <relativePath/> 
        parent>
        <groupId>com.examplegroupId>
        <artifactId>springboot-taskartifactId>
        <version>0.0.1-SNAPSHOTversion>
        <name>springboot-taskname>
        <description>Demo project for Spring Bootdescription>
        <properties>
            <java.version>1.8java.version>
        properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
    
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
                <scope>testscope>
            dependency>
    
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-mailartifactId>
            dependency>
        dependencies>
    
    
    project>
    
    
    • 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
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    spring:
      mail:
        host: smtp.qq.com
        username: 你的qq号
        password: qq邮箱的授权码,当然这些东西用网易、移动那些也可以
        #    开启加密验证
        properties.mail.smtl.ssl.enable: true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    package com.example.task;
    
    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 SpringbootTaskApplicationTests {
    
        @Autowired
        JavaMailSenderImpl javaMailSender;
    
        // 发送一个简单的邮件
        @Test
        void contextLoads() {
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            simpleMailMessage.setSubject("这是邮件的主题");
            simpleMailMessage.setText("这是邮件的内容");
    
            // 接收者
            simpleMailMessage.setTo("1393519068@qq.com");
            // 发送者
            simpleMailMessage.setFrom("1393519068@qq.com");
    
    
            javaMailSender.send(simpleMailMessage);
    
        }
    
    
    
    
    
        // 发送一个复杂的邮件
        @Test
        void contextLoads2() throws MessagingException {
    
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            // 组装
            // 第二个参数为 多文件
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
    
            mimeMessageHelper.setSubject("这是邮件的主题");
            // true 会解析html,否则是纯文本
            mimeMessageHelper.setText("

    这是邮件的内容

    "
    ,true); // 附件 mimeMessageHelper.addAttachment("1.txt",new File("C:\\Users\\EDY\\Desktop\\新文件 1.txt")); // 接收者 mimeMessageHelper.setTo("1393519068@qq.com"); // 发送者 mimeMessageHelper.setFrom("1393519068@qq.com"); javaMailSender.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
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70

    springboot之定时任务

    @SpringBootApplication
    // 开启使用定时任务,使用spring自带的定时任务
    @EnableScheduling
    public class CrawlerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(CrawlerApplication.class,args);
        }
    }
    
    	// 这两个不是在同一个类中,用spring定时任务的这个类需要交给spring管理
     	// 添加定时任务配置,这里一般是用corn表达式
        // initialDelay,项目启动成功后,多久执行任务,单位毫秒
        // fixedDelay,任务执行完成后,间隔多久下一次任务执行,单位毫秒
        @Scheduled(initialDelay = 1000, fixedDelay = 10000)
        public void run(){
        }
    
    
    /**
    cron:cron表达式,指定任务在特定时间执行;
    fixedDelay:上一次任务执行完后多久再执行,参数类型为long,单位ms
    fixedDelayString:与fixedDelay含义一样,只是参数类型变为String
    fixedRate:按一定的频率执行任务,参数类型为long,单位ms
    fixedRateString: 与fixedRate的含义一样,只是将参数类型变为String
    initialDelay:延迟多久再第一次执行任务,参数类型为long,单位ms
    initialDelayString:与initialDelay的含义一样,只是将参数类型变为String
    zone:时区,默认为当前时区,一般没有用到
    */
    
    • 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
    // 使用cron表达式
    package com.example.task.service;
    
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    
    @Component
    public class TaskScheduling {
    
        @Scheduled(cron = "0/10 * * * * ?")
        public void testScheduling(){
            System.out.println("testScheduling");
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    部分内容转载自:
    https://bilibili.com/video/BV1PE411i7CV/?p=52&spm_id_from=pageDriver&vd_source=64c73c596c59837e620fed47fa27ada7

    世界是复杂的,但又不是随机的,知识也应当如此。求知的道路,意味着永恒的疲倦以及偶尔的惊喜。

    可能性的艺术:比较政治学30讲
    刘瑜

  • 相关阅读:
    IPv6详解
    【校招VIP】前端算法考察之链表算法
    【2021研电赛】基于深度学习的蛋白质与化合物结合性质预测
    Spring Cloud:三【详细】
    java子类向父类转型再转回子类
    pytest+allure生成测试报告
    DevOps|AGI : 智能时代研发效能平台新引擎(上)
    什么是HTML?
    Zookeeper集群 + Kafka集群
    【C语言进阶篇】数组&&指针&&数组笔试题
  • 原文地址:https://blog.csdn.net/weixin_44431371/article/details/136479742