如何在OSGi(Apache Felix)中注册服务? [英] How do I register a service in OSGi (Apache Felix)?

查看:190
本文介绍了如何在OSGi(Apache Felix)中注册服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已成功设法从代码启动Apache Felix并注册自己的Bundle。

I have successfully managed to start Apache Felix from code and register an own Bundle.

需要OSGI项目之间的关系:

Following relation between OSGI-projects is needed:

[OsgiInterface] - 提供接口。

[OsgiModuleA] - (bundle)提供了这些接口的实现。

知道 [OsgiInterface]

[OsgiModuleA] -- (bundle) provides an implementation of those interfaces.
knows [OsgiInterface]

[OsgiUsage] - 使用一个或多个捆绑包。

知道 [OsgiInterface] [OsgiModuleA]

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

现在我在注册实现接口的服务时遇到了问题。
我猜我在 manifest.mf 文件中的条目是错误的。

Now I have problems registering a service which implements an interface. I would guess that my entries in manifest.mf files are wrong.

如果有人能查看我的 上一个问题

It would be very kind, if someone could look at the code in my previous question

让我参考这个问题:

我试图创建第三个​​项目 OsgiInterfaces ,它提供了一个接口 接口包中的SomeInterface OsgiModuleA OsgiUsage 都知道这个项目。

I tried to create a third project OsgiInterfaces, which provides an interface SomeInterface in the package interfaces. This project is known by both OsgiModuleA and OsgiUsage.

OsgiModuleA: manifest.mf 现在为条目 Import-Package: interfaces $ C>。此外,还有一个 SomeInterface 的实例提供给激活器。

OsgiModuleA: manifest.mf has now an additional value interfaces for the entry Import-Package:. Furthermore, there is an instance of SomeInterface provided to the activator.

当捆绑启动时, NoClassDefFoundError 发生:接口 SomeInterface 未知。

When the bundle is started, an NoClassDefFoundError occurs: the interface SomeInterface is not known.

现在,错误已修复,我可以说,最重要的部分是:

Now, that the error is fixed, I can tell, that the most important part was:

map.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA,
    "my.interfaces; version=1.0.0");

没有这个,我得到 ClassCastException

推荐答案

在最基本的形式中,服务是用Java代码注册的,而不是使用清单或任何其他文件。这通常发生在你的BundleActivator中。

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);

我建议你阅读一个教程,比如 Knopflerfish

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

另一种方法是使用Declarative Services或新的Blueprint工具。使用这些(或其他非标准化系统)中的任何一个,您将在(通常是XML)文件中声明您的服务,而不是编写代码来与服务注册表进行交互。

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] - 使用一个或多个捆绑包。
知道[OsgiInterface]和[OsgiModuleA]

[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.

这篇关于如何在OSGi(Apache Felix)中注册服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆