You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@karaf.apache.org by maaruks <ma...@gmail.com> on 2012/05/16 13:19:33 UTC

service reference is null after service reinstall

I have got service provider in one bundle and consumer in other.    
If I reinstall (stop, uninstall, install and start) service provider then
consumer can't get reference to the service anymore.

Here is service consumer code:




It keeps printing "ServiceReference is null"  although provider has
registered a new service  and it can get service reference. 
ctx.getServiceReference(MarketStatusStreamService.class.getName() is not
null.

Service provider code:


Am I doing something wrong ?

--
View this message in context: http://karaf.922171.n3.nabble.com/service-reference-is-null-after-service-reinstall-tp3996874.html
Sent from the Karaf - User mailing list archive at Nabble.com.

Re: service reference is null after service reinstall

Posted by Freeman Fang <fr...@gmail.com>.
On 2012-5-17, at 下午10:09, maaruks wrote:

> no,  is just a local variable.      means it can't be changed after  
> it has
> got a value
IMO final prevent ref to get a new value, so when you uninstall the  
service provider bundle, the ServiceReference is gone(means ref become  
null) and have no chance to assign a new value to ref.

In your code, the ref do need change when you uninstall/install  
service provider bundle as the ServiceReference actually changed, so  
it just couldn't be final.

>
>
> I am not sure why my code didn't work.    "OSGi in action" book has  
> similar
> code and claims it should work.
>
> --
> View this message in context: http://karaf.922171.n3.nabble.com/service-reference-is-null-after-service-reinstall-tp3996874p3999113.html
> Sent from the Karaf - User mailing list archive at Nabble.com.

---------------------------------------------
Freeman Fang

FuseSource
Email:ffang@fusesource.com
Web: fusesource.com
Twitter: freemanfang
Blog: http://freemanfang.blogspot.com
http://blog.sina.com.cn/u/1473905042
weibo: http://weibo.com/u/1473905042











Re: service reference is null after service reinstall

Posted by maaruks <ma...@gmail.com>.
no,  is just a local variable.      means it can't be changed after it has
got a value


I am not sure why my code didn't work.    "OSGi in action" book has similar
code and claims it should work.

--
View this message in context: http://karaf.922171.n3.nabble.com/service-reference-is-null-after-service-reinstall-tp3996874p3999113.html
Sent from the Karaf - User mailing list archive at Nabble.com.

Re: service reference is null after service reinstall

Posted by Freeman Fang <fr...@gmail.com>.
Hi,

I think it's final keyword
final ServiceReference ref =  
ctx.getServiceReference(MarketStatusStreamService.class.getName());
which prevent to assign a new value to the ServiceReference ref.
Try remove the final, and don't use final for variable if you wanna  
the variable changed during runtime.

Freeman
On 2012-5-16, at 下午7:19, maaruks wrote:

> I have got service provider in one bundle and consumer in other.
> If I reinstall (stop, uninstall, install and start) service provider  
> then
> consumer can't get reference to the service anymore.
>
> Here is service consumer code:
>
>
>
>
> It keeps printing "ServiceReference is null"  although provider has
> registered a new service  and it can get service reference.
> ctx.getServiceReference(MarketStatusStreamService.class.getName() is  
> not
> null.
>
> Service provider code:
>
>
> Am I doing something wrong ?
>
> --
> View this message in context: http://karaf.922171.n3.nabble.com/service-reference-is-null-after-service-reinstall-tp3996874.html
> Sent from the Karaf - User mailing list archive at Nabble.com.

---------------------------------------------
Freeman Fang

FuseSource
Email:ffang@fusesource.com
Web: fusesource.com
Twitter: freemanfang
Blog: http://freemanfang.blogspot.com
http://blog.sina.com.cn/u/1473905042
weibo: http://weibo.com/u/1473905042











Re: service reference is null after service reinstall

Posted by sankooc <sa...@yahoo.com.cn>.
I think it's maybe your consumer bundle block the bundleActive thread
here my code and it worked

//common bundle

public interface IService {

}

//consumer bundle
public class Activator implements BundleActivator {
	
	public void start(final BundleContext bundleContext) throws Exception {
		
		new Thread(new Runnable() { //do not block active thread 
			@Override
			public void run() {
				while (true) {
					final ServiceReference<IService> ref =
bundleContext.getServiceReference(IService.class);
					if (ref == null) {
						System.err.println("refer is null");
					} else {
						System.out.println("get service");
						break;
					}

				}
			}

		}).start();
	}
	public void stop(BundleContext bundleContext) throws Exception {
	}

}

//provider bundle
public class Activator implements BundleActivator {

	private static BundleContext context;

	static BundleContext getContext() {
		return context;
	}
	
	public void start(BundleContext bundleContext) throws Exception {
		Activator.context = bundleContext;
		bundleContext.registerService(IService.class.getName(), new IService() {
		}, null);
	}
	
	public void stop(BundleContext bundleContext) throws Exception {
		Activator.context = null;
	}

}

--
View this message in context: http://karaf.922171.n3.nabble.com/service-reference-is-null-after-service-reinstall-tp3996874p4008020.html
Sent from the Karaf - User mailing list archive at Nabble.com.

Re: service reference is null after service reinstall

Posted by maaruks <ma...@gmail.com>.
replaced my code with service tracker   and it works now   




--
View this message in context: http://karaf.922171.n3.nabble.com/service-reference-is-null-after-service-reinstall-tp3996874p3997139.html
Sent from the Karaf - User mailing list archive at Nabble.com.

Re: service reference is null after service reinstall

Posted by maaruks <ma...@gmail.com>.
Service interface is in service bundle only.     Package is exported by
service bundle   and  imported in consumer bundle.

BTW    If I simply stop and start service bundle then consumer can get
service reference without problems.

--
View this message in context: http://karaf.922171.n3.nabble.com/service-reference-is-null-after-service-reinstall-tp3996874p3996968.html
Sent from the Karaf - User mailing list archive at Nabble.com.

Re: service reference is null after service reinstall

Posted by Christian Schneider <ch...@die-schneider.net>.
Where do you have the service interface?
In the service bundle, external or in both bundles?

Christian

Am 16.05.2012 13:19, schrieb maaruks:
> I have got service provider in one bundle and consumer in other.
> If I reinstall (stop, uninstall, install and start) service provider then
> consumer can't get reference to the service anymore.
>
> Here is service consumer code:
>
>
>
>
> It keeps printing "ServiceReference is null"  although provider has
> registered a new service  and it can get service reference.
> ctx.getServiceReference(MarketStatusStreamService.class.getName() is not
> null.
>
> Service provider code:
>
>
> Am I doing something wrong ?
>
> --
> View this message in context: http://karaf.922171.n3.nabble.com/service-reference-is-null-after-service-reinstall-tp3996874.html
> Sent from the Karaf - User mailing list archive at Nabble.com.


-- 
Christian Schneider
http://www.liquid-reality.de

Open Source Architect
Talend Application Integration Division http://www.talend.com