Compound Theory

v2.0

Categories

  1. Transfer
  2. ColdFusion
  3. JRuby
  4. Java
  5. ColdSpring
  6. Squabble
  7. JavaLoader
  8. ColdDoc
  9. 2ddu
  10. AsyncHTTP
  11. OO Analysis and Design
  12. Flex
  13. Railo
  14. XML / XSL
  15. Hibernate
  16. ColdFusion Builder
  17. Fall
  18. Ubuntu
  19. XHTML / CSS
  20. Eclipse
  21. Git
  22. Oracle Database
  23. Usability / UI Design
  24. webDU
  25. cf.Objective()
  26. LWJGL
  27. cf.Objective(ANZ)
  28. Captcha
  29. MAX
  30. Melbourne CFUG
  31. Martial Arts
  32. Random Things
  33. Conduit

Recent Posts

Projects

Recent Comments

19 July 2006 10:33 AM

AsyncHTTP - Asynchronous HTTP Requests

I don't know how many times I've gone looking to do asynchronous processing in ColdFusion, and come up against the myriad of hacks, workarounds and that general feeling that I want to poke my eyes out with a rusty spoon.

Now that JavaLoader exists, integrating Java with ColdFusion becomes so ridiculously easy, it would have been relative heresy if I hadn't sat down for an afternoon and wrote a library for making asynchronous HTTP GET and POST requests.

To use the asyncHTTP library , you simply have to drop /asyncHTTP/ the folder in your web root, or create a mapping to it.

From here we create an instance of the AsyncHTTP.cfc:

asyncHTTP = createObject("component", "asyncHTTP.AsyncHTTP").init();

Now that we have an instance of the AsynHTTP CFC, to do an asynchronous GET request, we simply call:

asyncHTTP.get("http://www.site.com/mytemplate.cfm?var=value");

If we want to pass form data across, we can also do an asynchronous POST request, like so:

formData = StructNew();
formData.var = "value";
asyncHTTP.post("http://www.site.com/anothertemplate.cfm", formData);


That is it! It is as simple as that.

Please download the library, give is a spin, and any questions, feedback, bugs or general comments, please either reply to this blog post, or shoot me an email .



Comments

Posted by Jordan Clark on 19 July 2006 08:04 PM

Cool tool. In the past I've used CFHTTP but set the timeOut attribute to 1, so the request is fired off, and the response ignored (well after a 1 sec pause). Do you think making requests with your CFC would be faster? Also I'm curious why you created your own java object for the request instead of using the apache httpclient that comes packaged with coldfusion?

Posted by Mark on 19 July 2006 10:07 PM

Jordan,

Good points all around -

On the CFHTTP with the timeout, I often found this wasn't that reliable (I may be wrong, but it just seemed that way), and I wanted a nice clean way to do this without having to basically hack your way through it.

In regards to my Java Obect - I also assumed it was the apache httpclient with ColdFusion, but in fact, it's not, it's a different library found here:
http://www.innovation.ch/java/HTTPClient/

The AsyncHTTP lib does actually use this library, if you look at the Java source, you can see it being used throughout. It made making HTTP calls a breeze.

Essentially AsyncHTTP is just a wrapper for this lib in a new java.lang.Thread, and a nice CFC wrapper over all that.

Nothing fancy, but hopefully useful.

Posted by Kurt Wiersma on 20 July 2006 02:08 AM

The other day I was wondering if this same type of technique could be used with the Java CFC proxy to call CFCs asyncronizely on CF Professional.

Posted by Jordan Clark on 20 July 2006 04:32 AM

Hey I just had a great idea for your tool, what about adding the ability for the asych request to save the response to a file. I was just thinking about mining some amazon data, if I could save the REST xml response to a file I could fire off a bunch of downloading threads at once, without having to wait for each one to finish.

Posted by Mark on 20 July 2006 04:30 PM

Kurt:

Unfortunatley, it can't be done, as CFCProxy is limited to being used by Enterprise only, otherwise, you could do exactly that... however, let's remember that CFC's with remote methods can be called via URL... :D

Jordan:

While an interesting idea, it would probably be better to fire off your async call to the page that does the mining.. which means you can do whatever you want in it. Doesn't seem to be a need to build that sort of functionality you need in here.

Posted by Andy Allan on 20 July 2006 06:32 PM

Interestingly enough, you could use the CFCProxy with CF7.0 Standard. However, this was then restricted when 7.01 came out.

Add Comment