`
classtwo5367
  • 浏览: 39029 次
  • 性别: 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”,但可以从给定的内容中提取出文章的重点信息。标签“源码工具”可能意味着在文章的系列...

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

    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原理与最佳实践pdf下载(完整版)

    OSGI原理与最佳实践的完整版,共12章 第1 章OSGi 简介 第2 章OSGi 框架简介 第3 章基于Spring-DM 实现Petstore 第4 章基于Apache CXF 实现分布式Petstore 第5 章构建OSGI Bundle Repositor'y 第6 章OSGi 规范解读 ...

    declarative

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

    org.osgi.core-6.0.0.jar

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

    OSGi_Service

    **OSGi服务详解** OSGi(Open Service Gateway Initiative)是一种Java模块化系统,它为创建、部署和管理软件组件提供了一种强大而灵活的方式。在Java世界中,OSGi被视为解决大型复杂应用系统中模块化问题的有效...

    OSGI错误分析解决

    OSGI(Open Services Gateway Initiative)是一个Java平台的模块化系统,它允许开发人员将应用程序分解为独立的组件,称为服务。这些服务可以独立地安装、更新和卸载,而不会中断整个系统的运行。在OSGI环境中,错误...

    spring-boot-with-embeded-osgi:带有嵌入式OSGI框架的Spring Boot项目

    带有嵌入式OSGI的Spring Boot 这是一个嵌入了Felix OSGI框架的示例Spring Boot项目。 其他项目是API(接口和模型类)及其实现。 Spring Boot应用程序将这些程序包作为OSGI框架的额外程序包公开(以便能够使用公开的...

    tutorial-osgi:OSGI 示例

    教程-osgi OSGI 示例 设置 设置 Git、Ant 和 Maven 查看示例。 $ git clone https://github.com/danidemi/tutorial-osgi.git $ cd tutorial-osgi 只需在项目文件夹中运行ant ,这将指示 Ant 下载一些额外的库。 ...

    primefaces-cookbook-osgi:在各种 osgi 容器(jboss、kara、virgo)上制作工作 primefaces 食谱

    primefaces-cookbook-osgi 在各种 osgi 容器(jboss、karaf、virgo)上制作工作 primefaces 食谱注意:用于 osgi env 的 primefaces coobook。 只是来自一个叉子和一小撮osgi JBoss EAP 6.2 - 工作Apache Karaf - 不...

    osgi.r4.services

    本文档基于`osgi.r4.services.pdf`文件提供的内容,深入探讨OSGi R4服务规范中的关键概念和技术要点。 #### 二、OSGi服务平台发布版4简介 - **发布日期**:2005年8月。 - **版权所有**:OSGi联盟版权所有2005年、...

Global site tag (gtag.js) - Google Analytics