I’d published a quick app called Memory Explorer I threw together using Rob Gonda’s ajaxCFC framework a while back. However, one thing that’s been bugging me ever since I made it was that I was using JSP pages to create certain Java objects and then putting them in a scope that ColdFusion could access – specifically the Request scope.
I always knew there had to be a way to create not only a Thread object, but also an array of Threads, however I didn’t make the time to investigate it. Then, by chance, I stumbled across some code I did last year regarding Bytes and Byte arrays and realized that I could use the same principles (after a few blind stabs in the dark) to create what I needed.
I also, then, re-found an article on Chrisian Cantrell’s site regarding creating Byte arrays in ColdFusion which I remembered that I referenced when I did my original code. Using my code and his examples as inspiration, I finally was able to access all of the threads currently running on my JVM and output them nicely to the screen. Here’s what you need to do.
First, create a Thread object which captures the current thread of your JVM
thisThread = createobject(“java”, “java.lang.Thread”);
Then, create a native Java Thread type by using the getClass() method
threadClass = createobject(“java”, “java.lang.Thread”).init().getClass();
Now, you need an Array to hold all of the threads because the ThreadGroup.enumerate() method requires a native Thread array as an argument.
threadList = createobject(“java”, “java.lang.reflect.Array”).newInstance(threadClass,thisThread.activeCount());
Next, you populate the Thread array with each item in the ThreadGroup.
thisThread.currentThread().getThreadGroup().enumerate(threadList);
Now you have your array of Threads which you can access in ColdFusion as a simple one-dimenstional array.
<cfdump var=“#threadList[1]#”>
4 Responses for "Java Native Types (Threads) via ColdFusion"
I am new to this concept. i understood how you are getting all the threads from JVM. But what are we going to do with this threat; how is it going to be useful for us?
For monitoring purposes
Steve,
I’m curious what your output is on the cfdump. It looks to me like you would get exactly the same thing by doing this:
obj = CreateObject(“java”,”java.lang.Thread”);
curThread = obj.currentThread();
cfdump var=”#curThread#”
What am I missing?
Cheers,
Alex
It dumps all of the active JVM threads. The currentThread only dumps the one currently being used by ColdFusion
Leave a reply