首页 > 网站 > 帮助中心 > 正文

Springboot实现高吞吐量异步处理详解(适用于高并发场景)

2024-07-09 22:41:33
字体:
来源:转载
供稿:网友

技术要点

org.springframework.web.context.request.async.DeferredResult<T>

示例如下:

1.   新建Maven项目  async

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</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.   AsyncStarter.java

package com.java;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class AsyncStarter {  public static void main(String[] args) {    SpringApplication.run(AsyncStarter.class, args);  }}

4.   AsyncVo.java

package com.java.vo;import org.springframework.web.context.request.async.DeferredResult;/** * 存储异步处理信息 *  * @author Logen * * @param <I> 接口输入参数 * @param <O> 接口返回参数 */public class AsyncVo<I, O> {  /**   * 请求参数   */  private I params;  /**   * 响应结果   */  private DeferredResult<O> result;  public I getParams() {    return params;  }  public void setParams(I params) {    this.params = params;  }  public DeferredResult<O> getResult() {    return result;  }  public void setResult(DeferredResult<O> result) {    this.result = result;  }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表