Skip to content

Instantly share code, notes, and snippets.

@JamoCA
Last active July 31, 2021 12:46
Show Gist options
  • Save JamoCA/6b63a2ce2bbeb8e9c89b798a2c48e9a7 to your computer and use it in GitHub Desktop.
Save JamoCA/6b63a2ce2bbeb8e9c89b798a2c48e9a7 to your computer and use it in GitHub Desktop.
ColdFusion utility UDFs to identify running threads; returns count or array of script files. #cfml
<cfscript>
numeric function getActiveThreadCount() hint="I return quantity of active requests (not including cf-threads)" {
local.threadCount = javacast("int", 0);
local.thread = createobject("java","java.lang.Thread");
local.stackTrace = local.thread.getAllStackTraces();
local.stackArray = local.stackTrace.entrySet().toArray();
for (local.stackIndex=1; local.stackIndex < arrayLen(local.stackArray); local.stackIndex+=1){
try{
local.thisThread = local.stackArray[local.stackIndex].getValue();
for (local.threadIndex=1; local.threadIndex < arrayLen(local.thisThread); local.threadIndex+=1){
local.thisThreadFile = local.thisThread[local.threadIndex].getFileName();
if (local.keyExists("thisThreadFile") and listFind("cfm,cfc", lCase(listLast(local.thisThreadFile,".")))){
local.threadCount += 1;
break;
}
}
} catch (any error){};
}
return local.threadCount;
}
array function getActiveThreadScripts() hint="I return an array of active request file paths" {
local.threadFiles = [];
local.thread = createobject("java","java.lang.Thread");
local.stackTrace = local.thread.getAllStackTraces();
local.stackArray = local.stackTrace.entrySet().toArray();
for (local.stackIndex=1; local.stackIndex < arrayLen(local.stackArray); local.stackIndex+=1){
try{
local.thisThread = local.stackArray[local.stackIndex].getValue();
for (local.threadIndex=1; local.threadIndex < arrayLen(local.thisThread); local.threadIndex+=1){
local.thisThreadFile = local.thisThread[local.threadIndex].getFileName();
if (local.keyExists("thisThreadFile") and listFind("cfm,cfc", lCase(listLast(local.thisThreadFile,".")))){
arrayAppend(local.threadFiles, local.thisThreadFile);
break;
}
}
} catch (any error){};
}
return local.threadFiles;
}
</cfscript>
<cfoutput>
<h1>getActiveThreadCount() and getActiveThreadScripts()</h1>
<p>2021-07-23: ColdFusion utility UDFs to identify running threads; returns count or array of script files.<br>
Tweet: <a href="https://twitter.com/gamesover/status/1418725566874796038">https://twitter.com/gamesover/status/1418725566874796038</a></p>
<p><b>Server:</b> #Server.ColdFusion.ProductName# #Server.ColdFusion.ProductVersion#</p>
<cfset start = getTickCount()>
<p><b>getActiveThreadCount:</b> #getActiveThreadCount()# (#numberFormat(getTickCount()-start)# ms)</p>
<cfset start = getTickCount()>
<cfdump var="#getActiveThreadScripts()#" label="getActiveThreadScripts">
<div>(#numberFormat(getTickCount()-start)# ms)</div>
</cfoutput>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment