Syntax: how to check if an additional dependency is satisfied?

In blueprint.xml I declare an optional dependency as follows:

 <reference id="RepositoryListener" interface="ru.focusmedia.odp.server.datastore.api.RepositoryListener" availability="optional" /> <bean id="Repository" class="ru.focusmedia.odp.server.datastore.jpa.repository.RepositoryImpl"> <jpa:context property="entityManager" unitname="ODP_Server" /> <tx:transaction method="*" value="Required" /> <property name="repositoryListener" ref="RepositoryListener" /> </bean> 

and in RepositoryImpl , I

 public void setRepositoryListener(RepositoryListener repositoryListener) { logger.info("Repository listener set"); this.repositoryListener = repositoryListener; } 

This method is called by Blueprint, even if there is no RepositoryListener service as expected. The problem is, how can I check later if there is a service?

  if (repositoryListener != null) { repositoryListener.notifyDelete(node); } else { logger.warn("No repository listener set!"); } 

does not work because the RepositoryListener not null , but a Blueprint proxy.

+8
osgi blueprint-osgi
source share
1 answer

There are three options.

  • Usually, if you try to use a (missing) additional service, you will receive a five-minute delay while the project waits for the support service to appear, and then ServiceUnavailableException . Thus, one option is to set a very short circuit timeout and catch a ServiceUnavailableException.
  • Perhaps a cleaner option is to use a reference listener to monitor the service life cycle. This has the disadvantage of having to use additional code.
  • The simplest - and therefore laziest - option is to use an optional list of links instead of an sitelink. If there are no satisfactory services, the list will be empty. Of course, you will have to think about what you want if several satisfying services are present.

To set a shorter timeout, simply add the attribute to your secondary service link:

 <reference id="someReference" interface="org.some.service.ServiceInterface" availability="optional" timeout="100" /> 

To use the link listener, you added xml to your project (for example, a more detailed example and discussion in Chapter 6 of Enterprise OSGi in action ):

 <reference id="someReference" interface="org.some.service.ServiceInterface"> <reference-listener ref="someBean" bind-method="bind" unbind-method="unbind" /> </reference> 

The bind and unbind are called when your service appears and disappears (or when services are added and removed to the link list, if you use the link list).

Using a reference list really doesn't need any sample code - just use the <reference-list element and make sure your setter method accepts the list.

+14
source share

All Articles