OSGi 组件和服务之间有什么区别

发布于 2024-12-27 11:22:55 字数 94 浏览 3 评论 0 原文

在 OSGi 下,组件与服务之间的主要区别是什么? 据我了解,所有服务都必须是组件,但并非所有组件都必须是服务。

在示例用例中使用其中一种比另一种有什么好处?

Under OSGi, what are the main differences between Components vs Services?
As I understand it, all Services must be Components, but not all Components must be services.

What are the benefits of using one over the other with sample use cases?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

懒的傷心 2025-01-03 11:22:55

“组件”的定义不如服务那么正式。

服务是在 OSGi 服务注册表中注册的任何对象,并且可以使用其接口名称进行查找。唯一的先决条件是服务应该实现某个接口……任何接口。例如,我可以在 java.lang.Runnable 接口下注册一个可运行对象,客户端可以使用该接口名称查找它。

“组件”往往是其生命周期受管理的对象,通常由声明性服务 (DS)、Blueprint 或 iPOJO 等组件框架进行管理。请参阅 OSGi 社区 Wiki 上的此页面,了解可用的不同组件框架的讨论。

组件可以组合或单独具有以下任何功能:

  • 组件可以启动停止;这将被视为“主动”组成部分,尽管这也是一个非正式术语。不需要启动或停止的组件称为被动组件。
  • 组件可以将自身作为 OSGi 服务发布
  • 组件可以绑定使用 OSGi 服务。

一般来说,使用组件框架是使用 OSGi 服务的最简单方法,因为该框架将管理与您想要使用的服务的绑定。例如,您可以说您的组件“依赖于”特定服务,在这种情况下,该组件仅在该服务可用时才会被创建和激活,并且当该服务不可用时它将被销毁。

"Components" are less formally defined than services.

A service is any object that is registered in the OSGi Service Registry and can be looked up using its interface name(s). The only prerequisite is that a service should implement some interface... any interface. For example I could register a runnable object under the java.lang.Runnable interface, and clients could look it up using that interface name.

A "component" tends to be an object whose lifecycle is managed, usually by a component framework such as Declarative Services (DS), Blueprint or iPOJO. See this page on the OSGi Community Wiki for a discussion of the different component frameworks available.

A component may have any of the following features, in combination or alone:

  • A component may be started and stopped; this would be considered an "active" component, though that is also an informal term. A component that doesn't need to be started or stopped is called passive.
  • A component may publish itself as an OSGi service.
  • A component may bind to or consume OSGi services.

In general, using a component framework is the easiest way to work with OSGi services because the framework will manage the binding to the services that you want to consume. For example you could say that your component "depends on" a particular service, in which case the component will only be created and activated when that service is available -- and also it will be destroyed when the service becomes unavailable.

王权女流氓 2025-01-03 11:22:55

编辑:请参阅 Neil Bartlett 的回答,我已经非常具体地回答了声明性服务,但它比我在这里错误地表述的更微妙。

简而言之:组件是服务的消费者而不是提供者,服务是一种扩展的组件注册为服务提供商。

组件具有生命周期(取消/激活和修改)、服务依赖管理(取消/绑定)。

服务是组件的扩展;通过服务注册表,它通过发布实现的接口和属性向其他捆绑包等提供服务。

服务的用例很明显,使用组件的典型案例是需要管理生命周期、配置或服务依赖关系,但不需要/不想发布组件以供捆绑包间使用。

EDIT: See Neil Bartlett's answer, I've answered quite specifically wrt Declarative Services but it's more subtle than I've stated incorrectly here.

In short: Components are consumers of services not providers, Services are an extension of Components registring as service providers.

A component has a lifecycle (de/activate and modify), service dependency management (un/bind).

A service is an extension of a component; via the service registry, it offers it's services to other bundles etc by publishing implemented interfaces and properties.

The use-case for Services is obvious, the typical cases for using Components is needing management of lifecycle, configuration or service dependencies, but not needing/wanting to publish the Component for inter-bundle usage.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文