English 中文(简体)
When to use ServiceTracker vs ServiceReference
原标题:
  • 时间:2009-11-17 21:27:01
  •  标签:
  • java
  • osgi

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?

最佳回答

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

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签