How to make ColdFusion MX go to sleep

coldfusion Many people have had the need for a ColdFusion page to sleep, typically between iterations of a loop. There is a tag called CFX_Sleep in the Tag Gallery, but in ColdFusion MX you don't need a CFX tag to make the current processing thread sleep using the static sleep method on the java.lang.Thread class, part of the standard java platform. Because CFMX doesn't allow us to call a static method without an object reference, we have to first use CreateObject, or CFOBJECT to get an instance of a java.lang.Thread object. We then call the Thread.sleep(long) method, which takes in the number of miliseconds to sleep for.
<cfset thread = CreateObject("java", "java.lang.Thread")>
About to sleep for 5 seconds...<cfflush>
<cfset thread.sleep(5000)>
Done sleeping.



Related Entries

20 people found this page useful, what do you think?

 Download FuseGuard WAF for ColdFusion

Trackbacks

Trackback Address: 85/D9D76E3AFB002C279217932083935F71

Comments

On 03/08/2004 at 2:06:25 PM UTC Patrick Steil wrote:
1
BTW, this also works on CF5!!!!

On 05/09/2004 at 1:05:38 AM UTC john wrote:
2
Very good. Helped me with the problem I was having. Thanks!

On 06/16/2004 at 4:50:53 PM UTC Alex wrote:
3
I am very frustrated. This code doesn't seem to work! I am getting this error: The JVM library path is empty. To use Java objects or Java CFXs, you need to install (if not already installed) the JDK or JRE and specify the path of the JVM in the ColdFusion Administrator. Please read the release notes regarding the version of the software that is needed.

Any ideas??

On 06/23/2004 at 11:25:52 AM UTC Steve Tringali wrote:
4
Two words: Thank you.

On 07/28/2004 at 6:35:33 AM UTC Eduard wrote:
5
I had this error when I patched this code on my template. Pls Help!

Unhandled System exception !

Failed to create JavaVM. JNI_CreateJavaVM returned error code JNI_ERR (this may indicate an unrecognized option string)

On 10/22/2004 at 3:39:28 PM UTC William wrote:
6
Code worked beautifully for me. I had a page that updated my Access database, then sent an HTML email which was another webpage that queried the info just updated. The problem was that Access was slower than ColdFusion, so the email you received said nothing had been updated.

Putting the thread to sleep for 5 seconds after the update gave Access enough time to catch up.

On 01/10/2005 at 5:01:38 PM UTC Scott wrote:
7
Thank you, thank you!! I don;t have access to my server to load custom tags so <cfx_sleep> was not an option. This was perfect. Thank you so much!

On 04/22/2005 at 1:03:20 PM UTC Abhisekh wrote:
8
Any suggestion will be higly appreciated on how to resolve this error "Failed to create JavaVM. JNI_CreateJavaVM returned error code JNI_ERR".

On 06/06/2005 at 9:22:39 PM UTC mos wrote:
9
I get the following error when I added the code below

<cfset thread = CreateObject("java", "java.lang.Thread")> About to sleep for 5 seconds...<cfflush> <cfset thread.sleep(5000)> Done sleeping. ---------------------

Error: Unhandled System exception ! java.security.AccessControlException: access denied (java.lang.RuntimePermission createClassLoader). Java exception occurred in attempt to construct object.

How do I fix this ?

thanks, mos

On 11/16/2005 at 2:23:53 AM UTC bkbk wrote:
10
<cftry> <!--- your sleep code ---> <cfcatch type="any"> Error-message: sleep interrupted </cfcatch> </cftry>

On 12/22/2005 at 12:15:25 AM UTC Umbrae wrote:
11
If you are receiving this error: "The selected method sleep was not found"

You may need to cast your variable that you're sending to sleep to a long via JavaCast. I.E.

<cset thread.sleep(JavaCast('long', yourVariable)) />

HTH, -Umbrae

On 12/22/2005 at 12:16:12 AM UTC umbrae wrote:
12
Er. Thats, "cfset". *cough*

On 04/12/2006 at 2:07:15 PM UTC William Kossack wrote:
13
neat! We have a large CF app that has recently migrated from CF 5. I'm investigating a duplicate thread problem that has been discovered.

The app produces a second thread with the same time stamp in the log.

I've seen it with inserts into a database where errors are returned when a key is duplicated by the duplicate thread. Is there any way of catching duplicate threads in CF?

On 04/12/2006 at 2:08:10 PM UTC William Kossack wrote:
14
neat! We have a large CF app that has recently migrated from CF 5. I'm investigating a duplicate thread problem that has been discovered.

The app produces a second thread with the same time stamp in the log.

I've seen it with inserts into a database where errors are returned when a key is duplicated by the duplicate thread. Is there any way of catching duplicate threads in CF?

On 06/25/2006 at 12:33:05 PM UTC Hughes wrote:
15
This code worked fine when in a CFM to show some text wait 5 seconds and then show some more text.

When I moved it to a CFC file the 5 second pause was there but the first set of text did not show up until after the pause.

Is this something that can not be done with Components??? Thanks

On 07/11/2006 at 2:36:01 PM UTC Josh Giese wrote:
16
awesome. This will come in handy

On 03/17/2008 at 2:39:53 PM UTC STITCH wrote:
17
wrong ColdFusion

On 04/08/2010 at 12:48:27 PM UTC George wrote:
18
"When I moved it to a CFC file the 5 second pause was there but the first set of text did not show up until after the pause."

- try using cfflush

Post a Comment




  



Spell Checker by Foundeo

Recent Entries



foundeo


did you hack my cf?