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

Getting Started with OSGi: Introducing Declarative Services

阅读更多
Welcome to the much-delayed next installment of the "Getting Started with OSGi" series. This installment is quite exciting, because we're going to start experimenting with Declarative Services.

The Declarative Services (or "DS") specification is one of the newest parts of OSGi, and it came about as a result of some of the issues with wiring together service across bundles. It's not that this task is difficult – as I hope my previous tutorials showed – but it does require a fair amount of boilerplate code. It also requires you to be cautious about threading issues, which means you can easily shoot yourself in the foot.

An early attempt to resolve this problem was a tool called Service Binder, developed by Humberto Cervantes and Richard Hall . Service Binder introduced automated service dependency management , allowing developers to focus on the job of writing their services. The wiring between services was handled declaratively, and the declarations were made in XML.

The Declarative Services specification evolved from Service Binder, and it is now a standard part of OSGi versions 4.0 and above. So, lets see what it can do for us.

As I mentioned in the previous installment of the tutorial, I'm tired of doing everything on the command line: from this point onwards, we will use the Eclipse SDK. Please remember though that none of what I'm introducing really depends on Eclipse. Although Eclipse helps a lot with what we're about to do, there's no black magic going on, so anything you see here is perfectly possible in NetBeans, IntelliJ or even good old vi.

The first thing we need to do is download the Equinox implementation of Declarative Services. Assuming you're using the current stable version of Eclipse, which is 3.2.2 at time of writing, the direct link is here . If you're using a different version, you need to find that version from the top-level Equinox download page here and get the file org.eclipse.equinox.ds_x.x.x_xxxxx.jar . Once it's downloaded, drop it into the plugins directory of your Eclipse installation, and restart Eclipse. Actually if you know a little about Eclipse plug-in development already, you should put this JAR into your Target Platform folder. If you don't know what a "Target Platform" is, then don't worry about it, just drop the JAR into the plugins folder.

Now we'll create a new bundle. To do this, we create a project in Eclipse using the Plug-in Project wizard:


   1. From the main menu, select File -> New -> Project.

   2. Select Plug-in Project and hit Next.

   3. Enter the name of the project, SampleExporter .

   4. At the bottom, below the text "This plug-in is targeted to run with", select "an OSGi framework" and then select "standard" from the drop-down. This step isn't absolutely essential, it just prevents us from accidentally using features that are not available out-of-the-box on other OSGi framework implementations.

   5. Click Next. On the following page of the wizard, deselect "Generate an activator...", if it is checked. Then click Finish.



We now have an empty bundle project, so we need to add some code. To keep things as simple as possible, we're going to offer a service using an interface that's already available on every Java runtime: java.lang.Runnable . Using a handy little Eclipse shortcut, just copy the following code from your browser, and then select the src folder inside the SampleExporter project and hit Edit -> Paste. Eclipse will create the package and source file for you.
package org.example.ds;
 
public class SampleRunnable implements Runnable {
 
	public void run() {
		System.out.println("Hello from SampleRunnable");
	}
 
}



So far we haven't seen anything new, but here's where it gets interesting: we're going to create an XML file to declare SampleRunnable as a service. Create a folder at the top level of the project called OSGI-INF and copy the following into a file in that folder called samplerunnable.xml :
<?xml version="1.0"?>
<component name="samplerunnable">
	<implementation class="org.example.ds.SampleRunnable"/>
	<service>
		<provide interface="java.lang.Runnable"/>
	</service>
</component>



This is one of the simpler declarations we will see with DS. It says that there is a component called "samplerunnable" which provides a service to the OSGI Service Registry under the interface java.lang.Runnable , and the component is implemented by the class org.example.ds.SampleRunnable .

The final step is to tell the Declarative Services runtime about the existence of this XML file. We do this by adding a field to the MANIFEST.MF for the bundle. So, open the manifest (try right-clicking on the project and selecting PDE Tools -> Open Manifest) and in the editor, skip to the tab labeled "MANIFEST.MF", as this allows us to edit the text contents of the manifest directly. Add the following line:
Service-Component: OSGI-INF/samplerunnable.xml



Then save the file. As in previous installments, the blank line at the end of the manifest is VERY important, but unlike previously, you will now get an error message from Eclipse if you forget.

Before going any further, lets run Equinox to check that this works. Go to the Run menu and select "Run...". When the dialog opens, select "Equinox OSGi Framework" in the tree on the left, and then hit the New button. If you're an experienced Eclipse user but have not done OSGi development, this launcher dialog might look a little bit strange. The first tab, labeled Bundles, allows us to choose which bundles should be included in the launch configuration, and whether they should be initially started or not. To get the minimal set, click Deselect All and then place a tick next to the following bundles only:


    * SampleExporter (underneath Workspace)

    * org.eclipse.equinox.ds (underneath Target Platform)



These are the bundles we are interested in, but they do have dependencies, so click Add Required Bundles to add those dependencies back in. Also it's a good idea to select "Validate bundles automatically prior to launching". Finally, click Run, and in a few moments the osgi> prompt should appear in the Eclipse console. The OSGi framework is running, and you can interact with it using the same commands you learned previously.

So is our service registered? We can check by typing the command services . Somewhere in the list of services – probably the bottom – you should see the following:

{java.lang.Runnable}={component.name=samplerunnable, component.id=1, service.id=22}
  Registered by bundle: initial@reference:file:..../SampleExporter/ [5]
  No bundles using service.



Yes, it's registered! And notice one important thing: it appears to have been registered by our bundle, not by the Declarative Services bundle. In fact DS has registered it on behalf of our bundle. We'll look at how it does that a little later on, but for the purposes of this lesson, it's enough to note that consumers of our service don't need to do anything special, in fact they need not even know that we are using Declarative Services. Those consumers can use Declarative Services as well if they wish to, but they can also use straightforward OSGi code.

Another thing to note is that in our bundle, there is no OSGi-specific Java code. In other words, the class we wrote is a POJO, and this is a major feature of Declarative Services.

That's enough for this installment, and I'm sorry that this lesson is a little basic. Next time we'll look at how to consume services in various ways with DS, which should really start to show the power and convenience that it offers.
分享到:
评论

相关推荐

    OSGi Technology 教程

    "Getting Started with OSGi 7 Introducing Declarative Services.doc"引入了声明式服务(Declarative Services,DS),这是OSGi中一种简化服务管理的方法。DS允许开发者通过XML配置文件来声明服务依赖和生命周期...

    Getting Started with OSGi_ Part1

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

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

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

    Getting Started with OSGi_ Part2

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

    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...

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

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

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

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

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

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

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

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

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

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

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

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

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

    全面解读OSGi规范,深刻揭示OSGi原理,详细讲解OSGi服务,系统地介绍Equinox框架的用法,并通过源代码分析其工作机制,包含大量可操作性极强的解决方案和最佳实践。

    declarative

    Declarative Services(DS),在OSGi环境中,是一种声明式的方式来管理服务和组件的机制。它的核心思想是通过XML配置文件来定义服务的提供者和消费者,而不是通过代码直接引用和依赖其他服务,从而实现更加灵活和...

    org.osgi.core-6.0.0.jar

    osgi的jar包,第一次上传,有需要的可以自取,联网下载很慢,希望对你们有帮助,偶然间遇到了

Global site tag (gtag.js) - Google Analytics