Skip to content

OSGi current recommended usage patterns and 'how to' s.

Gordon Hutchison edited this page May 5, 2023 · 7 revisions

Mandatory static references

For mandatory static references the preferred way to inject is to use constructor injection:

@Activate
public NettyFrameworkImpl(@Reference ExecutorService executorService) {
    this.executorService = executorService;
}

Slack

Service Components

Use a @Component rather than a .bnd file entry and there is no need to have the service.vendor field

slack

Configurable Components

See

If your component is configurable then the target filter for the reference can be configured with configuration admin. For example if you gave your reference a name @Reference(name="example1") and your component PID is mypid then in the server.xml it could be configured like: <mypid example1.target="(some.property=select.this.one)" /> Slack

Lazy Service Instantiation

Slack The most simple is always to get directly injected with the service object implementation with a simple @Reference. Only after you prove that such a simple reference contributes to a performance issue should you consider using AtomicServiceReference to try to have lazy service instantiation.

Inheritance and References

Keep Java types for references as simple as possible: Slack

Use service events to count the available services to be event based instead of polling.

Slack Code

Extra packages

Slack Liberty does not let Equinox calculate the system packages. Instead it has all these system-packages properties files that list the packages. See dev/com.ibm.ws.kernel.boot/resources/OSGI-OPT/websphere/system-packages_1.6.0.properties If you are having issues related to this you could always set org.osgi.framework.system.packages.extra to the java.* packages in your environment in the bootstrap.properties file.

Clean Start

SlackIssue Setting the osgi spec'ed property org.osgi.framework.storage.clean=onFirstInit in the bootstrap.properties.

Debug Metatype Issues

Slack

OSGI debug options to resolve weird case where the bundle metatype is not found
org.eclipse.osgi/debug=true
org.eclipse.osgi/debug/bundleFile=true
org.eclipse.osgi/debug/cachedmanifest=true

bootstrap.properties is used to have these by setting osgi.debug=<the above>