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 (;
{
// 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)