何时使用 ServiceTracker 与 ServiceReference
我刚刚开始 OSGi 编程,遇到了两种监听正在激活的服务的方法。
第一种方法来自 EclipseRCP 书籍,使用 ServiceReference:
String filter="(objectclass="+IModelCreator.class.getName()+")";
context.addServiceListener(this, filter);
modelCreators = Collections.synchronizedMap(
new HashMap<ModelID, List<IModelCreator>>());
ServiceReference references[] = context.getServiceReferences(null, filter);
if(references==null) return;
for(int i=0;i<references.length;++i) {
this.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED,
references[i]));
}
第二种方法来自互联网示例,使用 ServiceTracker:
ServiceTracker logReaderTracker = new ServiceTracker(context,
org.osgi.service.log.LogReaderService.class.getName(), null);
logReaderTracker.open();
Object[] readers = logReaderTracker.getServices();
if (readers != null) {
for (int i = 0; i < readers.length; i++) {
LogReaderService lrs = (LogReaderService) readers[i];
m_readers.add(lrs);
lrs.addLogListener(m_logger);
}
}
logReaderTracker.close();
其中哪一种是保存实现给定接口的所有服务的寄存器的正确和/或最佳方法?还有另一种方法可以实现此目的吗?为什么似乎有两种方法可以做同样的事情?
I am just starting with OSGi programming and have come across two ways to listener for services being activated.
The first way, from an EclipseRCP book, uses ServiceReference:
String filter="(objectclass="+IModelCreator.class.getName()+")";
context.addServiceListener(this, filter);
modelCreators = Collections.synchronizedMap(
new HashMap<ModelID, List<IModelCreator>>());
ServiceReference references[] = context.getServiceReferences(null, filter);
if(references==null) return;
for(int i=0;i<references.length;++i) {
this.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED,
references[i]));
}
The second, from internet examples, uses ServiceTracker:
ServiceTracker logReaderTracker = new ServiceTracker(context,
org.osgi.service.log.LogReaderService.class.getName(), null);
logReaderTracker.open();
Object[] readers = logReaderTracker.getServices();
if (readers != null) {
for (int i = 0; i < readers.length; i++) {
LogReaderService lrs = (LogReaderService) readers[i];
m_readers.add(lrs);
lrs.addLogListener(m_logger);
}
}
logReaderTracker.close();
Which one of these is the correct and/or best way of holding a register of all the services that implement a given Interface? Is there another way of accomplishing this? Why does there seem to be two ways of doing the same thing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您已经可以从包名称
org.osgi.util.tracker.ServiceTracker
派生的那样,ServiceTracker
是一个实用程序类,它(在某些情况下)在编程中总是有多种做事的方法。您可以自己管理您的 ServiceReferences,或者如果它适合您或您的问题,请使用具有其用例的捆绑实用程序类。
另请检查访问服务的最佳实践
其他一些来源表明大多数时候它使用
ServiceTracker
是明智的Eclipse 扩展和 OSGi 服务的比较
OSGi 服务Tracker
OSGi 入门:使用服务
As you already can derive form the package name
org.osgi.util.tracker.ServiceTracker
isServiceTracker
a utility class which (in some cases)In programming there are always several ways of doing things. You either can manage your ServiceReferences by yourself or if it suits you or your problem use the bundled utility class which has its use cases.
Also check this Best practices for accessing services
Some other sources which state that it most of time it is wise to use
ServiceTracker
A Comparison of Eclipse Extensions and OSGi Services
OSGi Service Tracker
Getting Started with OSGi: Consuming a Service