How do I register a service in OSGi (Apache Felix)?

﹥>﹥吖頭↗ 提交于 2019-12-06 10:31:34

In the most basic form, services are registered in Java code, not using manifest or any other file. This usually happens in your BundleActivator.

Long      i     = new Long(20);    // the service implementation
Hashtable props = new Hashtable();
props.put("description", "This an long value");
bundleContext.registerService(Long.class.getName(), i, props);

I suggest you read a tutorial, like the one at Knopflerfish

An alternative is using Declarative Services, or the new Blueprint facility. Using either of these (or other non-standardized systems) you will declare your services in a (usually XML) file, instead of writing code to interact with the services registry.

But you should probably figure out the fundamentals manually first.

[OsgiUsage] -- makes use of one or more bundle. knows [OsgiInterface] and [OsgiModuleA]

It should not be necessary for the bundle that uses a service to know about the bundle that provides it. Both of them just need to know the service interface. In fact, bundles should not need to know about other bundles at all. They only need to import packages, and consume or provide services.

I understand that you have SomeInterface in another bundle, right? Then you must also export that package in that bundle's manifest, eg.

Export-Bundle: interfaces

But you really should have a look at the bnd tool mentioned in another answer. This generates standard OSGi manifests.

Peter Lawrey

I suggest you look at the iPOJO project. This make using Felix much easier.
https://felix.apache.org/documentation/subprojects/apache-felix-ipojo.html

I would say use bnd directly or maven-bundle-plugin to create OSGI enabled jars.

It's easier than writing the OSGI manifest yourself(typos, mistakes, missing imports/exports)

Trying wrapping the jars with bnd as a start.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!