`
classtwo5367
  • 浏览: 38563 次
  • 性别: Icon_minigender_1
  • 来自: Cork
最近访客 更多访客>>
社区版块
存档分类
最新评论

Getting Started with OSGi: Declarative Services and Dependencies

阅读更多
Welcome back to the EclipseZone "Getting Started with OSGi" tutorial series. Before getting into today's lesson I'd like to remind you that you can find links to all the previous parts of the tutorial on the contents page  on my personal blog.

Last time we took our first look at Declarative Services. This time we will look at the service consumer side of Declarative Services. Remember that previously we registered a service under the java.lang.Runnable interface; now we will create a component that depends on that service.

As discussed, the Declarative Services specification is all about letting you focus on the application logic of your code, rather than the OSGi "glue" code that had to be written in previous lessons. With that in mind, we're simply going to dive into the code, but before we do that we need to create a project. Follow the same steps as in the last lesson, but use the project name "SampleImporter".

Now copy the following code from your browser and paste it into the src folder inside the newly created Eclipse project:
package org.example.ds;
 
import org.eclipse.osgi.framework.console.CommandInterpreter;
import org.eclipse.osgi.framework.console.CommandProvider;
 
public class SampleCommandProvider1 implements CommandProvider {
	
	private Runnable runnable;
	
	public synchronized void setRunnable(Runnable r) {
		runnable = r;
	}
	public synchronized void unsetRunnable(Runnable r) {
		runnable = null;
	}
	public synchronized void _run(CommandInterpreter ci) {
		if(runnable != null) {
			runnable.run();
		} else {
			ci.println("Error, no Runnable available");
		}
	}
	public String getHelp() {
		return "\trun - execute a Runnable service";
	}
}



This class implements the CommandProvider interface, which is used to extend the set of commands available at the "osgi>" prompt when you run Equinox. The reason for writing a CommandProvider is that it provides a convenient way to test our code interactively. There is a more detailed discussion of command providers in Chris Aniszczyk's article on IBM developerWorks.

Notice that we don't call any OSGi APIs in this class, in fact we don't even need to import anything from the org.osgi.* packages. The service that we depend on – in this case, an instance of java.lang.Runnable – is provided to us through the setRunnable method, and it is taken away using the unsetRunnable method. We can consider this a form of dependency injection .

The other two methods, getHelp and _run are the implementation methods for the command provider. Yes, "_run" is a funny name with its leading underscore, but that is just an odd feature of the Equinox console API, and nothing to do with OSGi or Declarative Services. Methods using the leading-underscore pattern become commands on the Equinox console, so by providing a method called _run we have added a "run" command. Another thing to notice about this class is that we're being careful to ensure that the runnable field is updated and accessed in a thread-safe way. Thread safety is particularly important in OSGi since it is intrinsically multithreaded, but frankly we should always write our code to be thread-safe.

As before, we have to provide an XML file containing the DS declarations to get this to work. Copy the following into OSGI-INF/commandprovider1.xml in your plug-in project:
<?xml version="1.0"?>
<component name="commandprovider1">
	<implementation class="org.example.ds.SampleCommandProvider1"/>
	<service>
		<provide interface="org.eclipse.osgi.framework.console.CommandProvider"/>
	</service>
	<reference name="RUNNABLE"
		interface="java.lang.Runnable"
		bind="setRunnable"
		unbind="unsetRunnable"
		cardinality="0..1"
		policy="dynamic"/>
</component>



There's another important step which I neglected to mention last time. (Thanks to Seamus Venasse for pointing it out .) You need to edit the build.properties file in your plug-in project, and check the box next to the OSGI-INF folder. This is necessary to ensure the folder is included when the bundle is exported using Eclipse's export wizard, or built using PDE Build.
Also we need to add the following line to the bundle manifest:

Service-Component: OSGI-INF/commandprovider1.xml



This declaration has two of the same elements that we saw before, the implementation and service nodes. The implementation node provides the name of the class which implements the component, and the service node tells DS to register the component as a service. In this case we are registering under the CommandProvider interface, which is how we let the Equinox console know about the existence of our command provider.

The next element called reference is something we haven't seen before – it declares to DS that our component has a dependency on a service. The name attribute is simply an arbitrary string which names the dependency (we don't need to worry yet about what this is used for) and the interface attribute specifies the name of the interface we depend on. The bind attribute is the name of a method in the implementation class that will be called by DS when a service becomes available, or in other words, when a Runnable service is registered with the Service Registry, DS will obtain a reference to the new service object and supply it to our component using the specified method. Likewise the unbind attribute is the name of a method that will be called by DS when a service we were using becomes unavailable.

The cardinality attribute reveals the real power of DS. This attribute controls whether the dependency is optional or mandatory, and whether it is singular or multiple. The possible values are:

    * 0..1: optional and singular, "zero or one"
    * 1..1: mandatory and singular, "exactly one"
    * 0..n: optional and multiple, "zero to many"
    * 1..n: mandatory and multiple, "one to many" or "at least one"



In this example we chose optional and singular, which means our command provider can cope with the dependent service being unavailable. Looking back at the code for the _run method, you can see it was necessary to do a null check to handle such a situation.

Let's see what happens if we run this bundle. If you still have your SampleExporter bundle from last time, then you should see the following response when you type the "run" command at the osgi> prompt:


    Hello from SampleRunnable



Great! This confirms that we have successfully imported the Runnable service that we wrote in the last lesson. Now try using the stop command to turn off the SampleExporter bundle. When you type the "run" command again you should see:


    Error, no Runnable available



Which means that DS noticed the Runnable service going away, and called our unsetRunnable method to let us know.

Looking again at the cardinality attribute, what do you think will happen if we change to "1..1", i.e. switch from an optional to a mandatory dependency? Try making that change and restarting Equinox. If the SampleExporter bundle is active when we type "run" then we will see the same message as before: "Hello from SampleRunnable". However, if SampleExporter is inactive then we will see a very different error message than before. In fact, we will see Equinox's console help message, which is its standard response to an unrecognized command. This indicates that our command provider itself has been unregistered by DS! Since the component has a mandatory dependency which cannot be satisfied, DS is forced to deactivate the component and unregister any services it was providing. That in turn means that the Equinox console forgets about the "run" command.

The ease with which we can make changes like this is one of the best reasons to use Declarative Services. Remember that with ServiceTracker we would have had to rewrite a fairly substantial piece of code.

You might be wondering about the policy attribute, since I haven't mentioned it yet. The value of this can be either "static" or "dynamic", and it states whether the component implementation is able to cope with having services dynamically switched. If it is not, then it is necessary for DS to deactivate the component and create a new instance each time the target service changes. As you might expect, this is quite a heavyweight approach, and it's best to code your component classes to support dynamic switching whenever possible. Unfortunately the default value is static, so you have to remember to set it to dynamic explicitly.

So, we've seen optional singular and mandatory singular, but what about multiple dependencies? There may be more than one Runnable registered in the Service Registry, and if we only bind to one of them then the choice of which one we get is arbitrary. Perhaps instead we would like to implement a "runall" command that runs all of the currently registered Runnables.

What would happen if we just changed the cardinality to "0..n"? In a way, that would almost work: instead of calling the setRunnable method just one time, DS will call setRunnable once for each instance of Runnable in the registry. The problem is that the class we wrote will get confused. Instead of setting a single Runnable field, we need to accumulate the Runnables into a collection. Here's the slightly modified version of the class, which you can again copy and paste directly into the src folder of your project:
package org.example.ds;
 
import java.util.*;
import org.eclipse.osgi.framework.console.CommandInterpreter;
import org.eclipse.osgi.framework.console.CommandProvider;
 
public class SampleCommandProvider2 implements CommandProvider {
 
	private List<Runnable> runnables =
		Collections.synchronizedList(new ArrayList<Runnable>());
	
	public void addRunnable(Runnable r) {
		runnables.add(r);
	}
	public void removeRunnable(Runnable r) {
		runnables.remove(r);
	}
	public void _runall(CommandInterpreter ci) {
		synchronized(runnables) {
			for(Runnable r : runnables) {
				r.run();
			}
		}
	}
	public String getHelp() {
		return "\trunall - Run all registered Runnables";
	}
}



Now create OSGI-INF/commandprovider2.xml and copy in the following contents:
<?xml version="1.0"?>
<component name="commandprovider2">
	<implementation class="org.example.ds.SampleCommandProvider2"/>
	<service>
		<provide interface="org.eclipse.osgi.framework.console.CommandProvider"/>
	</service>
	<reference name="RUNNABLE"
		interface="java.lang.Runnable"
		bind="addRunnable"
		unbind="removeRunnable"
		cardinality="0..n"
		policy="dynamic"/>
</component>



... and finally add this file to the Service-Component header of the manifest, so that it looks like this:

Service-Component: OSGI-INF/commandprovider1.xml,

  OSGI-INF/commandprovider2.xml



This declaration essentially the same as before, except we have renamed the bind and unbind methods, and changed the cardinality to "0..n". As an exercise, try registering a few additional Runnable services and check that the "runall" command executes all of them. Next, what do you think will happen if we change the cardinality to "1..n"? Try verifying it does what you expect.

That's all for this lesson. Remember that the OSGi Alliance Community Event is happening next week in Munich, Germany, and I believe it's still not too late to register . See you there!
分享到:
评论

相关推荐

    OSGi Technology 教程

    最后,"Getting Started with OSGi 8 Declarative Services and Dependencies.doc"详细阐述了DS的用法和优势,包括如何声明服务组件、依赖关系以及处理组件间的交互。 通过这个系列的教程,开发者将全面了解OSGi...

    Getting Started with OSGi_ Part1

    在标题“Getting Started with OSGi Part1”中,指明了这是一个关于OSGi入门的系列文章中的第一部分。描述部分虽然为“NULL”,但可以从给定的内容中提取出文章的重点信息。标签“源码工具”可能意味着在文章的系列...

    Getting Started with OSGi_ Part2

    OSGi(Open Services Gateway initiative)是一种Java框架,它定义了服务加载和模块化应用的标准方式。OSGi技术广泛应用于企业级应用开发中,尤其是在Eclipse插件开发和Java EE应用服务器中。OSGi规范定义了如何在...

    深入理解OSGi:Equinox原理、应用与最佳实践.pdf

    OSGi(Open Service Gateway Initiative)是一个定义了Java应用程序如何组织和模块化以及如何动态发现、启动、停止、更新这些模块化组件的规范。Equinox是OSGi规范的一个实现,它是由Eclipse基金会开发的。本文将...

    Getting Started with OSGi_ Part3

    《OSGi初识系列教程——第三部分:模块间的依赖关系》 在OSGi(Open Service Gateway Initiative)框架中,理解并管理模块间的依赖关系是至关重要的。本篇教程将深入探讨这一主题,帮助开发者们更好地掌握OSGi环境...

    深入理解OSGi:Equinox原理、应用与最佳实践源代码+equinox-SDK-3.8源代码

    本资源包括两部分:《深入理解OSGi:Equinox原理、应用与最佳实践》的源代码和equinox-SDK-3.8的源代码。 深入理解OSGi这本书提供了对OSGi,特别是Equinox实现的全面洞察。书中可能涵盖以下几个知识点: 1. **OSGi...

    Automatically managing service dependencies in OSGi(2005)

    In a service oriented architecture, ...components (bundles) can be started and stopped any time. Other components often depend on these services and need to deal with changes in their availability.

    spring-osgi-1.2.1-with-dependencies.zip

    spring-osgi-1.2.1-with-dependencies.zip spring-osgi-1.2.1-with-dependencies.zip spring-osgi-1.2.1-with-dependencies.zip

    深入理解OSGi:Equinox原理、应用与最佳实践.zip

    在《深入理解OSGi:Equinox原理、应用与最佳实践》这本书中,作者深入探讨了OSGi的核心概念、Equinox的工作原理以及如何在实际项目中应用OSGi。这本书的源码可能是为了辅助读者理解和实践书中所讲解的内容。 **OSGi...

    深入理解OSGi:Equinox原理、应用与最佳实践,书本源代码

    在深入理解OSGi:Equinox原理、应用与最佳实践中,我们可以学习到以下几个关键知识点: 1. **模块化编程**:OSGi的核心是模块化,它将应用程序划分为独立的单元,称为服务或bundle。每个bundle都有自己的类路径,...

    《深入理解OSGi:Equinox原理、应用与最佳实践》迷你书

    本书《深入理解OSGi:Equinox原理、应用与最佳实践》深入剖析了OSGi技术的原理和应用,着重介绍了基于OSGi R5.0规范的内容,并结合了Equinox框架的实践经验,旨在帮助读者更好地理解和应用OSGi技术。 本书共分为四...

    《深入理解OSGi:Equinox原理、应用与最佳实践》附赠光盘

    《深入理解OSGi:Equinox原理、应用与最佳实践》这本书是关于OSGi技术的一部权威著作,其附赠光盘包含丰富的学习资源,旨在帮助读者深入掌握OSGi的精髓,特别是Equinox实现的细节。OSGi(Open Services Gateway ...

    SpringDM笔记28-Spring And OSGi:Layers of Integration

    标题中的"SpringDM笔记28-Spring And OSGi:Layers of Integration"表明这是一篇关于Spring框架与OSGi(Open Service Gateway Initiative)集成的详细笔记。OSGi是一种模块化系统,它允许Java应用程序以模块化的方式...

    深入理解OSGi:Equinox原理、应用与最佳实践

    ### 深入理解OSGi:Equinox原理、应用与最佳实践 #### OSGi概述 OSGi(Open Service Gateway Initiative)是一种模块化系统和服务组件模型,它为Java平台提供了一种动态部署、管理和更新应用程序和服务的方法。...

    《深入理解OSGi:Equinox原理、应用与最佳实践》

    《深入理解OSGi:Equinox原理、应用与最佳实践》自从1999年OSGi联盟成立以来,OSGi技术随着Java一起飞速发展,它已经成为一种被广泛认可的软件架构技术和方法,许多世界著名的IT企业都加入到OSGi的阵营之中,OSGi...

    spring-osgi-1.2.1-with-dependencies

    总结来说,“spring-osgi-1.2.1-with-dependencies”是一个集成了Spring与OSGi的完整包,它提供了在OSGi环境中运行Spring应用所需的所有组件和服务。通过理解和掌握这个包,开发者可以更好地利用OSGi的模块化优势,...

    spring-osgi-1.2.0-rc1-with-dependencies.zip

    《Spring OSGi详解:基于1.2.0-rc1版本》 Spring OSGi是Spring框架与OSGi(开放服务网关倡议)技术的结合,它为开发者提供了在OSGi环境中使用Spring的能力。OSGi是一种Java模块化系统,旨在解决大型软件系统的复杂...

    spring-osgi-1.2.0-with-dependencies.zip

    spring-osgi-1.2.0-with-dependencies.zip spring-osgi-1.2.0-with-dependencies.zip spring-osgi-1.2.0-with-dependencies.zip

    Eclipse RCP与Spring OSGi技术详解与最佳实践

    《Eclipse RCP与Spring OSGi:技术详解与最佳实践》由资源的Eclipse专家亲自执笔,并得到了Eclipse官方技术社区的强烈推荐,权威性毋庸置疑!内容全面,系统讲解了利用Eclipse RCP和Spring OSGi开发大规模Java应用的...

Global site tag (gtag.js) - Google Analytics