Spring boot 配置异步处理执行器

Stella981
• 阅读 646

示例如下:

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 配置异步处理执行器

.

点赞
收藏
评论区
推荐文章
执键写春秋 执键写春秋
3年前
基于Maven工程下的MyBatis框架+MySQL+连接池的数据查询操作
具体操作项目结构引入项目依赖pom.xml<?xmlversion"1.0"encoding"UTF8"?<projectxmlns"http://maven.apache.org/POM/4.0.0"xmlns:xsi"http://www.w3.org/2001/XMLSchemainstance"xsi
Easter79 Easter79
3年前
springboot2.1整合mybatis
1:添加依赖<?xmlversion"1.0"encoding"UTF8"?<projectxmlns"http://maven.apache.org/POM/4.0.0"xmlns:xsi"http://www.w3.org/2001/XMLSchemainstance"xsi
Stella981 Stella981
3年前
SpringBoot2.0配置durid数据源
pom依赖<?xmlversion"1.0"encoding"UTF8"?<projectxmlns"http://maven.apache.org/POM/4.0.0"xmlns:xsi"http://www.w3.org/2001/XMLSchemainstance"
Wesley13 Wesley13
3年前
JAVA spring 常用包作用详解(转)
转载地址:https://www.cnblogs.com/TmcBlog/p/6093162.html<projectxmlns"http://maven.apache.org/POM/4.0.0"xmlns:xsi"http://www.w3.org/2001/XMLSchemainstance"xsi:s
Stella981 Stella981
3年前
121 项目 034 笔记向 easyui
pom<?xmlversion"1.0"encoding"UTF8"?<projectxmlns"http://maven.apache.org/POM/4.0.0"xmlns:xsi"http://www.w3.org/2001/XMLSchemainstance"
可莉 可莉
3年前
121 项目 034 笔记向 easyui
pom<?xmlversion"1.0"encoding"UTF8"?<projectxmlns"http://maven.apache.org/POM/4.0.0"xmlns:xsi"http://www.w3.org/2001/XMLSchemainstance"
Stella981 Stella981
3年前
Mybatis自动生成实体类
Maven自动生成实体类需要的jar包一、pom.xml中1<projectxmlns"http://maven.apache.org/POM/4.0.0"xmlns:xsi"http://www.w3.org/2001/XMLSchemainstance"xsi:schemaLocation"http://maven.a
Stella981 Stella981
3年前
Spring Boot 集成log4j2日志
一、pom文件配置<projectxmlns"http://maven.apache.org/POM/4.0.0"xmlns:xsi"http://www.w3.org/2001/XMLSchemainstance"xsi:schemaLocation"http://maven.apache.org/POM/4.0.0h
Stella981 Stella981
3年前
Maven系列
<projectxmlns"http://maven.apache.org/POM/4.0.0"xmlns:xsi"http://www.w3.org/2001/XMLSchemainstance"xsi:schemaLocation"http://maven.apache.org/POM/4.0.0http://ma
Easter79 Easter79
3年前
SpringBoot2.0配置durid数据源
pom依赖<?xmlversion"1.0"encoding"UTF8"?<projectxmlns"http://maven.apache.org/POM/4.0.0"xmlns:xsi"http://www.w3.org/2001/XMLSchemainstance"