views:

4647

answers:

13

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
}
+4  A: 

Looks like you want (a part of) NIO -- there's a good tutorial here, the asynchronous networking part starts at p. 30 and there are many useful links at the end.

Alex Martelli
+1  A: 

So, it is probably worth considering that actionscript and Java don't server the same niche. For example, Java does make some things more tedious - but usually that is to give the user more options in how, for example, an HTTP connection is executed, whereas actionscript might abstract details or possible errors away for ease of use. But, your point still stands.

I myself am not aware of an asynchronous HTTP client for Java. Alex Martelli's answer talks about Java's NIO, which is a good answer if you are interested in implementing the HTTP protocol in your own code. NIO will let you use sockets to connect to the web server - but then you have to manually create your own GET requests and parse the incoming HTTP headers/data.

Another option is to use the java.net.URL classes - and you can find many tutorials for those online and on stackoverflow. You can wrap those in threads - so your java program has multiple threads of execution.

But then you run into the problem of synchronization. Which I agree, is a pain, but then it offers a more granular level of flexibility.

(I realize that this doesn't answer your question - and if anybody actually knows of a java facility to do asynchronous http requests, I'd be interested to know!)

rascher
Totally understand the different niches. However, in this instance, we are using Java as a client UI framework and a lot of the data to be presented is coming from external web services.
helifreak
Actually there are multiple async http clients, listed in other answers (jetty has had one for years, jakarta hc quite some time too; ning's async http client is newest and actively developed). They just haven't gotten enough attention they deserve.
StaxMan
+1  A: 

AFAIK the TCPMon tool takes a similar approach to what you describe. You can take a look at the source code in their SVN browser

Also have a look at WGET-java for the guts of the blocking code.

But do you have to write this in Java? There are a lot of other approaches using JRuby or Rhino to accomplish something like this easily that will run on the JVM but aren't written in Java.

cwash
Well, using other languages that compile to java bytecode is a future option, but we definitely need something Java based as well. Thanks for the suggestions.
helifreak
+1  A: 

httpunit and htmlunit are 2 customizable and configurable Java http clients able to anything a browser such as simulate firefox, headless browsing, scheduled software clients and agents.

LarsOn
I was going to mention this, but neither of these are asynch. They do make pretty quick work of automation tasks, though.
cwash
+1  A: 

I'd recommend firing separate threads for that.

alamar
+3  A: 

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.

Jim Ferrans
Good information. Thanks. I still can't believe how client unfriendly Java is. Maybe Java is just starting to show its age.
helifreak
I did almost no development in Java during the 1.4 and Java 5 years, and now I find Java 6 significantly better than 1.3. In addition to java.util.concurrent, generics and the Collections framework stand out. Technology layered on Java can seem unfriendly though: compare JAXP with Groovy's concise XML processing. Also. dynamic languages like Ruby, Python, and Scala are serious competition these days.
Jim Ferrans
No, it's not that Java is showing age; check out multiple non-JDK-bundled non-blocking (asynchronous) http clients.No need to explicitly mess with threads just to do concurrent accesses. While it would be convenient to have non-blocking alternative in JDK there are so many useful things that it is probably better to try to keep JDK from ballooning any bigger.
StaxMan
+1  A: 

Take also a look into http://www.javaworld.com/javaworld/jw-03-2008/jw-03-asynchhttp.html This article discusses async HTTP based on a HttpClient named xLightweb

See http://xlightweb.sourceforge.net/. It seems to be actively developed.
Eelco
A: 

I just stumbled upon the asynchronous HTTP client implemented within Geronimo. You might also want to take a look it, at http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/ - Caveat: the latest commit seems over a year old.

Another project building an asynchronous HTTP client is xsocket: xsocket.sourceforge.net

This http://cwiki.apache.org/AWEB/ seems to be the current location. Not sure how active it is though.
Eelco
+2  A: 

The Jetty HTTP client is asynchronous.

Avi Flax
+3  A: 

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.

rcampbell
+5  A: 

Use the ning http client library. See http://code.ning.com/2010/03/introducing-nings-asynchronous-http-client-library/

lenkite
And it's at github at http://github.com/ning/async-http-client.I can recommend this, having used it, although mostly just as a simple reliable synchronous http client. But with simple Future-based results, async operation is simple to use too.
StaxMan
They've changed GitHub repos, they're now at http://github.com/AsyncHttpClient/async-http-client
jfager
A: 

Asyncweb provides an asynchronous http client along with its http server. Available for download from the following location:

https://svn.apache.org/repos/asf/mina/asyncweb/trunk

Amit
But isn't it a dead project?
StaxMan
A: 

Old question, but this might still come in handy: http://hotpotato.factor45.org/

brunodecarvalho