You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Frazer Irving <fr...@bbc.co.uk> on 2011/02/16 12:05:30 UTC

Non-blocking file upload

Hi,

I have a requirement to upload a file, and to have the transfer of data 
be interrupted in the event of the server responding with an error. My 
code looks like:

             MimetypesFileTypeMap mime = new MimetypesFileTypeMap();
             request = new PostMethod(url);

             MonitoredRequestEntity ent = new MonitoredRequestEntity(
                     stream, file.length(), mime.getContentType(file), 
listener);
             request.setRequestEntity(ent);
             int status = 0;
             try {
                 status = httpClient.executeMethod(request);
             } catch (Exception e) {
                 if (!request.isAborted()) {
                     logger.error("http post failed for job {}", jobId, e);
                     listener.setStatus(new UploadStatus().setFailed());
                 }
                 return;
             }
             if (status != HttpStatus.SC_ACCEPTED && status != 
HttpStatus.SC_OK) {
                 throw new 
MuseException(request.getStatusLine().toString());
             }

It seems that the call to executeMethod is blocking until the entire 
body of the RequestEntity has been written to the server before 
processing the response. Is there a way to have the response processed 
earlier? The files in question are very large and I would like to avoid 
the transfer if possible.

I am using MultiThreadedHttpConnectionManager to manage connections if 
that affects things.

f

http://www.bbc.co.uk/
This e-mail (and any attachments) is confidential and may contain personal views which are not the views of the BBC unless specifically stated.
If you have received it in error, please delete it from your system.
Do not use, copy or disclose the information in any way nor act in reliance on it and notify the sender immediately.
Please note that the BBC monitors e-mails sent or received.
Further communication will signify your consent to this.
					

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: Non-blocking file upload

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Wed, 2011-02-16 at 11:05 +0000, Frazer Irving wrote:
> Hi,
> 

...

> It seems that the call to executeMethod is blocking until the entire 
> body of the RequestEntity has been written to the server before 
> processing the response.

Yes, this is indeed the case due to the limitations of the blocking I/O
model in Java (a thread execution while blocked in an I/O operation is
unable to react to other events).

>  Is there a way to have the response processed 
> earlier? The files in question are very large and I would like to avoid 
> the transfer if possible.
> 

You can try out the asynchronous version of HttpClient, based on NIO,
which does not have such limitation.

http://hc.apache.org/httpcomponents-asyncclient-dev/index.html

Please note, though, HttpAsyncClient is still an early alpha and is
likely to have defects.

Your application will need to implement a custom ProducingNHttpEntity or
HttpAsyncRequestProducer to able to upload data in a non-blocking
manner.

Hope this helps

Oleg 


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org