1. Scene
HTTP requests are processed asynchronously;
1) No need to return, such as sending SMS, push messages, e-mail and other ancillary services, asynchronous processing, reduce the total time consumption of HTTP requests, improve customer experience.
2) If you need to return, the front-end will wait for the business processing result data, and the asynchronous processing will return to the main thread of HTTP request, and return to the front-end; if it is a single business that can be optimized here, please check the previous and next articles in this blog; if it is multiple business processing, you can use multiple asynchronous processing with return, and the total weight will summarize the result and return.
2. Knowledge points
1) Thread pool threadpooltaskexecutor
2) Note: @ enableasync, @ async (“asyncserviceexecutor”)
3) Return value encapsulates future & lt; T & gt;, new asyncresult & lt; & gt; (T)
3. Code examples
1)controller
package com.liuxd.controller;
import com.liuxd.entity.Responses;
import com.liuxd.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.Future;
@Slf4j
@RestController
public class AsyncCtr {
@Autowired
private TaskService taskService;
@RequestMapping(value = "/get", method = RequestMethod.GET)
public Responses<String> getResult() throws Exception {
log.info("HTTP request received...") ;
long startTime = System.currentTimeMillis();
//1. Asynchronous execution_processing_data_without_waiting
taskService.handleData();
//2. Asynchronous execution_processing_data_waiting_for_processing_results
Future<String> future = taskService.getData();
String result = future.get();
log.info("Receiving HTTP request thread task completed, exit!") ;
long endTime = System.currentTimeMillis();
log.info("Total time spent on http request. " + (endTime - startTime) + "ms");
return new Responses<>(0, result, "SUCCESS");
}
}
2)service
package com.liuxd.service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import java.util.concurrent.Future;
@Slf4j
@Service
public class TaskService {
@Async("asyncServiceExecutor")
public void handleData() {
log.info("Call service no return asynchronous method, start execution...");
long startTime = System.currentTimeMillis();
try {
Thread.sleep(2500L);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("Call service no return asynchronous method, the end of execution!!!") ;
long endTime = System.currentTimeMillis();
log.info("Total time taken to call service without return asynchronous method. " + (endTime - startTime) + "ms");
}
@Async("asyncServiceExecutor")
public Future<String> getData(){
log.info("Call service has return asynchronous method, start execution...");
long startTime = System.currentTimeMillis();
try {
Thread.sleep(2500L);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("Call service has returned asynchronous methods, the end of execution!!!") ;
long endTime = System.currentTimeMillis();
log.info("Total time to call service with return asynchronous method: " + (endTime - startTime) + "ms");
return new AsyncResult<>("Asynchronous processing completed!");
}
}
3) Thread pool
package com.liuxd.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
/**
* @program: unif-insure-service
* @description: Thread Pool
**/
@Configuration
@EnableAsync
public class ExecutorConfig {
@Bean
public Executor asyncServiceExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//Configure the number of core threads
executor.setCorePoolSize(5);
// Configure the maximum number of threads
executor.setMaxPoolSize(5);
//configure queue size
executor.setQueueCapacity(1000);
//configure the name prefix of the threads in the thread pool
executor.setThreadNamePrefix("async-system-");
// rejection-policy: How to handle new tasks when the pool has reached its max size
// CALLER_RUNS: instead of executing the task in the new thread, the caller's thread will execute it
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//Execution initialization
executor.initialize();
return executor;
}
}
4)Responses
package com.liuxd.entity;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Responses<T> {
private Integer code;
private String msg;
private T data;
}
5) Print results
6) Result analysis
1) There are three threads, HTTP request main thread, asynchronous no return thread, asynchronous return thread
2) The main thread does not need to wait for no return thread, it will wait for the result of return thread, and finally return
Read More:
- Asynchronous processing of HTTP request by Java_ Method 2: through deferredresult
- After asynchronous file import and springboot multipartfile upload, the @async asynchronous processing reports an error: nosuchfileexception
- Asynchronous processing of HTTP request by Java_ Method 1: through callable
- Java uses single thread pool to realize multi thread sequential execution (non alternating, non synchronous)
- [Solved] Spring integrates canal to call feign error: pool-1-thread-1
- [Solved] Jenkins error: Asynchronous execution failure
- [Solved] Error processing condition on org.springframework.boot.autoconfigure.context.PropertyPlaceholderA
- Error processing condition on org.springframework.boot.autoconfigure.context.PropertyPlaceholderAuto
- Asynchronous callback case of Java callback function
- OpenFeignClient Use Object to Receive text/plain Type Return Error
- How to Solve RAR Files Extract Error with sevenzipjbinding (Multithread Processing)
- The principle and return value of get() function in C language
- [Solved] ‘build.plugins.plugin.version‘ for org.springframework.boot:spring-boot-maven-plugin is missing.
- Request processing failed; nested exception is java.lang.NullPointerException or UnsatisfiedDependencyE
- [Solved] IDEA Import springboot Project Error: Cannot resolve plugin org.springframework.boot:spring-boot-maven-plugin:<unknown>
- OTA Pack Compile Error: ExternalError: Failed to run signapk.jar: return code 1:Error: A JNI error has occurred
- [Solved] Upload Files Error: Request processing failed;nested exception is org.springframework.web.multipart.MultipartExcepti
- How to Solve JasperFillManager.fillReport Stuck issue (No Error and No Processing)
- [Solved] Redis Error: Unexpected exception while processing command