java - Is there a difference between extending ServiceTracker class and implementing ServiceTrackerCustomizer interface when tracking OSGi services? -
i creating osgi bundles. register services , (and of course use) each other's services.
i decided use servicetracker
instead of declarative services.
as searching information found 2 approach of tracking services.
the first 1 creating own tracker class each service, extends servicetracker
class , overrides methods need overridden. in activator class creating new instance of tracker class giving bundle context , open tracking.
the other approach creating tracker class each service, implements servicetrackercustomizer
interface , overrides methods need overridden. in activator class creating new instance of servicetracker
class giving bundle context, name of service needs tracked and new instance of our customizer class. open tracking.
are there differences between 2 approaches? no. in servicetracker javadoc can see servicetracker
class implements servicetrackercustomizer
interface.
could please tell me pros , cons on both approaches? in advance.
here reasons:
sub-class servicetracker if
- you want hard-code constructor parameters of super class. e.g.: hardcode type or filter. in case not nice if of users should know filter tracker should instantiated with
- you want override (wrap) open, close or other functions of servicetracker
- you want extend functionality of servicetracker. e.g.: having implementation pre-filters service objects based on java generics equality
implement interface if:
- you expect switch other servicetracker implementation in future. servicetracker add-on osgi core, part of core spec since 5.0.0. other, more effective implementations can created in future
- you not want decide constructors want override parameters flexible view point of business logic. there more constructors of standard servicetracker class in future. if sub-class it, class not support constructors
- you want sub-class class in implementation of servicetrackercustomizer
do not use servicetracker directly if
- declarative services or other component model helps writing cleaner , more stable code
Comments
Post a Comment