felix-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Clement Escoffier <clement.escoff...@gmail.com>
Subject Re: Making sense of service binding and dependencies
Date Wed, 04 Feb 2009 10:16:10 GMT
Hi,

On 04.02.2009, at 05:15, Richard S. Hall wrote:

> Gerald Bortis wrote:
>> Hi all,
>>
>> I've been playing with OSGi and Felix for the past week, and I  
>> think I have
>> a general understanding of how to apply it to my application. So  
>> far, I've
>> refactored my code into bundle-friendly packages and have setup a  
>> build
>> system to generate my bundles. For every bundle, I have the following
>> pattern:
>>
>> FooActivator.java
>> FooService.java
>> FooServiceImpl.java
>>
>
> Looks good.
>
>> FooActivator implements BundleActivator, and in the start method  
>> registers
>> FooService. Pretty straightforward. The issues I've run into are the
>> following:
>>
>> a) What is the best way to call BarService from FooServiceImpl?  
>> I've tried
>> both the ServiceTracker pattern, and the ServiceBinding-Utils, but  
>> both seem
>> a bit convoluted.
>>
>
> At a minimum, you should use the ServiceTracker (you could also  
> register a service listener and do your own service tracking, but  
> this is not recommended). With a ServiceTracker, you could use a  
> ServiceTrackerCustomizer to call you when a service is available,  
> for example.
>
>> b) What is the best way to make sure that FooServiceImpl doesn't call
>> BarService before BarService has been reigstered?
>>
>
> If you use a ServiceTrackerCustomerizer, it will only be called when  
> a BarService is available.
>
>> For the second issue, I thought I was on the right path when I  
>> found the
>> Depedency Manager bundle, but the example isn't clear how  
>> registering a
>> ServiceDepedency on BarService instantiates the instance that  
>> FooServiceImpl
>> requires? This lead me to the iPOJO section, which promises to  
>> address these
>> issues, but I'm not sure if it's what I'm looking for either. Does  
>> using
>> Dependency Manager or iPOJOs also address my first issue of how to  
>> implicity
>> bind the services? Any pointers in the right direction are  
>> appreciated.
>>
>
> DependencyManager and iPOJO definitely can help too. The benefit of  
> these types of dependency injection tools is that they actually  
> manage the life cycle of your component, so it won't even be created  
> until your dependencies are satisfied. Check out my iPOJO  
> presentation under the presentations section of the documentation  
> page on the Felix web site for a simple example at the beginning of  
> the presentation for how you provide and require services in iPOJO,  
> as an example.

Here is a simple example of how to implement it with iPOJO:

@Component
@Provides
public class FooServiceImpl implements FooService {
     @Requires
     private BarService myBar;

     public void doSomething() {
        myBar.doSomethingWithBar();
     }
}

That's simple, isn't it ?  You don't have to worry about BarService  
availability, iPOJO will manage this for you :-)


Clement


>
>
> -> richard
>
>> Regards,
>> Gerald
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
> For additional commands, e-mail: users-help@felix.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Mime
View raw message