Diff btw ServiceTracker, ServiceReference & ServiceReferences

此生再无相见时 提交于 2019-12-24 19:51:29

问题


I am new to OSGi and have been studying it for a while.

I want to know the key differences in usage and the benefits for ServiceTracker, ServiceReference & ServiceReferences.

I used an example with both ServiceTracker & ServiceReference and if the service isn't available then both return NULL. However if the service is available then both work just fine, but I couldn't observe what is the difference between two.

Somewhere I read ServiceTracker is a programmatic way to acquire a reference to a service. i.e. write ServiceTracker code that "tracks" a reference to another service and let's you use it when it becomes available. but in actual practice I couldn't see it how its done!

Can anyone plz explain it? Thank you.


回答1:


I recommend that you use neither!

ServiceReference simply represents an entry in the OSGi service registry. You probably mean that you are accessing services using API calls like BundleContext.getServiceReference(). This is the lowest possible level of working with OSGi services, and as you have probably noticed it is quite complicated and it couples your code to the OSGi APIs.

ServiceTracker is very slightly higher level, it wraps around the lower level calls. The key advantage is that it notifies you when a service goes away, which is very important because services are dynamic. However ServiceTracker is still very low-level, and it still couples your code to OSGi APIs.

I strongly recommend avoiding both the lowest level API (based around ServiceReference) AND the ServiceTracker until you have gained a lot more experience with OSGi.

As an alternative I recommend learning Declarative Services. This gives you a programming model based on dependency injection and it decouples you from OSGi APIs. It is much much easier and safer! You can start by following the tutorial here: http://bndtools.org/tutorial.html




回答2:


I agree with what Neil said but I need to extend upon it to answer your comment: If you want to build your project with declarative services using Maven you have to use the maven-bundle-plugin and the maven-scr-plugin. The former uses bnd under the hood while the latter processes your DS annotations. Example:

        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <version>2.3.7</version>
            <extensions>true</extensions>
            <configuration>
                <instructions>
                    <Bundle-Name>${project.name}</Bundle-Name>
                    <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
                    <Export-Package>${project.artifactId}.api</Export-Package>
                </instructions>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-scr-plugin</artifactId>
            <version>1.9.0</version>
            <executions>
                <execution>
                    <id>generate-scr-scrdescriptor</id>
                    <goals>
                        <goal>scr</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

In this example (which is in the build section) I direct the maven-bundle-plugin to export all the packages in my ${project.artifactId}.api directory (this is a convention in my OSGi projects) and the maven-scr-plugin generates xml cofiguration based on my Apache Felix declarative services annotations.

I suggest you should read about Apache Felix declarative services annotations and how they work because it will help you much in the long run.

I have an example OSGi application you can take a look at: here



来源:https://stackoverflow.com/questions/20347288/diff-btw-servicetracker-servicereference-servicereferences

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!