• (转)Spring boot 配置异步处理执行器


    https://www.cnblogs.com/jonban/p/10822251.html

    示例如下:

    1、 新建Maven 项目 async-executor

    2、pom.xml

    复制代码
    <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.java</groupId>
        <artifactId>async-executor</artifactId>
        <version>1.0.0</version>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.5.RELEASE</version>
        </parent>
    
    
        <dependencies>
    
            <!-- Spring Boot -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
    
            <!-- 热部署 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>springloaded</artifactId>
                <version>1.2.8.RELEASE</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>provided</scope>
            </dependency>
    
        </dependencies>
    
        <build>
            <finalName>${project.artifactId}</finalName>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
    
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </project>
    复制代码

    3、AsyncExecutorStarter.java

    复制代码
    package com.java;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    /**
     * 主启动类
     * 
     * @author Logan
     * @version 1.0.0
     * @createDate 2019-05-06
     *
     */
    @SpringBootApplication
    public class AsyncExecutorStarter {
    
        public static void main(String[] args) {
            SpringApplication.run(AsyncExecutorStarter.class, args);
        }
    
    }
    复制代码

    4、AsyncExecutorConfig.java

    复制代码
    package com.java.config;
    
    import java.util.concurrent.Executor;
    
    import org.springframework.context.annotation.Configuration;
    import org.springframework.scheduling.annotation.AsyncConfigurer;
    import org.springframework.scheduling.annotation.EnableAsync;
    import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
    
    /**
     * 异步任务执行器配置
     * 
     * @author Logan
     * @version 1.0.0
     * @createDate 2019-05-06
     *
     */
    @EnableAsync
    @Configuration
    public class AsyncExecutorConfig implements AsyncConfigurer {
    
        @Override
        public Executor getAsyncExecutor() {
            ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
            executor.setCorePoolSize(5);
            executor.setMaxPoolSize(10);
            executor.setQueueCapacity(30);
            executor.initialize();
            return executor;
        }
    
    }
    复制代码

    5、AsyncHandler.java

    复制代码
    package com.java.handler;
    
    import org.springframework.scheduling.annotation.Async;
    import org.springframework.stereotype.Component;
    
    /**
     * 异步处理器
     * 
     * @author Logan
     * @version 1.0.0
     * @createDate 2019-05-06
     *
     */
    @Component
    public class AsyncHandler {
    
        @Async
        public void handle(int i) {
            System.out.println(i + "[ 异步处理任务开始执行 ]" + System.currentTimeMillis());
    
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
            System.out.println(i + "[ 异步处理任务完成执行 ]" + System.currentTimeMillis());
    
        }
    
    }
    复制代码

    6、AsyncController.java

    复制代码
    package com.java.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.java.handler.AsyncHandler;
    
    /**
     * 控制层收到请求,任务交给异步处理器后台处理,接口直接返回响应
     * 
     * @author Logan
     * @version 1.0.0
     * @createDate 2019-05-06
     *
     */
    @RestController
    public class AsyncController {
    
        @Autowired
        private AsyncHandler handler;
    
        @GetMapping("/async")
        public String async() {
            System.out.println("[ 收到请求 ]");
    
            handler.handle(1);
            handler.handle(2);
    
            System.out.println("[ 返回响应 ]");
            return "您的任务已提交";
        }
    
    }
    复制代码

    7、 运行 AsyncExecutorStarter.java 启动服务

    浏览器输入 http://localhost:8080/async

    快速收到服务端响应结果<您的任务已提交>

    观察服务端控制台,异步处理程序仍在执行,最终打印内容如下:

    复制代码
    [ 收到请求 ]
    [ 返回响应 ]
    1[ 异步处理任务开始执行 ]1557148926609
    2[ 异步处理任务开始执行 ]1557148926610
    1[ 异步处理任务完成执行 ]1557148931609
    2[ 异步处理任务完成执行 ]1557148931610
    复制代码

    结论:

    服务端收到请求后立即返回,然后两个异步任务几乎同时提交,各自开始后台执行,互不影响。

  • 相关阅读:
    JVM学习02:GC垃圾回收和内存分配
    JVM学习01:内存结构
    webservice学习01:wsdl文档结构
    项目总结21:项目总结21:input实现多图上传(FormData)(上传OSS并保存数据库)
    mysql学习3:mysql之my.cnf详解
    项目总结20:阿里云免费https证书申请
    5月9日——vue渲染过程中{{xxx}}显示
    5月8日——iOS中的3D Touch效果
    5月7日——采用第三方页面内容,但是顶部title使用自己的
    5月5日——更改手机状态栏的背景颜色
  • 原文地址:https://www.cnblogs.com/telwanggs/p/12620455.html
Copyright © 2020-2023  润新知