You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@axis.apache.org by Donald Gorgonzola <do...@hotmail.com> on 2004/07/21 03:55:21 UTC

How to stop threads started by web service when Axis is stopped?

I am running Axis 1.1 with Tomcat 5.0.25 (with Java 1.4.2)

I have a web service call that starts a thread.  If axis is stopped by the 
Tomcat Web Application Manager, the thread continues to run.  It doesn't 
receive an interrupt exception.

Is there a way to have threads started by web services be stopped when Axis 
stops?
Is there a variable the threads could monitor to see if they should 
shutdown?

I created a sample .jws and client to illustrate the problem.
  cp ThreadTest.jws $CATALINA_HOME/webapps/axis
  java ThreadTestClient show


/*
* ThreadTest.jws
* Tests starting and stopping a thread in a web service call.
*/
public class ThreadTest
    implements Runnable
{

    /** Prints out a message every 2 seconds. */
    public void
    run ()
    {
        for (int i=0; i<300; i++) {
            try {
                System.out.println ("Inside thread " + i + " " +
                                    Thread.currentThread().toString());
                Thread.sleep(2000); // sleep 2 seconds
            } catch (InterruptedException ie) {
                System.out.println ("Received interrupt, exiting thread: " + 
ie);
                return;
            }
        }
    }

    /** Starts a thread with the given name. */
    public String
    start(String name)
    {
        ThreadGroup taskGroup = new ThreadGroup("Test Group");
        Thread myThread = new Thread(taskGroup, this, name);
        myThread.setDaemon(true);
        myThread.start();

        return ("started " + name);
    }

    /** Sends the thread with the given name an interrupt. */
    public String
    stop(String name)
    {
        Thread[] tarray = new Thread[100];

        for (int i=0; i<Thread.enumerate(tarray); i++) {
            // XXX: check to make sure it is in the expected thread group
            if (tarray[i].getName().equals(name)) {
                tarray[i].interrupt();
                return ("Thread " + name + " stopped");
            }
        }
        return ("Thread " + name + " not found");
    }

    /** Returns a string of all the current active threads. */
    public String
    show ()
    {
        String retStr = "";
        Thread[] tarray = new Thread[100];
        int numThreads = Thread.enumerate(tarray);
        if (numThreads == 0) {
            return ("No threads found.");
        }
        for (int i=0; i<numThreads; i++) {
            retStr += ((i+1) + ". " + tarray[i].toString() + " " + 
tarray[i].isInterrupted() + "\n");
        }

        return (retStr);
    }
}



/*
* ThreadTestClient.java
* Test client used to test ThreadTest.jws.
*/
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;

import javax.xml.rpc.ParameterMode;

public class ThreadTestClient
{
    static void
    usage()
    {
        System.err.println("Usage: ThreadTestClient start <name> | stop 
<name> | show");
    }

    public static void
    main(String [] args)
        throws Exception
    {
        String endpoint = "http://localhost:8080/axis/ThreadTest.jws";

        if (args == null || args.length < 1) {
            usage();
            return;
        }

        String method = args[0];
        String retStr = null;
        Object [] objs = null;

        Service  service = new Service();
        Call     call    = (Call) service.createCall();

        if (method.equals("start") || method.equals("stop")) {
            if (args.length != 2) {
                usage();
                return;
            }
            call.addParameter (method, XMLType.XSD_STRING, ParameterMode.IN 
);
            objs = new Object [] { args[1] };
        } else if (method.equals ("show")) {
            objs = new Object [] {};
        } else {
            usage();
            return;
        }

        call.setTargetEndpointAddress( new java.net.URL(endpoint) );
        call.setOperationName( method );
        call.setReturnType( XMLType.XSD_STRING );

        retStr = (String) call.invoke(objs);
        System.out.println("Result: " + retStr);
    }
}


This is what I see in catalina.out

  Inside thread 7 Thread[hugh,5,Test Group]
  Jul 20, 2004 9:42:09 PM org.apache.catalina.core.StandardHostDeployer 
start
  INFO: standardHost.start /axis
  Inside thread 8 Thread[hugh,5,Test Group]
  Inside thread 9 Thread[hugh,5,Test Group]

Any help would be greatly appreciated.
Thanks

_________________________________________________________________
MSN Toolbar provides one-click access to Hotmail from any Web page – FREE 
download! http://toolbar.msn.click-url.com/go/onm00200413ave/direct/01/