I am creating some OSGi bundles. They register services and also get (and of course use) each other's services.
我正在创建一些OSGi包。他们注册服务,并获得(当然也使用)彼此的服务。
I decided to use ServiceTracker
instead of Declarative Services.
我决定使用ServiceTracker而不是Declarative Services。
As I was searching for information about this I found two approach of tracking services.
在我搜索有关这方面的信息时,我找到了两种跟踪服务的方法。
The first one is creating an own tracker class for each service, which extends the ServiceTracker
class and overrides the methods that need to be overridden. Then in the activator class creating a new instance of this tracker class giving the bundle context to it and open it for tracking.
第一个是为每个服务创建一个自己的跟踪器类,它扩展了ServiceTracker类并覆盖了需要重写的方法。然后在激活器类中创建此跟踪器类的新实例,为其提供捆绑上下文并打开它以进行跟踪。
The other approach is creating a tracker class for each service, which implements the ServiceTrackerCustomizer
interface and overrides the methods that need to be overridden. Then in the activator class creating a new instance of the ServiceTracker
class giving to it the bundle context, the name of the service that needs to be tracked and a new instance of our customizer class. Then open it for tracking.
另一种方法是为每个服务创建一个跟踪器类,它实现ServiceTrackerCustomizer接口并覆盖需要重写的方法。然后在激活器类中创建ServiceTracker类的新实例,为其提供bundle上下文,需要跟踪的服务的名称以及我们的定制器类的新实例。然后打开它进行跟踪。
Are there any differences between the two approaches? I would say no. In the ServiceTracker javadoc I can see that the ServiceTracker
class also implements the ServiceTrackerCustomizer
interface.
这两种方法之间有什么不同吗?我会说不。在ServiceTracker javadoc中,我可以看到ServiceTracker类还实现了ServiceTrackerCustomizer接口。
Could you please tell me the pros and cons on both the approaches? Thanks in advance.
你能否告诉我两种方法的利弊?提前致谢。
2 个解决方案
#1
Here are my reasons:
这是我的理由:
Sub-class ServiceTracker if
子类ServiceTracker如果
- You want to hard-code the constructor parameters of the super class. E.g.: Hardcode the type or filter. In this case it would not look nice if all of the users should know what filter the tracker should be instantiated with
- You want to override (wrap) open, close or other functions of ServiceTracker
- You want to extend the functionality of ServiceTracker. E.g.: By having an implementation that pre-filters service objects based on Java Generics equality
您想要对超类的构造函数参数进行硬编码。例如:硬编码类型或过滤器。在这种情况下,如果所有用户都应该知道应该实例化跟踪器的过滤器,那就不好看了
您想要覆盖(换行)ServiceTracker的打开,关闭或其他功能
您想要扩展ServiceTracker的功能。例如:通过具有基于Java Generics相等性预过滤服务对象的实现
Implement the interface if:
如果实现接口:
- You expect to switch to other ServiceTracker implementation in the future. ServiceTracker is an add-on for OSGi core, it is only part of the core spec since 5.0.0. Other, more effective implementations can be created in the future
- You do not want to decide which constructors you want to override as the parameters are flexible from the view point of your business logic. Probably there will be more constructors of the standard ServiceTracker class in the future. If you sub-class it, your class will not support those constructors
- You want to sub-class from another class in the implementation of ServiceTrackerCustomizer
您希望将来切换到其他ServiceTracker实现。 ServiceTracker是OSGi核心的附加组件,它只是5.0.0以来核心规范的一部分。将来可以创建其他更有效的实现
您不希望决定要覆盖哪些构造函数,因为从业务逻辑的角度来看,参数是灵活的。未来可能会有更多标准ServiceTracker类的构造函数。如果对它进行子类化,则您的类将不支持这些构造函数
您希望在ServiceTrackerCustomizer的实现中从另一个类中进行子类化
Do not use ServiceTracker directly if
如果,请不要直接使用ServiceTracker
- Declarative Services or other Component Model helps you writing cleaner and more stable code
声明性服务或其他组件模型可帮助您编写更清晰,更稳定的代码
#2
In over 12 years of developing with OSGi I don't think I have ever written a single ServiceTrackerCustomizer
. IMHO it's just more convenient to directly subclass ServiceTracker
and leave the customizer parameter null
.
在使用OSGi开发的12年中,我认为我从未编写过一个ServiceTrackerCustomizer。恕我直言,直接将ServiceTracker子类化并将定制器参数保留为null更方便。
One simple reason it's easier to subclass is that you don't need to provide an implementation of the modifiedService
method, which is rarely needed.
子类化更容易的一个简单原因是您不需要提供很少需要的modifiedService方法的实现。
Functionally, however, the result will be the same so it's very much a personal preference.
但从功能上来说,结果是一样的,所以这是个人偏好。
#1
Here are my reasons:
这是我的理由:
Sub-class ServiceTracker if
子类ServiceTracker如果
- You want to hard-code the constructor parameters of the super class. E.g.: Hardcode the type or filter. In this case it would not look nice if all of the users should know what filter the tracker should be instantiated with
- You want to override (wrap) open, close or other functions of ServiceTracker
- You want to extend the functionality of ServiceTracker. E.g.: By having an implementation that pre-filters service objects based on Java Generics equality
您想要对超类的构造函数参数进行硬编码。例如:硬编码类型或过滤器。在这种情况下,如果所有用户都应该知道应该实例化跟踪器的过滤器,那就不好看了
您想要覆盖(换行)ServiceTracker的打开,关闭或其他功能
您想要扩展ServiceTracker的功能。例如:通过具有基于Java Generics相等性预过滤服务对象的实现
Implement the interface if:
如果实现接口:
- You expect to switch to other ServiceTracker implementation in the future. ServiceTracker is an add-on for OSGi core, it is only part of the core spec since 5.0.0. Other, more effective implementations can be created in the future
- You do not want to decide which constructors you want to override as the parameters are flexible from the view point of your business logic. Probably there will be more constructors of the standard ServiceTracker class in the future. If you sub-class it, your class will not support those constructors
- You want to sub-class from another class in the implementation of ServiceTrackerCustomizer
您希望将来切换到其他ServiceTracker实现。 ServiceTracker是OSGi核心的附加组件,它只是5.0.0以来核心规范的一部分。将来可以创建其他更有效的实现
您不希望决定要覆盖哪些构造函数,因为从业务逻辑的角度来看,参数是灵活的。未来可能会有更多标准ServiceTracker类的构造函数。如果对它进行子类化,则您的类将不支持这些构造函数
您希望在ServiceTrackerCustomizer的实现中从另一个类中进行子类化
Do not use ServiceTracker directly if
如果,请不要直接使用ServiceTracker
- Declarative Services or other Component Model helps you writing cleaner and more stable code
声明性服务或其他组件模型可帮助您编写更清晰,更稳定的代码
#2
In over 12 years of developing with OSGi I don't think I have ever written a single ServiceTrackerCustomizer
. IMHO it's just more convenient to directly subclass ServiceTracker
and leave the customizer parameter null
.
在使用OSGi开发的12年中,我认为我从未编写过一个ServiceTrackerCustomizer。恕我直言,直接将ServiceTracker子类化并将定制器参数保留为null更方便。
One simple reason it's easier to subclass is that you don't need to provide an implementation of the modifiedService
method, which is rarely needed.
子类化更容易的一个简单原因是您不需要提供很少需要的modifiedService方法的实现。
Functionally, however, the result will be the same so it's very much a personal preference.
但从功能上来说,结果是一样的,所以这是个人偏好。