I would like to be able to cancel the backend processing of data, but I'm not sure how to do that while the code is running.
I have the below Java code to receive a POST request:
@PostMapping(value = "/message", consumes = DEFAULT_CONTENT_TYPE, produces = DEFAULT_CONTENT_TYPE)
APIResponse[] postIsoMessage(
HttpServletRequest request,
HttpServletResponse response,
@RequestHeader(value = HEADER_CORRELATION_ID) String correlationId,
@RequestBody JsonMessage requestMessage)
throws Exception {
//for loop here that loops through the items in the JSON request
.....
The method will receive the POST request, which contains an array of 100 or so items to be processed in a loop. This can take upwards of 30 mins to process all items, is there any way the user can cancel the processing of the request? If something goes wrong, it currently has to wait for every single item in the request to process.
Is it possible to add another post mapping that the user can call to cancel the associated processing, by say the correlationId ?
CodePudding user response:
Actualy it's "solution architect" question, but from my point of view you can create new service class. In this class something like ConcurrentHashMap ot what you like collection. And method like Future doHardWork(String correlationId, JsonMessage requestMessage) {...} . In this method you can add ExecutorService which will call your hard logic and return Future, which will be added to concurrent collection by correlationId and your doHardWork method will return this Future to controller. In your postIsoMessage controller you can wait this Future and return result to user. In this solution you can add new controller like StopHardWorkController which will receive corellation id, then will go to service with concurrent collection wich contains Future. get this Fututre by correlation id and cancel it.
public class HardWorkService {
private final HashMap<String, Future<APIResponse[]>> container = new ConcurrentHashMap<>();
public Future<APIResponse[]> doHardWork(JsonMessage requestMessage, String correlationId) {
ExecutorService executorService = ...;
Future<APIResponse[]> resultFuture = executorService.submit(() -> {your logic..});
container.put(correlationId, resultFuture);
return resultFuture;
}
public boolean stopHardWork(String correlationId) {
Future<APIResponse[]> resultFuture = container.get(correlationId);
return resultFuture.cancel(true);
}
}
...
public class StopHardWorkController{
private final HardWorkService hardWorkService;
@DeleteMapping(...) // Not sure about http method
ResponceEntity stopHardWork(String correlationId) {
return hardWorkService.stopHardWork(correlationId) ? Status.OK : some status;
}
}