如何使用ServiceTracker高效地消费多个服务?

发布于 2024-10-24 23:16:01 字数 400 浏览 3 评论 0原文

我想使用ServiceTracker来使用我们公司发布的服务。 我认为最好只创建一个带有过滤器的 ServiceTracker,然后从中获取服务,而不是为我想要使用的每个服务创建新的 ServiceTracker:

Filter filter = ctx.createFilter("(" + Constants.OBJECTCLASS + "=com.mycomp*)");
tracker = new ServiceTracker(ctx, filter, null);

这种方法的问题是,我需要迭代服务引用tracker 发现检查它们的 objectClass 属性,看看我是否可以将其分配给服务对象,这非常麻烦,并且由于需要强制转换而容易出错。

还有其他想法如何使用更优雅的方式使用多个服务吗?

I would like to use ServiceTracker in order to consume the services published by our company.
Instead of creating new ServiceTracker for each service I want to consume I thought it would be better to create just one with a filter and then get the services from it:

Filter filter = ctx.createFilter("(" + Constants.OBJECTCLASS + "=com.mycomp*)");
tracker = new ServiceTracker(ctx, filter, null);

The problem with this approach is that I then need to iterate over the service references the tracker had found examine their objectClass property and see if I can assign it to the service object which is very cumbersome and error prone due to casting that is required.

Any other ideas how to cunsume multiple services using more elegant way?

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

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

发布评论

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

评论(2

浸婚纱 2024-10-31 23:16:01

我认为这是错误的问题:-) 从这个问题我推断您有一个从您的公司获取服务的方法,并且您希望调用该方法。也就是说,在代码中的某个地方,您需要了解特定类型 com.mycomp.X,也就是说,您对公司的一般服务不感兴趣,您有明确的类型依赖性。在您的问题中,您假设它们需要集中调度,这通常不稳健、容易出错并且是维护热点;每次您有新的公司服务时,您都需要更新调度方法。

一个更好的解决方案似乎是使用声明性服务并使用带注释的 bndtools。在该模型中,每个需要服务的地方:

@Component public class SomeMyCompComponent {
  ...
  @Reference
  void foo( com.mycomp.X x ) { ... }
  ...    
}

在该模型中,您不需要集中维护调度程序,任何类都可以在需要时获取其所需的服务。该模型还可以准确地处理多个依赖项并提供更多好处。

也许我没有正确理解问题,因为我是从您所需的解决方案中推断出问题的。但是,我认为您试图滥用服务跟踪器来执行它不打算执行的任务。

不幸的是,DS 并没有像我们应该做的那样构建到框架中:-(

I think it is the wrong question :-) From the question I infer that you have a method that takes a service from your company and you want that method called. That is, somewhere in your code you need to be informed about a specific type com.mycomp.X, that is, you're not interested in general services from your company, you have a clear type dependency. In your question you assume that they need to be dispatched centrally which is usually not robust, error prone, and a maintenance hotspot; every time you have a new company service you need to update the dispatch method.

A MUCH better solution seems to be to use Declarative services and use bndtools with annotations. In that model, each place where you need service:

@Component public class SomeMyCompComponent {
  ...
  @Reference
  void foo( com.mycomp.X x ) { ... }
  ...    
}

In this model, you do not need to centrally maintain a dispatcher, any class can get the services it needs when they need it. This model also accurately handles multiple dependencies and lots more goodies.

Maybe I do not understand the problem correctly because I inferred the problem from the solution you required. However, I think you try to abuse the Service Tracker for a task it was not intended to do.

Unfortunately, DS is not build into the framework as we should have done :-(

请止步禁区 2024-10-31 23:16:01

您可以子类化 ServiceTracker 并添加方法来提供对您感兴趣的服务类型的直接访问。例如,您可以将服务存储在类型安全的异构容器中[1]。然后,您将能够调用 ServiceTracker 子类上的方法,该方法采用您感兴趣的服务类型,并且可以在类型安全异构容器中轻松查找它们。

[1] 《Effective Java》,第二版,第 29 条。

You could subclass ServiceTracker and add methods to provide direct access to the service types in which you are interested. For example, you could store the services in a typesafe heterogeneous container [1]. Then you would be able to call method on your ServiceTracker subclass which take the type of the service you are interested in and they could be easily looked up in the typesafe heterogeneous container.

[1] Effective Java, 2nd Ed., Item 29.

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