zoukankan      html  css  js  c++  java
  • Spring Boot使用 @Async 注解进行异步调用

    Spring Boot使用 @Async 注解进行异步调用

    创建异步调用线程池配置代码:

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.scheduling.annotation.AsyncConfigurer;
    import org.springframework.scheduling.annotation.EnableAsync;
    import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
    
    import java.util.concurrent.Executor;
    import java.util.concurrent.ThreadPoolExecutor;
    
    @Configuration
    @EnableAsync // 启用异步调用
    public class AsyncConfig implements AsyncConfigurer {
    
    
        @Bean("taskExecutor")
        public Executor getCutChartExecutor() {
            //线程池
            ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
            taskExecutor.setCorePoolSize(3);
            taskExecutor.setMaxPoolSize(10);
            taskExecutor.setQueueCapacity(100);
            taskExecutor.setKeepAliveSeconds(60);
            taskExecutor.setThreadNamePrefix("async-task-thread-pool");
            //rejection-policy:当pool已经达到max size的时候,如何处理新任务
            //CALLER_RUNS:不在新线程中执行任务,而是由调用者所在的线程来执行
            //对拒绝task的处理策略
            taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
            taskExecutor.initialize();
            return taskExecutor;
        }
    
    }

    创建异步调用方法:

    import lombok.extern.slf4j.Slf4j;
    import org.springframework.scheduling.annotation.Async;
    import org.springframework.stereotype.Component;
    
    @Slf4j
    @Component
    public class AsyncExecutor {
    
        @Async("taskExecutor")
        public void execute(Runnable runnable){
            try {
                Thread thread = Thread.currentThread();
                log.info("当前异步线程:{}", thread.getName());
                runnable.run();
            } catch (Exception e) {
                log.error("AsyncExecutor execute执行异常",e);
            }
        }
    }

    调用异步方法:

        @RequestMapping("/async")
        public Future<String> async() {
            System.out.println("start");
            asyncExecutor.execute(() -> {
                System.out.println("async start");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("async end");
            });
            System.out.println("end");
            return new AsyncResult<>("我是返回值");
        }

    执行结果:

  • 相关阅读:
    JS 图像上传前实现压缩
    php解决微信开发中用户昵称中的特殊字符与emoji表情写入mysql错误的问题
    PHP 删除非法UTF-8字符
    解决airserver在Windows下安装失败的问题
    微信开发自定义菜单数组结构
    在MAC下配置MySQL 5.7 数据库的编码问题
    MAC 系统升级10.10以后PHP验证码错误的解决办法。[ 一行代码轻松解决! ]
    解决ubuntu server mysql load data infile 导入本地文件ERROR 1148 (42000)错误。
    MySQL 批量导入 csv 文件
    bobo【转】使用jQuery解析JSON数据
  • 原文地址:https://www.cnblogs.com/tangshengwei/p/15001793.html
Copyright © 2011-2022 走看看