13

I just recently stumbled upon the fact that Declarative Services in OSGi can set the configuration of a component to required so that the component receives it upon activation, removing the gap between component activation and configuration. I also realized with this that you can receive configuration updates via the modified-method.

It seems to me like this functionality is quite similar to that provided by implementing the ManagedService interface and publishing that as one of the "services" you provide.

It seems like I could completely ignore ManagedService & just use the DS configuration injection.

Is one of these techniques preferred over the other or are there other trade-offs that I'm not seeing?

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
oconnor0
  • 3,154
  • 6
  • 34
  • 52

2 Answers2

14

Yes, you can completely ignore ManagedService and ManagedServiceFactory and just use Declarative Services components. And yes I would recommend this approach.

Just think of this as different levels of abstraction. MS/MSF is the low-level API for config admin, and it is available even when you don't have a DS bundle running. The advantage of this is you can write configurable services without having a dependency on DS, which may be desirable for certain "system level" components.

However, if you are happy to depend on DS, e.g. for "application level" components, then using DS's built-in integration with config admin will make your life a lot easier.

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
Neil Bartlett
  • 23,743
  • 4
  • 44
  • 77
  • Ok, this is what I was leaning toward doing, but I noticed/was a little worried about when a DS component can't be created because its configuration is invalid & throws an exception because Equinox keeps trying to instantiate it & lots of exceptions get thrown & logged. It didn't seem clean. – oconnor0 Nov 09 '10 at 05:11
  • 1
    Equinox should not keep trying to instantiate the component if its activate method throws an exception. It should only try again to recreate the component if the configuration changes. As for logging -- well, of course the exception thrown by the component should be logged! – Neil Bartlett Nov 09 '10 at 08:17
  • 1
    What's the right way to coax DS to treat a component as a `ManagedServiceFactory`, as opposed to as a `ManagedService`? In other words, if one wants DS to create a new instance of a component for *each* applicable configuration instance, what's the trick? Does it require writing a Metatype definition? – seh Jul 31 '11 at 15:13
  • Ah, Neil addresses this question indirectly here: http://njbartlett.name/2010/07/19/factory-components-in-ds.html It looks like the `component@name` attribute declares a PID that can be a target factory PID, though it's still not clear how DS knows what to do here simply by virtue of the `component@name` attribute being present. – seh Jul 31 '11 at 15:27
  • Got it: Section 112.1.3 of the Compendium r4 specification says to specify the `service@servicefactory` attribute as "true," in concert with a *delayed component* definition. This is different, though, from a *factory component*, which uses the `factory` attribute to name a `ComponentFactory` service. – seh Jul 31 '11 at 15:34
  • 3
    @seh: a component can be used as either a singleton (i.e. MS) or a factory (i.e. MSF) by adding "configurationPolicy: require". If you create a factory config with a Factory PID the same as the component.name, then you get a factory. If you create a singleton config with a PID the same as the component.name, then you get a singleton component. – Neil Bartlett Aug 01 '11 at 12:36
  • 3
    @seh: Note that DS never actually creates a ManagedService or ManagedServiceFactory for your component. It works by listening to Config Admin with a ConfigurationListener. However the internal details are unimportant... simply create configs with PID/factoryPID matching the component.name and it "just works". – Neil Bartlett Aug 01 '11 at 12:37
0

couple of examples of building declarative services factories:

1) via config admin : carrot-osgi-scr-factory-cm

2) via component factory : carrot-osgi-scr-factory-ds

take a look on tests for use cases;

Andrei Pozolotin
  • 897
  • 3
  • 14
  • 21