Skip to content

osgi_Open_Service_Gateway_Initiative

cheyiliu edited this page Jun 23, 2016 · 11 revisions

osgi

OSGi联盟现在将OSGi定义为一种技术: OSGi技术是指一系列用于定义Java动态化组件系统的标准。这些标准通过为大型分布式系统以及嵌入式系统提供一种模块化架构减少了软件的复杂度。

concept

  • Bundle, A "bundle" is a module in OSGi terminology. Or if you are an Eclipse developer, you may know them as plug-ins; bundles and plug-ins are basically the same things.

  • A bundle also needs a manifest file that declares various metadata about the bundle, e.g. its name, version, etc.

  • BundleActivator interface, allowing the framework to notify us of important lifecycle events. When the bundle is started, the framework calls the start method, and when the bundle is stopped, the framework calls the stop method.

  • BundleContext is a magic ticket that the OSGi framework passes to our bundle. When code needs to interact with the framework in any way, you will use the BundleContext . In fact this is the only way to interact with the OSGi API, and the framework issues one of these tickets to each bundle through its BundleActivator when the bundle is started.

  • Dependencies between Bundles, Export-Package: osgitut.movies;version="1.0.0" Import-Package: osgitut.movies;version="[1.0.0,2.0.0)"

  • Registering a Service,

public void start(BundleContext context) {
       MovieFinder finder = new BasicMovieFinderImpl();

       Dictionary props = new Properties();
       props.put("category", "misc");

       registration = context.registerService(
                              MovieFinder.class.getName(),
                              finder, props);
   }

  • Consuming a Service, ServiceTracker is a very useful class which abstracts away a lot of unpleasant detail in the lowest levels of the OSGi API. Note that you may have seen elsewhere code that retrieves a service from the registry without using ServiceTracker . For example, it is possible to use the getServiceReference and getService calls on BundleContext . However the code you have to write is quite complex and it has to be careful to clear up after itself. In my opinion, there is very little benefit in dropping down to the low level API, and lots of problems with it. It's better to use ServiceTracker almost exclusively.

  • Dynamic Service Tracking, TODO

doc & jar

Clone this wiki locally