Oleg,

I looked at buffered entity, it seems to create a duplicate copy in memory of 
the entire multipart data.
I think this overhead will impact performance adversely in our application.
I understand Multipart entity is inherently blocking.  Is there any other 
option to use with nio to get the same behaviour?

Thanks,

Rishi.


From: Oleg Kalnichevski <[email protected]>
To: HttpClient User Discussion <[email protected]>
Sent: Saturday, June 09, 2012 at 7:07 am
Subject: Re: HttpAsyncClient Multipart Entity not working?

On Fri, 2012-06-08 at 10:43 -0400, [email protected] wrote:

> Hi,

>

> I would like some help, clarification on using HttpAsyncClient with multipart 
> entitity .

> I got it working with HttpCleint but I need it to work with HttpAsyncClient.

> Blocked on this but any help on this would be great.

>

> Sample of how I am using it with both Clients.

>

> MultipartEntity mpe = new MultipartEntity()

> ;

> mpe.addPart("key1", new StringBody("value1"))

> ;

> mpe.addPart("key2", new StringBody("value2"))

> ;

> mpe.addPart("data1", new ByteArrayBody(hugeData1, ""))

> ;

> mpe.addPart("data2", new ByteArrayBody(hugeData2, ""))

>

> ;Using Blocking I/O: This one works.

>

> SchemeRegistry schemeRegistry = new SchemeRegistry()

> ;schemeRegistry.register(new Scheme("http", 80, new PlainSocketFactory()))

> ;ClientConnectionManager cm = new 
> PoolingClientConnectionManager(schemeRegistry)

> ;HttpClient httpClient = new DefaultHttpClient(cm)

> ;HttpPost post = new HttpPost(myurl)

> ;post.setEntity(mpe)

> ;HttpResponse r =  client.execute(post)

> ;int status = r.getStatusLine().getStatusCode()

>

>

> ;Using NIO: This fails with the following exception. (Multipart form entity 
> does not implement #getContent())

>

> HttpParams params = new BasicHttpParams()

> ;AsyncSchemeRegistry schemeRegistry = new AsyncSchemeRegistry()

> ;schemeRegistry.register(new AsyncScheme("http", 80, null))

> ;ClientAsyncConnectionManager cm = new 
> PoolingClientAsyncConnectionManager(new DefaultConnectingIOReactor(), 
> schemeRegistry)

> ;DefaultHttpAsyncClient asyncClient = new DefaultHttpAsyncClient(cm)

> ;asyncClient.setParams(params)

> ;asyncClient.start();

> HttpPost post = new HttpPost(myurl)

> ;

> post.setEntity(mpe)

> ;Future<HttpResponse> f= asyncClient.execute(post, null)

> ;// do something

> HttpResponse r = f.get(5, TimeUnit.SECONDS)

> ;int status = r.getStatusLine().getStatusCode();

>

> Thanks,

>

> Rishi.

>



Rishi



MultipartEntity is inherently blocking. It can only generate its content

by writing it out to an OutputStream through #writeTo() method. It does

not support #getContent() method, which makes it impossible to use with

a non-blocking I/O model without buffering.



You can wrap the MultipartEntity instance with BufferedHttpEntity in

order to make it compatible with the HttpAsyncClient's non-blocking I/O

model.



Oleg





---------------------------------------------------------------------

To unsubscribe, e-mail: [email protected]

For additional commands, e-mail: [email protected]



Reply via email to