SpringBoot中异步注解@Async介绍

介绍在SpringBoot项目中,使用@Async不生效的原因介绍和分析;

代码参考gitee仓库:spring-boot-2022-05: 主要是介绍Spring框架注解、常用的功能的使用案例,记录平时遇到的技术知识点,进行实践操作; - Gitee.com

 一、常见原因:

1.启动类中没有添加注解@EnableAsync;

2.同一个类中调用含有@Async的方法;因为@Transactional和@Async是采用Spring AOP原理实现的,需要通过代理对象调用其方法。

3.方法必须是public修饰,且返回值是void或Future。

4.使用ThreadPoolTaskExecutor对象创建自定义线程池?

5.关于@Async("param")中参数param含义?

正确使用步骤事项:

第一步:启动类中应该添加注解@EnableAsync;

第二步:执行方法和异步方法不在同一个类中。

第三步:异步方法必须是public 修饰,且返回值是void或Future;

二、具体案例:

1.启动类需要添加注解@EnableAsync

package com.dbzhang;

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

/**
 * @program: spring-boot-2022-05
 * @description:
 * @author: zdb
 * @motto: 认真写好每一行代码
 * @create: 2022-05-05 13:15
 */
@SpringBootApplication
@EnableAsync
public class ApplicationRun {
    public static void main(String[] args) {
        SpringApplication.run(ApplicationRun.class, args);
    }
}

2.PlayerService业务类:通过代理对象调用异步方法发送短信asyncMethodClass.sendMessage(playerId, msg);

package com.dbzhang.asyc;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @program: spring-boot-2022-05
 * @description:
 * @author: zdb
 * @motto: 认真写好每一行代码
 * @create: 2022-10-18 13:11
 */
@Component
@Slf4j
public class PlayerService {

    @Resource
    private AsyncMethodClass asyncMethodClass;


    public void register(Long playerId) {
        log.info("begin----注册用户编号,playerId:{}", playerId);
        //异步发送短信
        String msg = "异步发送短信";
        asyncMethodClass.sendMessage(playerId, msg);
        log.info("end----注册用户编号,playerId:{}", playerId);
    }
}

3.AsyncMethodClass中异步方法,使用@Async("param")标记异步方法,其中param参数是引用线程池的bean名称,但是Spring容器中必须存在这个bean。

package com.dbzhang.asyc;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @program: spring-boot-2022-05
 * @description:
 * @author: zdb
 * @motto: 认真写好每一行代码
 * @create: 2022-10-18 13:12
 */
@Component
@Slf4j
public class AsyncMethodClass {

    /**
     * 测试调用异常方法:发送短信
     * 
     */
    @Async("threadPoolTaskExecutor")
    public void sendMessage(Long playerId, String msg) {
        log.info("玩家注册编号playerId:{},发送短信信息msg:{}", playerId, msg);
    }
}

4.自定义线程池ThreadPoolTaskExector,并交给Spring容器管理。

package com.dbzhang.asyc;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;


/**
 * @program: spring-boot-2022-05
 * @description: 自定义线程池配置
 * @author: zdb
 * @motto: 认真写好每一行代码
 * @create: 2022-10-19 12:50
 */
@Configuration
public class AsyncThreadParam {

    @Value("${threadPool.corePoolSize}")
    private int corePoolSize;

    @Value("${threadPool.maxPoolSize}")
    private int maxPoolSize;

    @Value("${threadPool.keepAliveSeconds}")
    private int keepAliveSeconds;

    @Value("${threadPool.queueCapacity}")
    private int queueCapacity;

    @Value("${threadPool.namePrefix}")
    private String namePrefix;

    /**
     * 初始化线程池参数
     *
     * @return
     */
    @Bean(value = "threadPoolTaskExecutor")
    public ThreadPoolTaskExecutor initThreadPoolTaskExecutor() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setCorePoolSize(corePoolSize);
        threadPoolTaskExecutor.setMaxPoolSize(maxPoolSize);
        threadPoolTaskExecutor.setQueueCapacity(queueCapacity);
        threadPoolTaskExecutor.setKeepAliveSeconds(keepAliveSeconds);
        threadPoolTaskExecutor.setThreadNamePrefix(namePrefix);
//        threadPoolTaskExecutor.setRejectedExecutionHandler();
        return threadPoolTaskExecutor;
    }
}

5.配置文件中执行线程参数:

#自定义线程池ThreadPoolTaskExecutor参数
threadPool:
  corePoolSize: 2
  maxPoolSize: 5
  keepAliveSeconds: 60
  queueCapacity: 10
  namePrefix: async-zdb-

6.测试类:

package com.dbzhang.async;

import com.dbzhang.asyc.PlayerService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * @program: spring-boot-2022-05
 * @description:
 * @author: zdb
 * @motto: 认真写好每一行代码
 * @create: 2022-10-18 13:18
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class AsyncTest {

    @Autowired
    private PlayerService playerService;

    @Test
    public void testAsync() {
        playerService.register(123456789L);
    }
}

 7.执行结果:

SpringBoot中异步注解@Async介绍_第1张图片

 

你可能感兴趣的:(Spring框架,SpringBoot框架,spring,boot,spring)