java 何时使用 ServiceTracker 与 ServiceReference

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1751918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 17:49:33  来源:igfitidea点击:

When to use ServiceTracker vs ServiceReference

javaosgi

提问by KitsuneYMG

I am just starting with OSGi programming and have come across two ways to listener for services being activated.

我刚刚开始使用 OSGi 编程,并且遇到了两种方法来监听被激活的服务。

The first way, from an EclipseRCP book, uses ServiceReference:

第一种方法,来自 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]));
}

The second, from internet examples, uses ServiceTracker:

第二个,来自互联网示例,使用 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?

其中哪一个是保存实现给定接口的所有服务的寄存器的正确和/或最佳方式?有没有另一种方法来实现这一点?为什么似乎有两种方法可以做同样的事情?

回答by jitter

As you already can derive form the package name org.osgi.util.tracker.ServiceTrackeris ServiceTrackera utility class which (in some cases)

正如你已经可以得出形成包名称org.osgi.util.tracker.ServiceTrackerServiceTracker一种实用工具类(在某些情况下)

simplifies using services from the Framework's service registry.

简化了使用来自框架服务注册中心的服务。

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.

在编程中,总是有几种做事的方式。您可以自己管理您的 ServiceReferences,或者如果它适合您或您的问题,请使用具有其用例的捆绑实用程序类。

Also check this Best practices for accessing services

另请检查访问服务的最佳实践

Some other sources which state that it most of time it is wise to use ServiceTracker

其他一些消息来源指出大多数时候使用是明智的 ServiceTracker

A Comparison of Eclipse Extensions and OSGi Services

Eclipse 扩展和 OSGi 服务的比较

OSGi Service Tracker

OSGi 服务跟踪器

Getting Started with OSGi: Consuming a Service

OSGi 入门:使用服务