Wednesday, February 08, 2012

Kill a ColdFusion Thread Using Admin API(ColdFusion - 9)

In ColdFusion we can kill a thread of same page request by using "cfthread" tag with action=terminate.
This is the case when we are dealing with the thread of a currently running page request.

   But sometimes it needed to kill some threads which are created by some other page request and the execution of the thread hung or taking too many times. At that time we can kill those type  threads using the following user defined functions in coldfusion.

<cffunction name="getAllActiveThreads" access="public" description="Returns all currently active threads." output="false" returntype="Array">
        <cfargument name="CFAdminPW" required="true" type="string" hint="The password to the cf admin." default="#application.CFAdminPW#" />
        <cfscript>
            local.returnedObject = arrayNew(1);
           
            try{
                //Creating admin and servermonitoring Object
                local.adminObj = createobject("component","cfide.adminapi.administrator");
                local.monitorObj = createobject("component","cfide.adminapi.servermonitoring");
               
                //Login to Admin API
                local.adminObj.login(arguments.CFAdminPW);
               
                //Get all active thread information
                local.returnedObject = local.monitorObj.getAllActiveCFThreads();
               
                //log out from the admin API login
                local.adminObj.logout();
            } catch(Any e){
            }
            return local.returnedObject;
        </cfscript>
    </cffunction>
 The above function will return all the active threads information in an array Collection Object. Now we can display the information in the UI. If we will dump the thread information then it looks like:



    Now, in the second step we will kill any specific thread. From the above dump image you will find fields "THREADNAME" and "CFTHREADNAME". Here THREADNAME is the name assigned to the thread by the system and CFTHREADNAME is the name assigned to a thread in the "name" parameter during thread creation. So, when you are trying to kill one thread forcefully then always pass the THREADNAME instead of CFTHREADNAME as it will be unique always.

Below is the coldfusion function to kill a thread.

<cffunction name="killActiveThread" access="public" description="Kill One Hung active cfthread" output="false" returntype="any">
        <cfargument name="threadName" required="true" type="string" hint="The thread name. It can be cfthread name or THREADNAME  of the thread Object" />
        <cfargument name="CFAdminPW" required="true" type="string" hint="The password to the cf admin." default="#application.CFAdminPW#" />
        <cfscript>
            try{
                //Creating Object Of Admin and Server monitoring API
                local.adminObj = createobject("component","cfide.adminapi.administrator");
                local.monitorObj = createobject("component","cfide.adminapi.servermonitoring");
               
                //login to admin API
                local.adminObj.login(arguments.CFAdminPW);
               
                //kill the thread whose is present the argument
                local.killedThread = local.monitorObj.abortCFThread(arguments.threadName);
               
                //logout from admin API
                local.adminObj.logout();
               
                return "The thread:#arguments.threadName# has been killed sucessfully!";
            } catch(Any e) {
                return e.message;
            }
        </cfscript>
    </cffunction>
 Hope you will enjoy with the code. :)

5 comments:

  1. Hi, thanks a ton for this post. It came in very handy at a time when I had been splitting my hair over external thread control.

    Just one pint of note though, the killActiveThread() function requires the THREADNAME of the thread Object and not the cfthreadname.

    Great post! Thanks again. :)

    ReplyDelete
    Replies
    1. Very glad that this post help you. I understand your point. But, for each thread we are creating our system generates an unique thread name even though we are trying to create threads with same name. So to avoid confusion it is better to use system generated thread name during thread killing.

      If you will say again that we can not create two threads with same name from the same page request. Then we can create two thread with the same name using two different page request.

      If you have any confusion then I can help you.

      Delete
  2. I guess admin password is required?

    ReplyDelete
  3. BlueHost is ultimately the best website hosting provider for any hosting plans you might need.

    ReplyDelete

Followers