How to kill a Thread in SAP BC

Hi all,

looking for a little advice for killing a thread - the code compiles ok but on testing via my flow service it gets into a deadlock [endless looping ] is there a way of killing this without bouncing my server? I’m aware of the Thread API but would need to know how I may stop this as it has a habit of hanging my system!! I am using SAP BC 46 [ WebMethods 46 Integration Server with Corefix2 SP1 &2 ]
running on Windows 2000.

any pointers gratefully accepted…especially if this has been addressed before.

tia
T

Tee,
Are you using a ServiceThread (it’s the Runnable returned by doThreadInvoke( … ) ) or are you relying solely on the jdk?
Either way, it’s better to write code that won’t deadlock, rather than trying to kill the process. The stop() method is deprecated in the jdk now, etc., as you probably know.
Have you looked at the wait and notify services in the built in services, and looked at the example in wmsamples? Perhaps you could think about synchronizing on the object both your threads want to use?

Nick F

many thanks, the idea was to call a commonly used java service, within a flow, which creates an idoc. The input is nearly identical to what normally comes in, but it is deadlocking!?! proving to be frustrating.
What I am proposing to do is call the java service as a thread [complete with the required inputs] and if it does not run within a certain period of time destroy it and do the requried gc and through a service error.

does this sound feasible to you?

Hi, tee.

What is your Flow doing when it deadlocks? Specifically, have you stepped through it in Developer using F7 to see where the hang occurs?

If you are using a parent Flow to call a child Flow, use F8 to “step into” the child Flow.

Thanks.

I F7 through and the Java service is called from within the main flow, the lock occurs whilst it runs the service, what is frustrating is the fact that it runs for lots of other services. being Java I can not step into into to determine where it is failing [looping endlessly] which is why I am looking invoke it as a thread, if it runs ok then proceed to next step else to kill it if it runs for longer than x time and ‘error’ out.
value your thoughts on this greatly.

thnx
Tee

Try creating a service which finds out all the threads running in the server. Then if you can figure out which thread is giving you trouble from the names of the thread, you can stop that thread.
Would this work?
Thanks

You might try outputting log statements from your Java service to see what it is doing. Not as efficient nor convenient as stepping through with a debugger but can be quite effective.

This article on using a Java timeouts suggests an interesting way to handle this situation. Basically, their first suggestion talks about spawning a “timer” thread and having that thread “do something” when it finds that your service has stalled for a long time.

So based on that guy’s code, I wrote a test service and it works. The Java source code is below:… you can cut and paste this into a new webMethods Java service - don’t worry about the Timer class - WM automatically creates an ‘inner class’ in the service.


class Timer extends Thread
{
public boolean timeoutExceeded = false;
/** Rate at which timer is checked */
protected int m_rate = 100;

/** Length of timeout */
private int m_length;

/** Time elapsed */
private int m_elapsed;

/**

  • Creates a timer of a specified length
  • @param length Length of time before timeout occurs
    */
    public Timer (int length )
    {
    // Assign to member variable
    m_length = length;

// Set time elapsed
m_elapsed = 0;
}

/** Resets the timer back to zero */
public synchronized void reset()
{
m_elapsed = 0;
}

/** Performs timer specific code */
public void run()
{
// Keep looping
for (;:wink:
{
// Put the timer to sleep
try
{
Thread.sleep(m_rate);
}
catch (InterruptedException ioe)
{
continue;
}

// Use ‘synchronized’ to prevent conflicts
synchronized ( this )
{
// Increment time remaining
m_elapsed += m_rate;

// Check to see if the time has been exceeded 
if (m_elapsed > m_length) 
{ 
 // Trigger a timeout 
 timeout(); 
 break ; //Breaks out of the endless for loop and returns, ending thread. 
} 

}

}

System.err.println (“Exiting timer ‘run’ method”);
}

// Override this to provide custom functionality
public void timeout()
{
System.err.println (“Network timeout occurred… setting status flag”);
timeoutExceeded = true;
}
}

Timer timer = new Timer(3000);
String errorMessage = “”;
timer.start();
while (true) {
System.err.println (“In while loop”);
if (timer.timeoutExceeded == true) {
errorMessage = “Timeout exceeded… terminating function call”;
System.err.println (errorMessage);
break;
}
}
//Reset the timer
//timer.reset();

if (errorMessage != “”)
{
throw new ServiceException(errorMessage);
}


The change to the author’s service is that I end the timer thread before I throw the Service exception (it keeps running otherwise)

Many thanks Sonam - sorry for delayed response!! And thanks to the other guru’s who added very valuable 50 cents [some of which I had already attempted]

:slight_smile:
T