1

I'm trying to use JavaLoader to load a java (HttpAsyncClient) class into ColdFusion.

client = loader.create("org.apache.commons.HttpAsyncClient")

How do we know the reference that is org.apache.commons.HttpAsyncClient? I thought if you open the jar file and follow the directory structure, it will give you the reference path. But I don't think this is true.

I'm trying to use the HttpAsyncClient but I'm unable to load it:

client = loader.create("org.apache.commons.HttpAsyncClient") returns a class not found error.

Loader is a reference to JavaLoader, which loads Java classes into your CF server.

Mohamad
  • 34,731
  • 32
  • 140
  • 219
  • Java packages *has* to follow the directory structure. `org.apache.commons.httpclient` will be found in the `org/apache/commons/httpclient` directory. – aioobe Oct 13 '11 at 20:19

2 Answers2

2

I don't know ColdFusion. You probably have to specify the full path to the class, not just the package containing the class.

According to an example I found the full package and class name is this: org.apache.http.nio.client.HttpAsyncClient

You can also use the javadoc to find out the package and class names: http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/index.html

Getting something async going with an interface like this will probably be brutal. I would suggest trying the sync version first.

EDIT

I would try adapting this sync example to CF: http://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientWithResponseHandler.java

When you instantiate HttpGet you have to pass extra parameters to init() as they do in this example: http://www.coldfusionjedi.com/index.cfm/2009/5/29/Generating-Speech-with-ColdFusion-and-Java

Sarel Botha
  • 12,419
  • 7
  • 54
  • 59
  • that's it. That did it. Thanks. Why would you say it would be brutal? I use things like AntiSamy and string.utils all the time... this one seemed a little different though... the path was longer. – Mohamad Oct 13 '11 at 20:27
  • Typically with an async interface you have to create your own class which implements their interface, then you pass an instance of your class when initiating the call and as events occur your class is called. I don't know if it's easy or hard to do all that using ColdFusion or not. – Sarel Botha Oct 13 '11 at 20:36
  • 1
    @Mohamad - In terms of path, there is nothing different about that class from any other. The full path can be long or short. All depends on how the author(s) chose to organize the classes. But you always have to supply the full path .. whatever it is. (And technically .class files do not *have to* be packaged within a jar, though that is far less common). – Leigh Oct 13 '11 at 20:49
  • @Leigh and @Sarel, thank you. It turns out I might not be able to use this after all. I'm getting a `No matching Constructor for org.apache.http.nio.client.HttpAsyncClient() found` whenever I call init() or start() on the client object it's instantiating, though)! Thanks for the help. – Mohamad Oct 13 '11 at 20:51
  • That is because it is an `interface`. You cannot instantiate it. It is essentially just an outline. Same as a CF `interface`. As Sarel said, you have to write your own implementation that implements that interface .. or find a library/class that already does that. Check the API. But what is it you are trying to do that you cannot achieve with plain CF code or an existing CF tool? Because Mark Mandel released an asynchronous http client a lo-oong time ago http://compoundtheory.com/?action=displayPost&ID=137 . – Leigh Oct 13 '11 at 21:04
2

Rather than reinvent the wheel, why not try an existing tool like Mark Mandel's AsyncHTTP library?

Update: From the comments, that tool is ACF only. So you might try using the concrete class DefaultHttpAsyncClient as shown in the Asynchronous HTTP Exchange example.

Leigh
  • 28,765
  • 10
  • 55
  • 103
  • I just found that. I'm actually reading the docs on it now. I want to pass data between two websites. :D – Mohamad Oct 13 '11 at 21:26
  • some further info on this. It only runs on ACF, and it uses the same http engine that CF uses. I spoke to Mark about it and he said you might as well just use `cfthread`to achieve the same thing. Which I did. It's terribly slow, though. – Mohamad Oct 14 '11 at 01:44
  • Oh, sorry. You did not mention using a different engine. I am not sure what is so slow. But did you review the library's API like I suggested? The Asynchronous HTTP exchange example looks promising. It uses a concrete implementation called `DefaultHttpAsyncClient` http://hc.apache.org/httpcomponents-asyncclient-dev/examples.html – Leigh Oct 14 '11 at 02:08
  • I'm going to have a read now. In my tests, the Railo implementation is doing about 5 requests per second using cfthread; I need to be near something like 25 requests per second. If I'm understanding you correctly with pointing me to the API, would I have to translate that code to CF, or write a CF wrapper that interacts with it? This is new territory for me. Maybe it's time to learn Java. – Mohamad Oct 14 '11 at 02:25
  • Here's the test I'm doing, just for reference: http://pastie.org/private/svl7yomsjywhipkm1lz8w – Mohamad Oct 14 '11 at 02:27
  • 1
    Yep. I would translate it to CF. With all the cfscript enhancements, it is not that different from CF. And fortunately, it is only about six (6) lines ;) This article has some good tips on translating http://www.coldfusionmuse.com/index.cfm/2009/2/23/Unpacking-Java-In-Coldfusion – Leigh Oct 14 '11 at 02:39
  • thank you. Your example works fine. In order to run several asynch requests, I can just use a new httpGet() objhect, right? What I mean is, I don't have to create a new httpClient object for each request... ? – Mohamad Oct 14 '11 at 16:10
  • I guess what I should ask is can I just store the httpClient object in my server or application scope and reuse it for all my requests... – Mohamad Oct 14 '11 at 16:45
  • I am not that familiar with it. But the API says it is not thread safe by default. It mentions a special connection manager for multi-threading http://hc.apache.org/httpclient-3.x/threading.html – Leigh Oct 14 '11 at 17:48