Configuring chunk size in Apache HttpClient 4.3.x

The default chunk size in Apache HttpClient 4.3 is set to 2048 bytes. There is no easy way to customize the chunk size in the latest Apache HttpClient release. It looks like the only approach is to implement a custom ClientConnectionFactory that allows to override the default chunk size. Here is an example.

import org.apache.http.Consts;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.ConnectionConfig;
import org.apache.http.conn.HttpConnectionFactory;
import org.apache.http.conn.ManagedHttpClientConnection;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.entity.ContentLengthStrategy;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.DefaultManagedHttpClientConnection;
import org.apache.http.impl.conn.ManagedHttpClientConnectionFactory;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.impl.io.ChunkedOutputStream;
import org.apache.http.io.SessionOutputBuffer;
import java.io.OutputStream;
import java.util.concurrent.atomic.AtomicLong;
/**
* Created by arul on 12/19/13.
*/
public class ChunkFactory {
public static class CustomManagedHttpClientConnection extends DefaultManagedHttpClientConnection {
private final int chunkSize;
public CustomManagedHttpClientConnection(final String id, final int buffersize, final int chunkSize) {
super(id, buffersize);
this.chunkSize = chunkSize;
}
@Override
protected OutputStream createOutputStream(long len, SessionOutputBuffer outbuffer) {
if (len == ContentLengthStrategy.CHUNKED) {
return new ChunkedOutputStream(chunkSize, outbuffer);
}
return super.createOutputStream(len, outbuffer);
}
}
public static class CustomManagedHttpClientConnectionFactory extends ManagedHttpClientConnectionFactory {
private static final AtomicLong COUNTER = new AtomicLong();
private final int chunkSize;
public CustomManagedHttpClientConnectionFactory(int chunkSize) {
this.chunkSize = chunkSize;
}
@Override
public ManagedHttpClientConnection create(HttpRoute route, ConnectionConfig config) {
final String id = "http-outgoing-" + Long.toString(COUNTER.getAndIncrement());
return new CustomManagedHttpClientConnection(id, config.getBufferSize(), chunkSize);
}
}
public static void main(String[] args) throws Exception {
int chunkSize = 1024;
HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory = new CustomManagedHttpClientConnectionFactory(chunkSize);
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(connFactory);
CloseableHttpClient client = HttpClients.createMinimal(connManager);
HttpPost httpPost = new HttpPost("http://…/resource&quot;);
StringEntity entity = new StringEntity("a very long string …..", ContentType.create("plain/text", Consts.UTF_8));
entity.setChunked(true);
httpPost.setEntity(entity);
CloseableHttpResponse response = client.execute(httpPost);
}
}

view raw
ChunkFactory
hosted with ❤ by GitHub

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s