Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache Felix SCR @Reference cheatsheet

I'm using Apache Felix declarative services in my application and I'm looking for a cheat sheet which describes the parameters one can give to the @Reference annotation. I saw a webpage once which contained a very informative cheat sheet (or table) about it but I can't seem to find it. I think this would be useful for a lot of Felix users out there. The parameters I'm talking about:

  • cardinality: MANDATORY_UNARY, MANDATORY_MULTIPLE, OPTIONAL_UNARY, OPTIONAL_MULTIPLE
  • strategy: EVENT, LOOKUP
  • policy: DYNAMIC, STATIC
  • policyOption: GREEDY, RELUCTANT

cardinality is rather straightforward but the others are not clear to me. What do they do and how do they work?

like image 437
Adam Arold Avatar asked May 25 '15 17:05

Adam Arold


1 Answers

from official OSGI R5 Compendium Spec page 281ff.

Reference Cardinality

The cardinality for a reference can be specified as one of four choices:

  • 0..1 – Optional and unary.
  • 1..1 – Mandatory and unary (Default) .
  • 0..n – Optional and multiple.
  • 1..n – Mandatory and multiple.

Strategy

  • Event strategy – SCR calls a method on the component instance when a service becomes bound, when a service becomes unbound, or when its properties are updated. These methods are the bind, updated, and unbind methods specified by the reference. The event strategy is useful if the component needs to be notified of changes to the bound services for a dynamic reference.
  • Lookup strategy – A component instance can use one of the locateService methods of ComponentContext to locate a bound service. These methods take the name of the reference as a parameter. If the reference has a dynamic policy, it is important to not store the returned service object(s) but look it up every time it is needed.

Policy

  • The static policy is the most simple policy and is the default policy. A component instance never sees any of the dynamics. Component configurations are deactivated before any bound service for a reference having a static policy becomes unavailable. If a target service is available to replace the bound service which became unavailable, the component configuration must be reactivated and bound to the replacement service.

  • The dynamic policy is slightly more complex since the component implementation must properly handle changes in the set of bound services that can occur on any thread. With the dynamic policy, SCR can change the set of bound services without deactivating a component configuration. If the component uses the event strategy to access services, then the component instance will be notified of changes in the set of bound services by calls to the bind, and unbind methods.

Policy Option

  • reluctant – Minimize rebinding and reactivating.
  • greedy – Maximize the use of the best service by deactivating static references or rebinding dynamic references.

other felix specific resources

[Felic SCR Annotations] (http://felix.apache.org/documentation/subprojects/apache-felix-maven-scr-plugin/scr-annotations.html)

like image 149
Peter Kirschner Avatar answered Sep 25 '22 15:09

Peter Kirschner