3

OSGi declarative services are explicitly allowed to have a bind method without a matching unbind method for a reference because "Once the component configuration is deactivated, SCR must discard all references to the component instance and component context associated with the activation."

I'm using the Apache Felix maven-scr-plugin to generate my service component XML from Java5 annotations. If I omit the "unbind" attribute from the @Reference annotation, then I get this failure:

[ERROR] @Reference: Missing method unbind for reference configuration at Java annotations in <classname>:<linenum>

Why is the SCR generator being so strict? Is there a way to tell it to tolerate an omitted unbind method? Perhaps I need to file a defect with Felix?

Of course, it would be easy to just add trivial unbind methods to my services but the spec says they are unneeded.

Chris Dolan
  • 8,905
  • 2
  • 35
  • 73

3 Answers3

4

The text you quote (112.5.13 compendium v4.2) doesn't directly relate to unbinding it refers to deactivation, which in turn necessitates unbinding. Later in the spec (112.5.15) it states "For each reference using the event strategy, the unbind method must be called for each bound service of that reference."

EDIT Ignore above, see comments below. In the current version of Felix SCR "generateAccessors" is enabled by default and you definitely don't need to write bind/unbind methods. IIRC in July 2011 that wasn't the case.

earcam
  • 6,662
  • 4
  • 37
  • 57
  • Interesting, so the "must be called" says that the unbind method is actually required? My understanding was that the service instance was going to be discarded anyway, so it was no big deal if the references were not nulled. But it seems that may have been incorrect. – Chris Dolan Jul 29 '11 at 01:46
  • As the service instance is not proxied the only way to discard it is by nulling it. In the case of uninstalling a bundle, all instances of classes loaded by the bundle's classloader must be nullified to ensure that the entire bundle (classloader, all classes and instances) are garbage collected. – earcam Jul 29 '11 at 07:50
  • 1
    @Chris, was just tinkering with the plugin - there's a configuration option "generateAccessors" - I haven't tried it yet but guessing if you set this to true it will generate the un/bind methods for you. – earcam Aug 08 '11 at 09:56
  • 4
    The DS spec does not require an unbind method. I suspect the tool processing the annotations is too strict. – BJ Hargrave Jan 11 '13 at 14:52
1

If you use the standard DS annotations from the specification, bnd will generate the XML and you don't even need a separate Maven plugin.

By the way, you SHOULD always have an unbind method if the reference policy is dynamic -- even if the cardinality is mandatory, because you may have to handle dynamic rebinds. In this case bnd will raise a warning, rather than an error.

An unbind method is completely unnecessary when the reference policy is static. In this case the component instance MUST be destroyed, so you can do your cleanup in the deactivate method.

Neil Bartlett
  • 23,743
  • 4
  • 44
  • 77
0

Bnd also generates DS XML from annotations and doesn't have this limitation.

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487