DZone Forums
Go Back   DZone Forums > Community > Languages & Frameworks > Java
Reload this Page Trouble Stopping a threaded OSGi bundle
Notices
Reply
 
LinkBack Thread Tools Display Modes
  (#1 (permalink)) Old
Member
 
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: Nov 2008
Default Trouble Stopping a threaded OSGi bundle - 11-07-2008, 03:14 PM

Hi all,

I am using Equinox to run my OSGi bundle. The bundle runs a thread for indefinite time. At certain point of time, I may need to stop the bundle. The problem I face is that, i am not able to stop the bundle!! When I issue the stop command on the OSGi console.. I get the following error:
Code:
org.osgi.framework.BundleException: State change in progress for bundle file:I:\OSGiRuntime\Equinox\custom\ThreadBundle.jar" by t
hread "main".
at org.eclipse.osgi.framework.internal.core.AbstractBundle.beginStateChange(AbstractBundle.java:1144)
at org.eclipse.osgi.framework.internal.core.AbstractBundle.stop(AbstractBundle.java:428)
at org.eclipse.osgi.framework.internal.core.AbstractBundle.stop(AbstractBundle.java:422)
at org.eclipse.osgi.framework.internal.core.FrameworkCommandProvider._stop(FrameworkCommandProvider.java:285)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.osgi.framework.internal.core.FrameworkCommandInterpreter.execute(FrameworkCommandInterpreter.java:150)
at org.eclipse.osgi.framework.internal.core.FrameworkConsole.docommand(FrameworkConsole.java:302)
at org.eclipse.osgi.framework.internal.core.FrameworkConsole.console(FrameworkConsole.java:287)
at org.eclipse.osgi.framework.internal.core.FrameworkConsole.run(FrameworkConsole.java:223)
at java.lang.Thread.run(Unknown Source)
Caused by: org.eclipse.osgi.framework.internal.core.AbstractBundle$BundleStatusException
... 13 more
Here is my bundle's code:

Code:
package threadbundle;


import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class Activator implements BundleActivator {

	private Thread myThread;
	private CustomThread cTObject;
	
	public void start(BundleContext context) throws Exception {
		System.out.println("Starting ThreadBundle...");
		cTObject = new CustomThread();
		myThread = new Thread(cTObject);
		myThread.run();
	}
	
	public void stop(BundleContext context) throws Exception {
		if(myThread.isAlive())
			cTObject.stopTheThread();
		System.out.println("Stopping thread bundle..");
	}
	
	private class CustomThread implements Runnable {

		boolean stopper;
		
		CustomThread() {
			stopper = false;
		}
		
		public void stopTheThread(){
			if(!stopper)
				stopper = true;
		}
		
		public void run() {
			try {
				while(!stopper) {
					System.out.println("Message from thread...");
					Thread.sleep(5000);
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
		}
	}

}
Anybody know how to solve this?
Thanks in advance,
Abdel Olakara.
Techno Paper
Reply With Quote
  (#2 (permalink)) Old
Member
 
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: Nov 2008
Default 11-07-2008, 10:50 PM

Shouldn't that be myThread.start() not myThread.run() ?

myThread.run() means you're not actually starting the thread, but running your task in the current thread from the OSGi framework - this is why the framework thinks your bundle is still starting, because it never exits the start method.
Reply With Quote
  (#3 (permalink)) Old
Member
 
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: Nov 2008
Default Still not working! - 11-08-2008, 03:53 AM

Hi,
I tried starting the bundle by calling myThread.start().. i noticed that, the state of the bundle to not change from STARTING to ACTIVE :-(


Quote:
Originally Posted by mcculls View Post
Shouldn't that be myThread.start() not myThread.run() ?

myThread.run() means you're not actually starting the thread, but running your task in the current thread from the OSGi framework - this is why the framework thinks your bundle is still starting, because it never exits the start method.
Reply With Quote
  (#4 (permalink)) Old
Member
 
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: Nov 2008
Default 11-08-2008, 04:44 AM

Quote:
Originally Posted by olakara View Post
Hi,
I tried starting the bundle by calling myThread.start().. i noticed that, the state of the bundle to not change from STARTING to ACTIVE :-(
Sorry, perhaps I wasn't clear enough - in the code you posted you are using the myThread.run() method, which simply executes your runnable code in the current thread (ie. the framework thread calling your bundle start method). This means your bundle state will stay as STARTING, because your bundle start method never returns (you can add a println to show this).

If you change this to be myThread.start() then the JVM will execute your runnable code in a new Java thread separate to the framework thread, and your start method will return as expected (for more background on starting threads, see the javadoc for Thread.start() compared to Thread.run()).

I've just tested this change locally on Equinox and Felix (another OSGi framework) and can start and stop your example bundle without error.

Here's the working code, where I've only changed one line:

Code:
package threadbundle;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class Activator implements BundleActivator {

	private Thread myThread;
	private CustomThread cTObject;
	
	public void start(BundleContext context) throws Exception {
		System.out.println("Starting ThreadBundle...");
		cTObject = new CustomThread();
		myThread = new Thread(cTObject);
		myThread.start(); // !! use start() to fork a new thread !!
	}
	
	public void stop(BundleContext context) throws Exception {
		if(myThread.isAlive())
			cTObject.stopTheThread();
		System.out.println("Stopping thread bundle..");
	}
	
	private class CustomThread implements Runnable {

		boolean stopper;
		
		CustomThread() {
			stopper = false;
		}
		
		public void stopTheThread(){
			if(!stopper)
				stopper = true;
		}
		
		public void run() {
			try {
				while(!stopper) {
					System.out.println("Message from thread...");
					Thread.sleep(5000);
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
		}
	}
}
You might want to check that you're definitely deploying the new version of the code - have you restarted the framework, or just updated the bundle? If you updated it, what commands did you use? BTW, if all else fails you could always clear out the bundle cache and do a fresh deployment with the new code, if you're not sure which version you're running...
Reply With Quote
  (#5 (permalink)) Old
Member
 
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: Nov 2008
Default 11-08-2008, 06:20 AM

hi
thanks, it worked. the problem was the jar didn't get updated. And was able to fix it by the time i got your reply. Thanks for the explanation.

Regards,
Abdel Olakara
Reply With Quote
  (#6 (permalink)) Old
Member
 
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: Nov 2009
Default how to stop threads - 11-10-2009, 10:47 AM

Hi all, I'm facing quite the same problem:
I'm able to correctly stop the bundle, but the thread continues to run also if I call a stopThread() method in my thread class the does the following:

Code:
		Thread ghost = this.myThread;
		this.myThread=null;
		ghost.interrupt();
how may I be sure to stop the thread?

thank you very much!
Reply With Quote
Reply

Tags
java, osgi, osgi bundle, threads

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help needed - Trouble in using Reflection in JRE 1.5 karthik7_7 Java 1 01-27-2009 06:03 AM
Running an RCP application as a simple OSGi bundle bestofmed Eclipse 0 11-28-2008 10:00 AM
How to use URLClassLoader in a OSGi Bundle? raja00cs Eclipse 0 11-19-2008 12:04 PM
conceptual trouble with OSGi AlexandreSH Java 4 05-31-2008 07:12 AM
Access OSGi servicees outside OSGi environment simongdkelly Eclipse 3 02-29-2008 09:43 AM


Copyright 1997-2009, DZone, Inc.
vBulletin Skin developed by: vBStyles.com