Spring boot 配置异步处理执行器

 

示例如下:

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

 

结论:

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

 

 

 

 

Spring boot 配置异步处理执行器

.

posted @ 2019-05-06 21:33  诚信天下  阅读(888)  评论(0编辑  收藏  举报