springboot异步执行service

springboot启用异步执行注解:

场景:对用户触发事件进行埋点记录分析

思路:

Controller进行token认证后讲请求信息转发给service异步执行,然后直接return结果(无需知道service执行结果)  前端也不关注controller执行结果

实现

将关键信息发给service后直接return结果

 

service异步执行,所以controller无需等待service执行完毕,可以直接返回结果(不知道我理解的对不对,欢迎指正)

 

异步执行注解配置类

package com.awservice.config;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

@Configuration
@EnableAsync
@Component
@ConfigurationProperties(prefix = "thread")
public class ExecutorConfig {
    private int corePoolSize;
    private int maxPoolSize;
    private int maxQueue;
    private String namePrefix;
    private int keepAlive;
    @Bean
    public Executor asyncServiceExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //核心线程数
        executor.setCorePoolSize(corePoolSize);
        //最大线程数
        executor.setMaxPoolSize(maxPoolSize);
        //队列大小
        executor.setQueueCapacity(maxQueue);
        //线程池中的线程的名称前缀
        executor.setThreadNamePrefix(namePrefix);
        //回收时间
        executor.setKeepAliveSeconds(keepAlive);
        // 当pool已经达到max size的时候
        // 不在新线程中执行任务,而是有调用者所在的线程来执行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //执行初始化
        executor.initialize();
        return executor;
    }
    public int getCorePoolSize() {
        return corePoolSize;
    }

    public void setCorePoolSize(int corePoolSize) {
        this.corePoolSize = corePoolSize;
    }

    public int getMaxPoolSize() {
        return maxPoolSize;
    }

    public void setMaxPoolSize(int maxPoolSize) {
        this.maxPoolSize = maxPoolSize;
    }

    public int getMaxQueue() {
        return maxQueue;
    }

    public void setMaxQueue(int maxQueue) {
        this.maxQueue = maxQueue;
    }

    public String getNamePrefix() {
        return namePrefix;
    }

    public void setNamePrefix(String namePrefix) {
        this.namePrefix = namePrefix;
    }

    public int getKeepAlive() {
        return keepAlive;
    }

    public void setKeepAlive(int keepAlive) {
        this.keepAlive = keepAlive;
    }


}

 

 

配置抽离到properties

 




----分割线----

1.异步调用

 

异步调用就是在不阻塞主线程的情况下执行高耗时方法

(高频率同样也可以,比如我这个埋点的项目)

 

2.常规异步

 

通过开启新线程实现

 

3.Springboot中启用异步方法

 

需要4个注解

 

@EnableAsync 开启异步

@Component 注册异步组件

@Autowired 注入异步组件

@Async 标注异步方法

4.进行一次异步调用

 

首先在一个Config类上标注开启异步

然后创建一个异步的组件类,就跟ServiceController 一样一样的,用Component标注,Service也行

在类内创建一个异步方法,打上Async 标记。这个方法必须是实例方法。

然后就跟注入Service一样一样的了。

5.异步事务

 

Async 方法上标注@Transactional是没用的。

Async 方法调用的Service上标注@Transactional 有效。

 

6.异步方法的内部调用

 

异步方法不支持内部调用,也就是异步方法不能写在需要调用他的类的内部。

比如Class A abcbAsync标注。此时ab的异步调用是失效的。

 

7.为什么异步方法必须是实例方法

 

因为static方法不能被Override。因为@Async 异步方法的实现原理是通过注入一个代理类到Bean中,这个代理继承这个Bean,需要覆写异步方法并执行。

然后这个东西,会被Spring放到自己维护的一个队列中。等待线程池读取并执行。

(尝试自己调优参数,核心线程数,最大线程数,队列容量,pool达到max时的策略,回收时间等等,默认的不一定是合适的)

这些解释参考自:https://blog.csdn.net/qq_15071263/article/details/80165680      (不想打字。。。。。)


  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中,您可以通过使用异步执行来提高应用程序的性能和响应能力。Spring Boot提供了多种方式来实现异步执行,下面是其中几种常用的方法: 1. 使用@Async注解:在Spring Boot中,可以使用@Async注解将方法标记为异步执行。首先,在主类上添加@EnableAsync注解以启用异步执行功能。然后,在需要异步执行的方法上添加@Async注解。这样,当调用该方法时,Spring会将其放入线程池中并立即返回,而不会阻塞主线程。 ```java @SpringBootApplication @EnableAsync public class YourApplication { public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } } ``` ```java @Service public class YourService { @Async public void yourAsyncMethod() { // 异步执行的逻辑 } } ``` 2. 使用CompletableFuture:CompletableFuture是Java 8中引入的一个异步编程工具类,可以很方便地实现异步执行。在Spring Boot中,您可以将CompletableFuture与@Async一起使用来实现异步执行。 ```java @Service public class YourService { @Async public CompletableFuture<Result> yourAsyncMethod() { // 异步执行的逻辑 return CompletableFuture.completedFuture(result); } } ``` 3. 使用ThreadPoolTaskExecutor:如果您需要更多的控制权,您可以自定义一个ThreadPoolTaskExecutor来管理异步任务的线程池。在应用程序配置文件中进行配置,然后在需要异步执行的方法上使用@Async注解。 ```java @Configuration @EnableAsync public class AsyncConfig implements AsyncConfigurer { @Override public Executor getAsyncExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(10); executor.setMaxPoolSize(20); executor.setQueueCapacity(30); executor.initialize(); return executor; } } ``` ```java @Service public class YourService { @Async public void yourAsyncMethod() { // 异步执行的逻辑 } } ``` 通过以上方法,您可以在Spring Boot应用程序中实现异步执行,并提升应用程序的性能和响应能力。请根据您的具体需求选择适合的方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值