I use REST API based system, in which there are some requests that take long time to complete. I want to give user an option to cancel the request.
-
1How exactly would "cancelling a request" behave? Should the sent data be revoked? Why don't you just ignore the result? – Dominik Sandjaja Aug 05 '15 at 12:58
-
Okay, but then the cancel would have to be handled on the client that fires the request. You don't give any description about the client at all. – Gimby Aug 05 '15 at 13:02
-
1I don't know why this was downvoted to -2. It's a valid question. – mahemoff Aug 05 '15 at 22:42
-
Does this answer your question? [How to model a CANCEL action in a RESTful way?](https://stackoverflow.com/questions/42533872/how-to-model-a-cancel-action-in-a-restful-way) – Roman Vottner Dec 06 '19 at 13:19
2 Answers
First, support
POST /requests
which will return a reference to the status of the request
{
"id": 1234,
"self"": "/requests/1234"
"status": "Running"
}
Then add support for
PUT /requests/1234
{
"status": "Canceled:"
}
That will let clients cancel a request if it hasn't finished yet. If the request is to create some other kind of resource, then instead of POST /requests
, do POST /myResource
, but still return the status object with the pointer to /requests in the response.
Clients can then poll /requests
to see when the request is complete.

- 13,209
- 3
- 37
- 52
-
Worth noting that a more "purist" and simpler technique would be to support DELETE of the request, instead of changing status to cancelled. But in fact, your solution is better for the real world, where it's useful to keep a record of the cancelled request for auditing and debugging purposes. – mahemoff Aug 05 '15 at 22:46
-
-
1@mahemoff I think it depends on whether or not you can really delete the request, or if you're just changing the request's status. If your requirements let you do a soft delete, then by all means support `DELETE /requests/{id}`, and then return `410 Gone`. I don't think that hard deletes are generally preferable to soft deletes. – Eric Stein Aug 06 '15 at 00:27
Firstly you need to use multiple threads because your program will be on hold while it is sending the request so you cannot click on something until it is back from hold.
Create a thread which calls the rest API in background without hanging the overall application and terminate that thread on click of a button.
note for terminating the thread you need to use stop function which is depreciated now because you cannot interrupt the thread or check a Boolean during the process.
@Deprecated
public final void stop()
Alternatively, you can use Maximum Time for a HTTP Request call by
HttpConnectionParams.setConnectionTimeout(httpParams, 30000);
_For all scenario
Make thread pool method
executorService = Executors.newFixedThreadPool(1);
Put your logical method in callable store in future object
Future<Boolean> futureObjects = executorService.submit(newCallable<Boolean>() { ....call your logical method which you going run in multiple thread....});
3.Gets your results future object
return (futureObjects != null) ? futureObjects.get(timeout, TimeUnit.SECONDS) : null;
The default waits until getting separate calls response.
4.IN between calling multiple threads requesting user want to stop or break their multiple thread calls then simply check executor is not terminated then terminate immediately
if (executorService != null && !executorService.isTerminated(){
executorService.shutdownNow();
}

- 33
- 5

- 519
- 1
- 6
- 18