In case of Async HTTP, there are 3 different threads involved.
- Request thread – set MDC here in the request filter.
- Async thread – copy thread context from request thread to this async thread.
- Response thread – How do we copy the thread context from the async thread to this thread?
@RestController
public class ServiceChangeStatusPollingApi {
private final StatusChangePolling statusChangePolling;
private final Executor taskExecutor;
@Autowired
public ServiceChangeStatusPollingApi(StatusChangePolling statusChangePolling,
@Qualifier(TaskConfiguration.ASYNC_EXECUTOR_NAME)
Executor taskExecutor) {
this.statusChangePolling = statusChangePolling;
this.taskExecutor = taskExecutor;
}
@GetMapping(value = "/status/polling",
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public DeferredResult<ServiceDeploymentState> getLatestServiceDeploymentStatus(
@Parameter(name = "id", description = "ID of the service")
@RequestParam(name = "id") UUID serviceId,
@Parameter(name = "previousServiceDeploymentState", description = "Previous known service status to client")
@RequestParam(name = "previousServiceDeploymentState", required = false)
ServiceDeploymentState previousServiceDeploymentState
) {
DeferredResult<ServiceDeploymentState> stateDeferredResult = new DeferredResult<>();
taskExecutor.execute(() ->
this.statusChangePolling.waitForUsersWithStatus(
stateDeferredResult,
serviceId,
previousServiceDeploymentState));
return stateDeferredResult;
}
}
Implemented DeferredResultProcessingInterceptor
class but all methods in this class are executed either before the response thread is started or after it is completely executed.
New contributor
Swaroop Raghupathy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.