1.DeferredResult
Spring supports HTTP asynchronous return
2. Asynchronous processing
Start a new thread, process the data and return the value.
3. Code examples
1)controller
package com.liuxd.controller;
import com.liuxd.entity.Responses;
import com.liuxd.service.TaskService2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;
@Slf4j
@RestController
public class AsyncCtr2 {
@Autowired
private TaskService2 taskService2;
@GetMapping(value = "/getResult")
public DeferredResult<Responses<String>> getResult() {
log.info("HTTP request received...");
long startTime = System.currentTimeMillis();
DeferredResult<Responses<String>> deferredResult = new DeferredResult<Responses<String>>();
new Thread(new Runnable() {
@Override
public void run() {
taskService2.getData(deferredResult);
}
}).start();
log.info("The task of the receiving HTTP request thread has been completed, exit!");
long endTime = System.currentTimeMillis();
log.info("http Total request time. " + (endTime - startTime) + "ms");
return deferredResult;
}
}
2)service
package com.liuxd.service;
import com.liuxd.entity.Responses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.async.DeferredResult;
@Slf4j
@Service
public class TaskService2 {
public void getData(DeferredResult<Responses<String>> deferredResult) {
log.info("Call service asynchronous with return method, start execution...");
long startTime = System.currentTimeMillis();
try {
Thread.sleep(2500L);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("Call service asynchronous has return method, the end of execution!!!") ;
long endTime = System.currentTimeMillis();
log.info("Total time taken to call service asynchronous return method. " + (endTime - startTime) + "ms");
deferredResult.setResult(new Responses<>(0, "Done", "SUCCESS"));
}
}
3)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;
}
4) Print results
5) Result analysis
1) The HTTP main thread accepts the request and ends after processing the request
2) After the asynchronous thread ends, returns the
Read More:
- Asynchronous processing of HTTP request by Java_ Method 1: through callable
- Spring boot uses thread pool to realize asynchronous processing without return and asynchronous processing with return
- After asynchronous file import and springboot multipartfile upload, the @async asynchronous processing reports an error: nosuchfileexception
- Request processing failed; nested exception is java.lang.NullPointerException or UnsatisfiedDependencyE
- Asynchronous callback case of Java callback function
- Java: How to Find the Minimum Value from a Random Array by Bubble Sort Method
- [Solved] “status“:405,“error“ Request method ‘POST‘ not supported“
- Abstract method and static method of java interface
- SpringBoot :Error parsing HTTP request header [How to Solve]
- [Solved] Upload Files Error: Request processing failed;nested exception is org.springframework.web.multipart.MultipartExcepti
- [Solved] Method threw ‘java.lang.StackOverflowError‘ exception. Cannot evaluate
- [Solved] Java.lang.IllegalStateException: getReader() has already been called for this request
- JAVA: How to use the third-party font library method (using the third-party TTF / TTC font library method)
- [Solved] IDEA java Cannot resolve method ‘getName‘ in ***
- SpringCloud Use openFeign Multipartfile to Upload Files Error: Current request is not a multipart request
- [Solved] Jenkins error: Asynchronous execution failure
- [Solved] JAVA HttpClient Send Https request certificate error:PKIX path building failed:
- [Solved] MultipartException: Failed to parse multipart servlet request; nested exception is java.lang.Runtime
- [Solved] Invocation of init method failed; nested exception is java.lang.NoSuchMethodError:
- [Solved] ClientAbortException: java.io.IOException: Connection reset by peer