何时使用 ServiceTracker 与 ServiceReference

发布于 2024-08-11 17:25:46 字数 1169 浏览 2 评论 0原文

我刚刚开始 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 技术交流群。

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

发布评论

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

评论(1

甜`诱少女 2024-08-18 17:25:46

正如您已经可以从包名称 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 is ServiceTracker a utility class which (in some cases)

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.

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

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