luongbs94 / google-api-java-client

Automatically exported from code.google.com/p/google-api-java-client
0 stars 0 forks source link

cancel a pending server request #870

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
External references, such as a standards document, or specification?
Similar API: 
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Synchronous_and_
Asynchronous_Requests

Java environments (e.g. Java 6, Android 2.3, App Engine, or All)?
Java & Android for my case, but it would be appropriate for All.

Please describe the feature requested.
I would like to be able to cancel a request generated by 
com.google.api.client.googleapis.services.AbstractGoogleClient.
This probably would require an executeAsync method parallel to 
AbstractGoogleClientRequest.execute. An AbstractGoogleClient.cancelAll method 
would work for my case (ie, cancel all pending requests generated by that 
client), but that's not as clean an API.

Original issue reported on code.google.com by ptuc...@google.com on 11 Apr 2014 at 6:13

GoogleCodeExporter commented 9 years ago
Canceling an HTTP request that has been sent on the wire is not a good idea. 
The only way to do this is by closing the connection, but the server may never 
know that the client has closed so it may continue doing what it's doing. This 
wastes resources and introduces network latencies.

Adding such an interface requires changes at both ends, and is difficult to 
implement based on HTTP. If you really want to cancel an ongoing request, you 
could call HttpRequest.executeAsync(), get a Future, and abandon it once it 
times out. But again, this may cause resource waste at the server side.

Original comment by wonder...@google.com on 30 Dec 2014 at 4:40

GoogleCodeExporter commented 9 years ago
Right, that's why I recommended "an executeAsync method parallel to 
AbstractGoogleClientRequest.execute".

Original comment by ptuc...@google.com on 30 Dec 2014 at 6:08

GoogleCodeExporter commented 9 years ago
There's an executeAsync() method implemented by the underlining HttpRequest 
[1], you can get access to it by calling 
AbstractGoogleClientRequest.buildHttpRequest() [2].

Although named as executeAsync(), it's not a true async interface. But it 
should be sufficient for your case.

[1] 
https://code.google.com/p/google-http-java-client/source/browse/google-http-clie
nt/src/main/java/com/google/api/client/http/HttpRequest.java#1071
[2] 
https://code.google.com/p/google-api-java-client/source/browse/google-api-client
/src/main/java/com/google/api/client/googleapis/services/AbstractGoogleClientReq
uest.java#276

Original comment by wonder...@google.com on 30 Dec 2014 at 6:49