3

I'm having trouble with some code I'm testing using AsyncHTTPClient. Here is the general config

    this.config = (new AsyncHttpClientConfig.Builder())
            .setAllowPoolingConnection(true)
            .setAllowSslConnectionPool(true)
            .addRequestFilter(new ThrottleRequestFilter(10))
            .setMaximumConnectionsPerHost(20)
            //.setMaximumConnectionsTotal(20)
            .setRequestTimeoutInMs(100000)
            .build();
    this.client = new AsyncHttpClient(new NettyAsyncHttpProvider(config));

(Note that max connections is commented out because of some weird bug when using ThrottleRequestFilter, see here https://groups.google.com/forum/?fromgroups=#!topic/asynchttpclient/nEnQnPtCP2g)

Now here is some test code,

FileInputStream fstream = new FileInputStream("import.txt");
DataInputStream dstream = new DataInputStream(fstream);
BufferedReader reader = new BufferedReader(new InputStreamReader(dstream));
while ((line = reader.readLine()) != null) {
//Some code to build and create a request using the build function and then perform a GET operation. Request is built using Java's Future object from java.concurrent
}

When the import.txt file is less than 100 lines of simple text such as this

 196    242 3   881250949

Everything works fine, all the requests go through and when you check the responses all is fine. Anything over 100 and I start getting timeouts and in cases of something like 1000+, I actually start running into permGen memory errors.

I thought the ThrottleRequestFilter is supposed to limit the max threads to 10 and process only 10 at a time. Why does it bomb out when the text file has more than 100+ lines?

I've also tried switching to using the Grizzly implementation but that also bombs out the same way. I'm beginning to suspect it's either the way I wrote the test code or Async HTTP Client actually has some issues when creating a high number of requests. If so, are there any other good async http clients for java?

1 Answers1

1

Your problem is that you didn't set any buffersize before starting reading the file. look at this example

private static final int EXT_DEFAULT_BUFFER_SIZE = 1024 * 8;
InputStream   inputStream=this.getClass().getClassLoader().getResourceAsStream("Resource_Name");
public static String copyLargeExt(InputStream input) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[EXT_DEFAULT_BUFFER_SIZE];
        int n = 0;
        while(-1 != (n = input.read(buffer))) {
            baos.write(buffer, 0, n);
        }
        return baos.toString();
    }
Festus Tamakloe
  • 11,231
  • 9
  • 53
  • 65
  • I thought buffer size is initalized to some default value? Or is this not the case? –  Nov 07 '12 at 23:38