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:
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.
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
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..



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.
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
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.
Hey, this is really superb.
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();
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..
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
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 ();
}
Thank you Sundar, this worked perfectly.
I appreciate it.
/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
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.
Excellent post Hussain.
I was wondering if Unix or AIX platform allows you to kill the threads by some of their utilities instead of writing java services, do you something about that?
Thanks,
Ishti.
Hi,
Actually in Unix /Aix we have processes but as far as Java is concerned, the thread runs under Java environment and we’re trying to kill those threads and not processes.
We shouldn’t confuse between forked child processes and threads in Java.
I am not sure if JVM provides any mechanism by which a thread can be killed by some OS call.
Regards,
Hussain
Good day. To start with I would like to say that I truly like your webpage, just determined it the past week but I’ve been following it sometimes since then.
I appear to concur with most of your thinkings and opinions and this post is no exception. I fully
Thank you to get a fantastic web site and I hope you preserve up the beneficial operate. If you do I will keep on to checked out it.
Have a great evening.
Hi Hussain!
I see that killing a thread is deprecated in JAVA. Can you pls suggest me whether your utility is safe to interrupt a HUNG service.
I have a scenario where, there were 4 hung threads for the given service and 2 HUNG threads for its child service. When I ran the killThread service, output was perfect. It gave the total list of all the 6 services and status was “Interrupted”. When I checked it in webM IS Console->service usage, I still see the number of HUNG threads as it is.
Again I ran the service, the output was same(Displayes all the 6 services with status “Interrupted”). If I run 10 times, Im getting the same output, but its not actually Interrupting. Pls advise me on this.
Hi Hussain,
This java code works really well thanks for posting.
As I have an environment where more than 50 inegration servers are running, in which hung services may come on any server any time.
if I have to implement above kill thread service in my environment I have to deploy this service in each and every integration service which is quiet heavy. Is there any way we can have this service in common place from where we can kill any IS hung service in the envirronment. ideas are welcome from all.
You will have to deploy the service to every IS because all IS run on their own JVM and you cannot kill a JVM thread from outside, It will corrupt the memory.
Usually you need to kill a thread becasue its hung , so you rather not care about doing it gracefully. So this method works perfectly well. If you need thread to die with proper non-depricated API then you’ll have to program it that way.. !
This is because the thread may be running on a higer priority than your killing program. SO the killing program doesn’t get chance to execute and kill the thread. Check more on priorities.