Spring boot 配置異步處理執行器

 

示例以下:java

一、 新建Maven 項目 async-executorweb

 

二、pom.xmlspring

<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>

 

三、AsyncExecutorStarter.javaapache

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);
    }

}

 

四、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;
    }

}

 

五、AsyncHandler.javaapp

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());

    }

}

 

六、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 "您的任務已提交";
    }

}

 

七、 運行 AsyncExecutorStarter.java 啓動服務async

瀏覽器輸入 http://localhost:8080/asyncmaven

快速收到服務端響應結果<您的任務已提交>ide

觀察服務端控制檯,異步處理程序仍在執行,最終打印內容以下:

[ 收到請求 ]
[ 返回響應 ]
1[ 異步處理任務開始執行 ]1557148926609
2[ 異步處理任務開始執行 ]1557148926610
1[ 異步處理任務完成執行 ]1557148931609
2[ 異步處理任務完成執行 ]1557148931610

 

結論:

服務端收到請求後當即返回,而後兩個異步任務幾乎同時提交,各自開始後臺執行,互不影響。

 

 

 

 

Spring boot 配置異步處理執行器

.

相關文章
相關標籤/搜索