Osgi getService()

发布于 2024-10-04 16:47:29 字数 196 浏览 0 评论 0原文

在我们的项目(Osgi、Spring-Dm、Apache-Felix)中,当 BundleContext.getService() 返回 null 时,我们在生产中遇到了一些问题。 这种情况很少发生,而且一开始也不会发生,因此这不是配置错误的问题。

另一种方法是使用 ServiceTracker 来定位服务,但由于错误非常罕见,我不确定更改它是否可以解决问题。

In our project (Osgi, Spring-Dm, Apache-Felix) we are having some issue on production when BundleContext.getService() return null.
It happens very seldom, and not at the start, so it's not a misconfiguration problem.

An alternative could be using ServiceTracker to locate the service, but because the error is quite rare I'm not sure if changing it would solve the situation.

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

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

发布评论

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

评论(4

抚笙 2024-10-11 16:47:29

OSGi 规范规定该方法返回

  • 与引用关联的服务的服务对象
  • 或 null 如果
    • 服务未注册
    • ServiceFactory 返回的服务对象没有实现
      它注册的类别
    • ServiceFactory 抛出异常

我的第一个猜测是该服务未注册。

顺便说一句,使用 Spring-DM 应该可以使大多数对服务注册表的编程访问变得不必要。至少在我们的公司中,这些调用通常被禁止,并且仅在较低级别的技术代码中允许。

The OSGi-specification says that the method returns

  • A service object for the service associated with reference
  • or null if
    • the service is not registered
    • the service object returned by a ServiceFactory does not implement the
      classes under which it was registered
    • the ServiceFactory threw an exception

My first guess would be that the service is not registered.

By the way, using Spring-DM should make most programmatic accesses to the service registry unneccessary. At least in our companies these calls have been generally forbidden and are only allowed in lower level technical code.

别挽留 2024-10-11 16:47:29

Uberto,服务尚未注册一点也不奇怪。 OSGi 是动态的。服务来来去去。如果您尝试在服务注册之前访问该服务,那么您将得到 null。

当您执行简单的 getService() 时,您会假设该服务已经存在。如果您需要该服务,则应该执行一些涉及等待注册事件的操作。这可以通过 ServiceTracker 来完成,但级别相当低。正如其他人指出的那样,使用 Spring-DM 或声明性服务之类的东西使得这变得非常简单和强大。

Uberto, it's not at all weird for the service not to be registered yet. OSGi is dynamic. Services come and go. If you attempt to access a service before it has been registered then you will get null.

When you do a plain getService() you are kind of assuming that the service already exists. If you need the service to be there, you should instead do something that involves waiting for the registration event. This can be done with ServiceTracker, but it is rather low level. As others have pointed out, using something like Spring-DM or Declarative Services makes this quite easy and robust.

弱骨蛰伏 2024-10-11 16:47:29

我可以确认,如果您使用 ServiceTracker,可能会发生竞争条件!有时,即使服务已注册,ServiceTracker 也无法捕获服务。 为了解决这个问题,我使用 waitForService() 而不是 getService()。

问候
罗兰

I can confirm that race conditions can occur if you use the ServiceTracker! Sometimes, the ServiceTracker can not catch a Service even though the service is registered. To solve the problem I use waitForService() instead of getService().

Regards
Roland

So尛奶瓶 2024-10-11 16:47:29

如果 ServiceReference 不为 null 并且在 getService(serviceReference) 时获取空指针,则服务实现类未正确初始化。

确保服务实现类的构造函数是公共的,并且对象创建时没有错误。

If the ServiceReference is not null and getting null pointer while getService(serviceReference) then the service implementation class is not properly initialized.

Make sure the constructor of the service implementation class is public and no errors in object creation.

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