I'm new to the Blueprint service and I looked for any sample to satisfy the requirement of waiting the service being up in a reference-listener context.
I have a core bundle which provide some kernel services (db, network etc.), and I wrap it with the interface like the following (blueprint.xml):
<bean id="ServiceImplBean" class="com.mysite.service.impl.ServiceImpl" factory-Method="getInstance"/>
<service id="MyServiceInterface" ref="ServiceImplBean" interface="com.mysite.service.IService"></service>
In another bundle I'm using the reference listener to listen for the service being up and running, blueprint like following:
<bean id="ServiceHolderBean" class="com.mysite.consumer.ServiceHolder" factory-method="getInstance" />
<reference id="ServiceHolderBeanReference" interface="com.mysite.service.IService">
<reference-listener ref="ServiceHolderBean" bind-method="bind" unbind-method="unbind" />
</reference>
And here's the class of ServiceHolder piece which take the IService into its field and hold for further usage:
public class ServiceHolder {
private static ServiceHolder instance = new ServiceHolder();
private IService service;
private ServiceHolder() {
}
public static ServiceHolder getInstance() {
return instance;
}
public void bind(IService service) {
this.service = service;
}
public void unbind(IService service) {
this.service = null;
}
public IService getService() {
return this.service;
}
}
The intention is that in the consumer bundle I can easily use the ServiceHolder.getInstance().getService()
to utilize that service.
The problem is sometimes the service is required at initial phase of the bundle start or just before the service is registered. Obviously I'll get a null when call the getService().
What would be the best practice to use blueprint to fulfill the service need when bundle start? I think I can use something like while (service == null) { Thread.sleep(5000)'};
but it looks pretty bad. Any better suggestions?