Webmethods:How to Kill a Running Service from the Integration Server

Many of you have wondered how to kill a WebMethods service while its running on the Integration Server.

Its become a Pet Interview Question for many but the interviewer also expects one of the following facts which is not the most recommended method to kill a running service.

Here are some facts:

1. If you are running the service on debug mode, the service will get killed if you kill your developer.
2. If you are running the service on RUN mode, the service runs on the Integration Server and will not get killed.
3. If your client has activated the service but terminated the session, the service will still run on the IS.
4. If your webmethod service is hung, You will try to Reload the entire package. But this will NOT help.
5. You will also try to unload the package but this will NOT help too.

What you need to do is on a Thread Level. The idea is quite simple: Just get the list of all the current running threads and kill the service which you need.

I will try to explain this in a step by step manner. Developers please do try it out on your dev boxes as well.

Step 1:
Create a Flow service like this:

wm1

Step 2:
Lets create a Flow service which will get ALL running threads. This should take NO input but lets output a String containing all running threads on that Integration Server.

w2

Step 3:

Insert the following Java Code in your WebMethods Flow Service that you just created. The code is commented well enough.

//—BEGIN CODE—–
// Author: Hussain Fakhruddin (hussulinux@gmail.com)
// Date: 20090128
//Get the Current Thread
Thread currentThread = Thread.currentThread();
//Get all Thread Groups.
ThreadGroup rootThreadGroup = currentThread.getThreadGroup();
//Traverse to the Top Thread Level
while (rootThreadGroup.getParent() != null) {
rootThreadGroup  = rootThreadGroup.getParent();
}
//Getting all Threads
Thread [] threads = new Thread[1000];
int threadCount = rootThreadGroup.enumerate(threads, true);
String threadList = “”;
// Now traverse through all the Threads and use the ServerThread API from webmethods to get all the thread detail.
int i=0;
for (; i<threadCount ; ++i){
if (threads[i] instanceof com.wm.app.b2b.server.ServerThread) {
//Casting a raw thread into ServerThread type
com.wm.app.b2b.server.ServerThread serverThread = (com.wm.app.b2b.server.ServerThread) threads[i];
//Getting the service Name for which the thread belongs to
if (serverThread.getState().getService() != null){
threadList = threadList + serverThread.getState().toString() ;
java.util.Stack threadStack = (java.util.Stack) serverThread.getState().getCallStack();
for (Iterator iter=threadStack.iterator(); iter.hasNext();) {
Object threadObj = iter.next();
threadList = threadList + “\n ” + threadObj.getClass().getName();
threadList = threadList + “, ” + threadObj.toString();
}
}
}
}
IDataCursor cursor = pipeline.getCursor();
cursor.insertAfter(“threads”, threadList);
cursor.destroy();

//—–END OF CODE—–

Step 4: Now lets create another flow service “killThread” with the following inputs and outputs

wm3

Step 5: Now copy paste the following code. We’re going to traverse through the entire list and call the .interrupt() method to kill the process. Note: you need to provide FULL path of the service which you want to kill. You can get the full path from the webmethods admin panel (under package management).

//—BEGIN CODE—–
// Author: Hussain Fakhruddin(hussulinux@gmail.com)
// Date: 20090128
IDataCursor cursor = pipeline.getCursor ();
if (cursor.first (“serviceName”)) {
String serviceName = (String) cursor.getValue ();
Thread current = Thread.currentThread ();
ThreadGroup root = current.getThreadGroup ();
while (root.getParent () != null) {
root = root.getParent ();
}
Thread [] threads = new Thread [1000];
int count = root.enumerate (threads, true);
String sb = “Not Found”;
StringBuffer killedList = new StringBuffer ();
for (int i=0; i<count; i++) {
if (threads[i] instanceof com.wm.app.b2b.server.ServerThread) {
com.wm.app.b2b.server.ServerThread serverThread = (com.wm.app.b2b.server.ServerThread) threads[i];
if (serverThread.getState ().getService () != null) {
java.util.Stack stack = (java.util.Stack) serverThread.getState ().getCallStack ();
for (Iterator iter=stack.iterator (); iter.hasNext ();) {
Object obj = iter.next ();
String name = obj.toString ();
if (name.trim().equals (serviceName)) {
threads[i].interrupt ();
sb = “Interrupted”;
killedList.append (stack.toString ()).append (“\n”);
}
}
}
}
}
cursor.insertAfter (“status”, sb);
cursor.insertAfter (“killedList”, killedList.toString ());
cursor.destroy ();
}
//—–END OF CODE—–

That’s it folks! Save and run it.. Come back to me if you face problems..

People who read this article also read these...

11 Responses to “Webmethods:How to Kill a Running Service from the Integration Server”

  1. Stanley says:

    Hi Hussain,

    Thanks for this wonderful post on webmethods. Our company has been trying this out since long time and you’ve created just the right way….

    I will be implementing this in our UAT environment right away and send out for testing.

    Could you please pass me your email id, I’d like to discuss more things with you privately.

    Regards,

    Stanley
    Head of Technological Services,
    Cansoft India Pvt.Ltd.

  2. Sirat Moin says:

    Hussain,

    Excellent work as well. Although, I am not in webmethods integration now days, but i remembered that stopping a service that is long running was not possible.

    Excellent work and all the very best and keep blogging. You ignited me to start blogging as well [;)]

    Sirat Moin,
    Sr. Integration Consultant

  3. Yudi Ivankof says:

    Really a neat way to kill. I was just amazed we could use simple Java and stop the services… Ive tried this and it worked although I had doubts…!! Thanks a lot…! Can I have your email address, I would like to discuss a few more things with you.

    Yudi Ivankof.
    Project Manager,Romanian Technological Services.

  4. Prashant says:

    Hey, this is really superb.

  5. Sundar Patnaik says:

    I tried to run the service found following errors . Please help.

    /opt/webmethods71/IntegrationServer/packages/N045605/code/source/N045605/utils.java:65: cannot find symbol
    symbol : method getService()
    location: class java.lang.Thread.State
    if (serverThread.getState().getService() != null){
    ^
    /opt/webmethods71/IntegrationServer/packages/N045605/code/source/N045605/utils.java:67: cannot find symbol
    symbol : method getCallStack()
    location: class java.lang.Thread.State
    java.util.Stack threadStack = (java.util.Stack) serverThread.getState().getCallStack();

  6. Do you have server libraries in your system? It works fine for me and for many others…

    Please check if you have copy pasted the exact code..

  7. Francis says:

    Hi Hussain,

    Thanks for the great tool. I’m sure this will be helpfull, but I am also having the same error.

    What server librairies are you talking about exactly?

    Thanks in advance for your help.

    ————-

    /wmbadv1/IntegrationServer/packages/ft_temp/code/source/ft_temp.java:64: cannot find symbol
    symbol : method getService()
    location: class java.lang.Thread.State
    if (serverThread.getState().getService() != null){
    ^
    /wmbadv1/IntegrationServer/packages/ft_temp/code/source/ft_temp.java:66: cannot find symbol
    symbol : method getCallStack()
    location: class java.lang.Thread.State
    java.util.Stack threadStack = (java.util.Stack) serverThread.getState().getCallStack();
    ^
    2 errors

  8. Sundar Patnaik says:

    Francis,

    Looks like getState() is deprecated now. Try the following,it works..

    ————————

    IDataCursor cursor = pipeline.getCursor ();
    if (cursor.first (“serviceName”)) {
    String serviceName = (String) cursor.getValue ();
    Thread current = Thread.currentThread ();
    ThreadGroup root = current.getThreadGroup ();
    while (root.getParent () != null) {
    root = root.getParent ();
    }
    Thread [] threads = new Thread [1000];
    int count = root.enumerate (threads, true);
    String sb = “Not Found”;
    StringBuffer killedList = new StringBuffer ();
    for (int i=0; i<count; i++) {
    if (threads[i] instanceof com.wm.app.b2b.server.ServerThread) {
    com.wm.app.b2b.server.ServerThread serverThread = (com.wm.app.b2b.server.ServerThread) threads[i];
    if (serverThread.getInvokeState ().getService () != null) {
    java.util.Stack stack = (java.util.Stack) serverThread.getInvokeState ().getCallStack ();
    for (Iterator iter=stack.iterator (); iter.hasNext ();) {
    Object obj = iter.next ();
    String name = obj.toString ();
    if (name.trim().equals (serviceName)) {
    threads[i].interrupt ();
    sb = “Interrupted”;
    killedList.append (stack.toString ()).append (“\n”);
    }
    }
    }
    }
    }
    cursor.insertAfter (“status”, sb);
    cursor.insertAfter (“killedList”, killedList.toString ());
    cursor.destroy ();
    }

  9. Francis says:

    Thank you Sundar, this worked perfectly.

    I appreciate it.

  10. Prashant says:

    /opt/webmethods-dev1/IntegrationServer/packages/N676074/code/source/N676074/utils.java:45: illegal character: \8221
    if (cursor.first (\u201DserviceName\u201D)) {
    ^
    /opt/webmethods-dev1/IntegrationServer/packages/N676074/code/source/N676074/utils.java:45: illegal character: \8221
    if (cursor.first (\u201DserviceName\u201D)) {
    ^
    /opt/webmethods-dev1/IntegrationServer/packages/N676074/code/source/N676074/utils.java:54: illegal character: \8220
    String sb = \u201CNot Found\u201D;
    ^
    /opt/webmethods-dev1/IntegrationServer/packages/N676074/code/source/N676074/utils.java:54: illegal character: \8221
    String sb = \u201CNot Found\u201D;
    ^
    /opt/webmethods-dev1/IntegrationServer/packages/N676074/code/source/N676074/utils.java:66: illegal character: \8220
    sb = \u201CInterrupted\u201D;
    ^
    /opt/webmethods-dev1/IntegrationServer/packages/N676074/code/source/N676074/utils.java:66: illegal character: \8221
    sb = \u201CInterrupted\u201D;
    ^
    /opt/webmethods-dev1/IntegrationServer/packages/N676074/code/source/N676074/utils.java:67: illegal character: \8221
    killedList.append (stack.toString ()).append (\u201D\n\u201D);
    ^
    /opt/webmethods-dev1/IntegrationServer/packages/N676074/code/source/N676074/utils.java:67: illegal character: \92
    killedList.append (stack.toString ()).append (\u201D\n\u201D);
    ^
    /opt/webmethods-dev1/IntegrationServer/packages/N676074/code/source/N676074/utils.java:67: illegal character: \8221
    killedList.append (stack.toString ()).append (\u201D\n\u201D);
    ^
    /opt/webmethods-dev1/IntegrationServer/packages/N676074/code/source/N676074/utils.java:73: illegal character: \8221
    cursor.insertAfter (\u201Dstatus\u201D, sb);
    ^
    /opt/webmethods-dev1/IntegrationServer/packages/N676074/code/source/N676074/utils.java:73: illegal character: \8221
    cursor.insertAfter (\u201Dstatus\u201D, sb);
    ^
    /opt/webmethods-dev1/IntegrationServer/packages/N676074/code/source/N676074/utils.java:74: illegal character: \8221
    cursor.insertAfter (\u201DkilledList\u201D, killedList.toString ());
    ^
    /opt/webmethods-dev1/IntegrationServer/packages/N676074/code/source/N676074/utils.java:74: illegal character: \8221
    cursor.insertAfter (\u201DkilledList\u201D, killedList.toString ());
    ^
    /opt/webmethods-dev1/IntegrationServer/packages/N676074/code/source/N676074/utils.java:77: ‘)’ expected
    // — <> —
    ^
    /opt/webmethods-dev1/IntegrationServer/packages/N676074/code/source/N676074/utils.java:80: illegal start of expression
    }
    ^
    15 errors

  11. Srinivas Barla says:

    Prashant,

    It seems you are copying the code from browser and paste that to wM developer.

    Try, using different editor (TextPad) change the quotes to “smart quotes” and then paste that on to webMethods Developer and then compile.

Leave a Reply