Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynchronous HTTP Client for Java

As a relative newbie in the Java world, I am finding many things frustratingly obtuse to accomplish that are relatively trivial in many other frameworks. A primary example is a simple solution for asynchronous http requests. Seeing as one doesn't seem to already exist, what is the best approach? Creating my own threads using a blocking type lib like httpclient or the built-in java http stuff, or should I use the newer non-blocking io java stuff - it seems particularly complex for something which should be simple.

What I am looking for is something easy to use from a developer point of view - something similar to URLLoader in AS3 - where you simply create a URLRequest - attach a bunch of event handlers to handle the completion, errors, progress, etc, and call a method to fire it off.

If you are not familiar with URLLoader in AS3, its so super easy and looks something like this:

private void getURL(String url)
{
    URLLoader loader = new URLLoader();
    loader.addEventListener(Event.Complete, completeHandler);
    loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
    loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);

    URLRequest request = new URLRequest(url);

    // fire it off - this is asynchronous so we handle
    // completion with event handlers
    loader.load(request);
}

private void completeHandler(Event event)
{
    URLLoader loader = (URLLoader)event.target;
    Object results = loader.data;

    // process results
}

private void httpStatusHandler(Event event)
{
    // check status code
}

private void ioErrorHandler(Event event)
{
    // handle errors
}
like image 783
helifreak Avatar asked Jun 18 '09 18:06

helifreak


4 Answers

You have several choices for Async HTTP Clients in Java

  1. Java 8: Use the async-http-client formerly called ning http client library.
  2. Java 11 and above: JDK now comes with the java.net.http. HttpClient which is fully asynchronous.
  3. Square's OkHttpClient. Supports both sync blocking and async calls with callbacks. Quite popular on Android.
like image 83
lenkite Avatar answered Nov 02 '22 22:11

lenkite


Version 4.0 of Apache Commons HttpClient (now in HttpComponents/HttpCore) also support Java's NIO (non-blocking IO). I think this is your best bet.

like image 21
Robert Campbell Avatar answered Nov 03 '22 00:11

Robert Campbell


If you haven't looked at it already, check out the Java 5 java.util.concurrent -- it makes multi-threaded apps much easier to develop. You can set up a ThreadPoolExecutor that manages, say, four Threads. You then feed the pool any number of tasks to complete. Each task is a Runnable. The ThreadPoolExecutor will queue up the Runnable tasks and feed them to available Threads in parallel. The Pool's afterExecute() method is called when each Runnable task completes.

I vividly remember writing a fetch thread pool for a web browser written in Java back in 1999, and it was a bear to get right. Last month I wrote a load tester for a web server. The tester has a ThreadPoolExecutor that has n threads, and the Runnable tasks I feed it each fetch a page using Apache HTTP Client. It took just an hour or two to get it working reasonably well. I think you'll like java.util.concurrent coupled with Apache HTTP Client, though it sounds like you'll need to do some customization for progress indication.

(Note that Apache HTTP Client does its own thread pooling, and the default configuration limits you to 20 threads max, and only two to each web server.)

Update: Here's the link to Apache HTTP Client. Be sure to read up on MultiThreadedHttpConnectionManager, it's what handles the connection pool, and it's not shown in the most basic example.

like image 6
Jim Ferrans Avatar answered Nov 02 '22 23:11

Jim Ferrans


The Jetty HTTP client is asynchronous.

like image 6
Avi Flax Avatar answered Nov 03 '22 00:11

Avi Flax