Spring-Async调用

前言

  1. Spring 自带的异步调用,方便使用
  2. Spring 的异步存储在内存中,突然宕机会导致任务丢失,只能执行不重要的操作,比如:日志写入。
  3. 应用场景:发短信、邮件、消息通知、统计,和正常业务没有直接关联。上下文无关,数据不重要,可以出错。
  4. 正常业务上下文关联,异步不能出错。后面操作依赖前面操作的结果,需要等待。

步骤

  1. 参考博客。文章来自大佬博客,我只是实践一下。
  2. 思维导图。重点内容描述,并发步骤。
  3. pom依赖。pom依赖文件
  4. Service 层。用于编写 同步 和 异步方法。
  5. main 类。
  6. Test 类。用于测试 同步 和 异步方法。
  7. application.yml。对配置类的讲解,实际上就是自动装配方案,加上一些描述。
  8. 学习方法。
  9. 注意事项。

1. 参考博客

  1. 芋道 Spring Boot 异步任务入门 https://www.iocoder.cn/Spring-Boot/Async-Job/

2. 思维导图

  1. 待补充

3. pom依赖

<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.jtfr</groupId>
    <artifactId>Spring-async</artifactId>
    <version>1.0.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
        <relativePath/>
    </parent>

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

        <!-- 方便等会写单元测试 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

4. Service 层


5. main 类

package com.jtfr;

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

@SpringBootApplication
@EnableAsync // 开启 Spring 异步的功能
public class SpringBootMain {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootMain.class, args);
    }
}

6. Test 类

package com.jtfr.service;

import com.jtfr.SpringBootMain;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootMain.class)
public class TestServiceTest {

    private static final Logger logger = LoggerFactory.getLogger(TestServiceTest.class);

    @Autowired
    private TestService testService;

    /**
     * 同步测试调用
     * 1. 完全的顺序执行
     */
    @Test
    public void test01(){
        long l = System.currentTimeMillis();

        logger.info("开始执行");

        testService.execute01();
        testService.execute02();


        logger.info("结束执行,耗时 {} 毫秒", System.currentTimeMillis() - l);
    }

    /**
     * 异步测试调用,无返回结果
     * 1. 执行结果完全异步,执行主流程执行完就结束。
     */
    @Test
    public void test02(){
        long l = System.currentTimeMillis();

        logger.info("执行开始");

        testService.executeAsync01();
        testService.executeAsync02();

        logger.info("结束执行,耗时 {} 毫秒", System.currentTimeMillis() - l);
    }

    /**
     * 异步测试调用,有返回结果
     * 1. 执行结果,取决于最长执行时间结果的返回
     */
    @Test
    public void test03() throws ExecutionException, InterruptedException {
        long l = System.currentTimeMillis();

        logger.info("执行开始");

        // 执行任务
        Future<Integer> future01 = testService.executeAsyncWithFuture01();
        Future<Integer> future02 = testService.executeAsyncWithFuture02();
        // 阻塞等待结果
        logger.info(String.valueOf(future01.get()));
        logger.info(String.valueOf(future02.get()));

        logger.info("结束执行,耗时 {} 毫秒, 等待返回结果", System.currentTimeMillis() - l);
    }
}

7. application.yml

spring:
  task:
    # Spring 执行器配置,对应 TaskExecutionProperties 配置类。对于 Spring 异步任务,会使用该执行器。
    execution:
      thread-name-prefix: task- # 线程池的线程名的前缀。默认为 task- ,建议根据自己应用来设置
      pool: # 线程池相关
        core-size: 8 # 核心线程数,线程池创建时候初始化的线程数。默认为 8 。
        max-size: 20 # 最大线程数,线程池最大的线程数,只有在缓冲队列满了之后,才会申请超过核心线程数的线程。默认为 Integer.MAX_VALUE
        keep-alive: 60s # 允许线程的空闲时间,当超过了核心线程之外的线程,在空闲时间到达之后会被销毁。默认为 60 秒
        queue-capacity: 200 # 缓冲队列大小,用来缓冲执行任务的队列的大小。默认为 Integer.MAX_VALUE 。
        allow-core-thread-timeout: true # 是否允许核心线程超时,即开启线程池的动态增长和缩小。默认为 true 。
      shutdown:
        await-termination: true # 应用关闭时,是否等待定时任务执行完成。默认为 false ,建议设置为 true
        await-termination-period: 60 # 等待任务完成的最大时长,单位为秒。默认为 0 ,根据自己应用来设置

8. 学习方法

  1. 百度啊,百度应用案例。

9. 注意事项

  1. VM 应用的正常优雅关闭,保证异步任务都被执行完成。
  2. 还没有处理 - 编写异步异常处理器 GlobalAsyncExceptionHandler ,记录异常日志,进行监控告警。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值