cancel
Showing results for 
Search instead for 
Did you mean: 

threads (webservices etc)

0 Kudos

hi,

i'm writing webservices, portal components etc which use a custom-base-class which starts a thread and listens on a given port. so far so good. everything works fine etc etc.

but my problem is, when i re-deploy the application or i stop it within the visual administrator's deploy feature, the webservice or portalcomponent stops successful (cant open it using the portal or the webservice navigator), but the thread is still running and i dont know why. i even abstracted the finalize() function to kill my sub-threads, but that doesnt work either.

any help or suggestions? i just want to have my applications clean, so if i stop them, all subthreads will die... or even better, a function call.. like finalize() would ne the best damn thing for me.

thanks,

constantin

Accepted Solutions (0)

Answers (4)

Answers (4)

0 Kudos

bump

0 Kudos

hi, this is a very simple code, just to demonstrate what i want to do.

this is a simple sap webservice, which i simply deploy and test the webservice using the "web services navigator" (function which is public to the service is "test(String)". so far so good. everything works as expected. but when i stop the web service with the deploy service using the visual administrator. the application and/or thread is still running... printing "zzz ZZZ zzz" in my debug log. even the simple checks in "run()" doesn't work. is there any work around? i COULD implement a function/service in my portal administration app which starts/stops applications using the sap deploy-service using the deploy.jar to start and stop the applications.. but let the application know that they are about to stop before, so they can shutdown their threads, open sockets etc...

package de.wsa.test;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Test {
	private static final Log LOG = LogFactory.getLog(Test.class);

	private TestThread thread;

	public Test() {
		LOG.debug("TEST");
		thread = new TestThread(this);
	}

	public String test(String test) {
		LOG.debug("test");
		return test;
	}

	protected void finalize() throws Throwable {
		LOG.debug("finalize");
		thread.setStop(true);
		super.finalize();
	}

	public static class TestThread extends Thread {
		private static final Log LOG = LogFactory.getLog(TestThread.class);

		private Test test;
		private boolean stop = false;

		public TestThread(Test test) {
			LOG.debug("TestThread");
			this.test = test;
			start();
		}

		public void setStop(boolean stop) {
			LOG.debug("setStop");
			this.stop = stop;
		}

		public void run() {
			LOG.debug("run");

			while (true) {
				if (stop) {
					LOG.warn("stop");
					break;
				}

				if (Thread.currentThread().isInterrupted()) {
					LOG.warn("Thread.currentThread().isInterrupted()");
					break;
				}

				if (isInterrupted()) {
					LOG.warn("isInterrupted()");
					break;
				}

				if (test == null) {
					LOG.warn("test == null");
					break;
				}

				LOG.debug("zzz ZZZ zzz");

				try {
					Thread.sleep(5000);
				} catch (Exception e) {
					//swallow silently
				}
			}
		}
	}
}

Edited by: Constantin Wildförster on Apr 23, 2010 11:46 AM

Former Member
0 Kudos

Hi,

do you have control over this custom class? Such a thread should always implement some kind of interruption policy, that is to check Thread.currentThread().isInterrupted() periodically (the definition of a period is of course custom) and then abort if the interruption flag has been set by the Thread owner. Since your Thread depends on a Socket, the easy (imho dirty way) is to close the Socket the Thread relies on when your service is being destroyed, therefore the Thread will receive a SocketException and end its life. I recommend reading ["Java Concurrency in Practice"|http://www.javaconcurrencyinpractice.com/].

Best regards,

Fabian

0 Kudos

hi fabian,

the problem is that the application (webservice) seems to not get interrupted if the application is stopped using the deploy-service nor if the application gets re-deployed....

Former Member
0 Kudos

Hi Constantin,

You wrote:

"but my problem is, when i re-deploy the application or i stop it within the visual administrator's deploy feature, the webservice or portalcomponent stops successful (cant open it using the portal or the webservice navigator), but the thread is still running and i dont know why."

Please be more precise what you deploy or rather what you are trying to finalize. I read from your post that you rely in your portal and/or webservice on a custom class which starts a thread. Does this class provide lifecycle methods like start/stop for the thread using an interruption policy like I pointed out? Or do you simply start the thread in the default constructor of the class and rely on the instance being finalized in order to stop the thread (which is always not a good idea)? Imho you should decide who controls the lifecycle of the thread, f.e. if the webservice indirectly controls the lifecycle you should start/stop the thread from the services lifecycle methods (f.e. if you use JAX-RPC you may want to look at the ServiceLifecycle interface) or if you want your Portal component to control the lifecycle you should introduce a service with startup=true and then start the thread in the afterInit() method of the service and stop it in the destroy method.

Best regards,

Fabian

Former Member
0 Kudos

Hi,

I have faced this problem. The class which is listening will not be killed when you stop the application or even remove the application from the Visual Administrator. You can stop the listener by restarting the server (which stops the JVM).

This can be avoided by having your own UI controls to start and stop the listener. So, after you deploy the application, go to the URL, start the listener. Similar way, before undeploying the application, go to the URL and stop the listener.

I solved it with this approach.

Thanks,

Venkat

0 Kudos

hi Venkat,

thanks for your reply. i was thinking of implementing such a "deploy/undeploy" feature in my administration-application (maybe i use the sap deploy-service too)

thanks