On Mon, May 24, 2010 at 5:57 PM, Rickard Öberg <[email protected]> wrote:
> Since what I want is to know whether the external service is available or
> not I'd propose to change this to:
> public boolean isActive()
> {
> getInstance();
> return serviceInstance.isActive();
> }
> ---
> Which ensures that the importer has tried to connect to the external service
> provider, has returned a service proxy, and now I can use the service
> instance to determine if the backing service is there or not.
Well, that can work but isn't clean.
Ideally, you should have the importer register a ServiceTracker to the
BundleContext, and it will be notified when services are
added/modified/removed accordingly. That can be done for instance by
having a ServiceTracker implementation acting the service implementing
proxy, and perhaps block for a while before failing calls during
service upgrades.
For instance;
public class SomeTracker extends ServiceTracker implements Some
{
private Some service;
public void serviceAdded( ServiceReference ref )
{
service = (Some) ref.get(); // can't recall the exact method
names here...
}
public void serviceRemoved( ServiceReference ref )
{
service = null; // Add synch
}
public SomeRetVal someMethod( SomeType value )
{
if( service == null )
waitWithTimeOutThenRecoverableError();
return service.someMethod( value );
}
}
And this is very well suited for both Qi4j and java.lang.reflect.Proxy
to avoid redoing the same code over and over again.
Cheers
--
Niclas Hedhman, Software Developer
http://www.qi4j.org - New Energy for Java
I live here; http://tinyurl.com/2qq9er
I work here; http://tinyurl.com/2ymelc
I relax here; http://tinyurl.com/2cgsug
_______________________________________________
qi4j-dev mailing list
[email protected]
http://lists.ops4j.org/mailman/listinfo/qi4j-dev