Apr 13, 2014

Get Active threads / Kill any specific Thread using CF Admin API.

As we know we can create threads using <cfthread> tag.
We can run, join or terminate a thread but lets say we have a real world scenario and we want to show all CF threads in our web Application which are running and do all operations though web App.

So for this we have to get Metadata Info for all threads.

In my Last post   we have discussed about how we can access CF Administrative APIs.
So to achieve this we are going to use CF Admin API using servermonitoring.cfc.
You can also visit Server Monitoring in ColdFusion Administrator and see those threads and Manage it.

<cfscript>
public array function getActiveThreadsRunning(adminPassword) hint="Get all active threads running currently" {
local.threadArrray = arrayNew(1);
//Creating Admin Object
request.adminObject = createobject("component","cfide.adminapi.administrator");
//Creating Server Monitoring Object
local.monitorObject = createobject("component","cfide.adminapi.servermonitoring");
//Login to Admin API
local.adminObject.login(arguments.adminPassword);
//Get all Active Thread info
local.threadArrray = local.monitorObject.getAllActiveCFThreads();
//log out from the admin API login
local.adminObject.logout();
return local.threadArrray;
}
</cfscript>
getActiveThreadsRunning() will return all metadata info of Threads which are running currently like ThreadName, TimeTaken,CFStrackTrace etc.

Let’s move on to see how we can Kill any thread.
You may get into situation where any threads are running for longer period of time and because of that other process are waiting in queue:

We can call abortCFThread() present under servermonitoring.cfc to achieve this.

<cfscript>
public string function killActiveThread(threadName,adminPassword) hint="Kill specified thread which are running" {
//Creating Admin Object
local.adminObject = createobject("component","cfide.adminapi.administrator");
//Creating Serbver Monitoring Object
local.monitorObject = createobject("component","cfide.adminapi.servermonitoring");
//Login to Admin API
local.adminObject.login(arguments.adminPassword);
//kill thread now by calling abortCFThread. It accepts thread name
local.killedThread = local.monitorObject.abortCFThread(arguments.threadName);
//logout from admin API
local.adminObject.logout();
// return true/false based on response.
return local.killedThread;
}
</cfscript>
view raw killThreads.cfc hosted with ❤ by GitHub
Hope it will help you.
Happy Coding :)

No comments:

Post a Comment