OSGi入门:介绍Declarative Services[译]

欢迎来到“OSGi入门”系列的下个部分。这个部分十分的有趣,因为我们将开始尝试使用Declarative Services。

Declarative Service(或者“DS”)规范是OSGi最新的一个部分,并且它来自于跨越Bundle将服务配置到一起的结果。并非这个任务困难——正如我希望我以前讲课中展示的——它需要相当数量的样板代码。它也要求你对内容的条理很谨慎,也就是说你很容易搬起石头砸自己的脚。

早期试图解决此问题的是一个叫做Service Binder的工具。由 Humberto Cervantes Richard Hall 开发的工具。Service Binder引入了自动化的服务依赖关系管理,允许开发人员可以集中精力编写他们的服务。服务之间的配置通过声明处理,并且声明都写在XML中。

这个Declarative Service规范是从Service Binder进化而来,并且是OSGi的4.0版本的一个标准部分。那么,让我们看看它能做什么。

正如我在课程的上一个部分叙述的,我厌倦了在命令行做任何事情:从这点上,我将使用Eclipse SDK。请记住我所介绍的事实上并不依赖Eclipse。虽然Eclipse对我们有或多或少的帮助,不过没什么黑色魔法,所以我们在这里看到的任何东西在NetBeans、IntelliJ甚至在老旧的vi上都是完全可行的。

第一件事,我们需要下载Declarative Services的Equinox实现。假如你使用当前稳定的Eclipse版本是3.2.2,那就从这里下载。如果你使用不同的版本,你需要从顶层的Equinox下载页找到这个版本,并下载org.eclipse.equinox.ds_x.x.x_xxxxx.jar文件。把下载下来的文件放到你的Eclipse安装目录的plugins下,并重启Eclipse。事实上,如果你已经对Eclipse Plug-in开发有一点点了解,你可以把这个jar文件放入你的目标平台文件夹下。如果你不知道什么是“目标平台”,不用担心,只要把这个jar放到plugins目录下就可以了。

现在,我们将创建一个新的Bundle。做这些事情,我们使用Eclipse中的Plug-in项目向导来创建一个项目:[list=decimal]
  • 从主菜单,选择File->New->Project
  • 选择Plug-in Project并单击下一步
  • 输入项目名称:SimpleExporter
  • 在底部,“This plug-in is targeted to run with”文字下面,选择“an OSGi framework”之后在下拉框中选择“standard”。这一步不是绝对必须的,它只是防止我们使用盒外无效的其他OSGi框架实现的特性。
  • 单击下一步。在向导接下来的页面中,取消选中的“Generate an activator”复选框。单击结束
    [/list]

    我们现在有个空的Bundle项目,所以我们需要添加一些代码。为了尽可能的简单,我们将提供一个使用了在所有的Java Runtime中都有效的接口:java.lang.Runnable,使用一个顺手的小的Eclipse快捷方式,仅仅从你的浏览器复制下面的代码,然后选择SampleExporter项目中的src文件夹并点击Edit -> Paste。Eclipse将为你创建包和源文件。
    1
    2
    3
    4
    5
    6
    7
    package org.example.ds;
     
    public class SampleRunnable implements Runnable {
    	public void run() {
    		System.out.println("Hello from SampleRunnable");
    	}
    }
    


    到目前为止我们没有看到任何新东西,但是,接下来会有趣些:我们将创建一个XML文件来声明SampleRunnable为一个服务。在顶级目录下创建一个名为OSGI-INF的文件夹,并复制下面的例子到文件samplerunnable.xml中:
    1
    2
    3
    4
    5
    6
    7
    <?xml version="1.0" encoding="UTF-8"?>
    <component name="samplerunnable">
    	<implementation class="org.example.ds.SampleRunnable"/>
    	<service>
    		<provide interface="java.lang.Runnable"/>
    	</service>
    </component>
    


    这是我们将看到的DS中简单声明中的一个。它描述了一个提供一个服务到OSGI Service Registry的叫做“samplerunnable”的在java.lang.Runnable下的组件,并且这个组件通过类org.example.ds.SampleRunnable来实现。

    最后一步是告诉Declarative Service运行时关于这个XML文件的存在。我们为Bundle做这个事情是通过添加一个字段到MANIFEST.MF文件。因此,在编辑器中打开manifest(试着右键点击这个项目并选择PDE工具 -> Open Manifest),跳到名为“MANIFEST.MF”的标签,这允许我们直接编辑manifest的文本内容。添加下面的一行:
    1
    Service-Component: OSGI-INF/samplerunnable.xml
    


    然后保存这个文件。正如在上个部分中说的,manifest的最后一个空行是非常重要的,但是不像以前,如果你忘记了,你将从Eclipse获得一个错误信息。

    在走的更远之前,让我们运行Equniox来检查这些工作。在Run菜单中选择“Run…”。对话框打开后,在左边的树形列表中选择“Equniox OSGi Framework”,然后点击New按钮。如果你是一个有经验的Eclipse用户但是没有做过OSGI开发,这个对话框可能看上去有些陌生。第一个标签是Bundles,允许我们选择哪些Bundles将包含在运行配置中,并且是否我们将在启动时便初始化。要获得最小集合,点击Deselect All并仅对以下Bundles做标记
    [list=decimal]
  • SampleExporter(underneath Workspace)
  • org.eclipse.equniox.ds(underneath Target Platform)
    [/list]

    这些是我们感兴趣的Bundles,但是它们有些依赖,所以单击Add Required Bundles来添加那些依赖。所以,更好的主意是选中“Validate bundles automatically prior to launching”。最后,点击Run,并稍等片刻,osgi>提示符将出现在Eclipse控制台中。OSGi框架正在运行,并且你可以使用你以前学过的相同的命令与之交互。

    那么是否我们的服务被注册了?我么可以通过键入services命令来检查。在服务列表中的某个地方—可能在底部—你将看到如下显示:
    1
    2
    3
    {java.lang.Runnable}={component.name=samplerunnable, component.id=1, service.id=22}
      Registered by bundle: initial@reference:file:..../SampleExporter/ [5]
      No bundles using service.
    


    是的,它被注册了!并且注意一件重要的事情:它通过我们的Bundle注册的,不是通过Declarative Services Bundle。事实上DS代表了我们的Bundle注册了它。我们将迟些了解它如何做的,但是这节课的目的,足够说明了我们的服务的消费者不需要任何具体的工作,事实上,它们连我们使用Declarative Services也不必知道。如果那些消费者愿意那可以很好的使用Declarative Services,但是它们也能使用简单的OSGi代码。

    另外要注意的是在我们的Bundle中,没有具体的OSGi的Java代码。换句话说,我们写了个POJO类,这是Declarative Services的一个主要特性。

    这些就够了,我很抱歉,这节课程只是一个小小的基础。下次,我们将看看如何用DS以不同的方式消费服务,这将真正的展示出DS提供的的强大和便利。


    参考:
    本文原文http://www.eclipsezone.com/eclipse/forums/t96740.html

    平均得分
    (0 次评分)





  • 文章来自: 本站原创
    标签: OSGi Equinox 
    评论: 6 | 查看次数: 1322
    • 共有 6 条评论
    游客 [2008-11-12 14:58:57]
    Expertise Rating. buy eve isk As explained back. ever quest platinum when it was implemented. guild wars gold Expertise reduces your opponent’s chances . guild wars gold to parry and dodge. HDRO gold Think of it as another. level wow Hit Rating. lineage 2 adena. While a mob cannot . lord of rings online gold parry attacks . lotro gold made from behind. lotro gold they can dodge them. lotro gold Since every point of Expertise. lotro po lowers their chance. power level to dodge by 0. powerleveling 25%. powerleveling wow you’ll need 26 Expertise . runescape gold or 104 Expertise Rating . runescape money to eliminate their. wow europe chance to dodge. wow level That’s 63 more Expertise. wow level Rating from gear . wow lvl if you pick up Weapon Expertise. wow lvl You can pick up 44 Expertise . wow power leveling Rating from just the [Shard of Contempt]. wow powerlevel from Magister’s Terrace. wow powerlevel Critical Strike Rating. 最新免费网络游戏 Critical Strike Rating. is not quite .
    游客 [2008-11-12 14:58:28]
    Strength. cheap wow power leveling If you see items . des po wow with Strength on them. free online games PASS. free online war games These are meant. gold wow for DPS Warriors or Ferals. gold wow Strength only gives you . gold wow 1 Attack Power . mp3 player for each point. online games You’re better . play war games off with other gear. po wow Trust me. power level Stamina. wow geld You should . wow geld get enough of . wow gold verkaufen this elsewhere. wow level service Do not try . wow leveling get to any more. wow leveling service Do not consider . wow lvl this stat . wow lvl 60 when you look at gear. wow lvl 70 It shouldn’t matter. wow power leveling Armor. wow powerlevel See Stamina. wow powerleveling If Blizzard made . wow schnell gold DPS cloth. wowgold I’d suggest you. 免费网络游戏look into it. 最新网游Most players .
    游客 [2008-11-12 14:57:13]
    Ogres down south. 8GB MP3 PLAYER in Badlands. apple ipod next to the earth . buy cheap wow gold elementals. canon digital camera they drop a bit of . cheap world of warcraft gold mageweave and silver. digital camera but as all other ogres . digital cameras they die really fast. dvd player so they are nice . eve isk to grind. ipod A lot of casters. ipod nano amoung them which. ipod shuffle is always nice. ipod touch Mithril spawn . ipods in the cave often . mp3 and a chest every 15 minutes. mp3 player Feralas. mp3 players ogres down south at the big ruin. mp4 45-47 is their lvls. portable dvd players drop decent mageweave. world of warcraft buy gold I am not going to be. wow breaking down class vs. wow gold class matchups. wow gold but going over . wow leveling the general . wow powerleveling idea of what . zubehoer mp3 player being a PvP Healing Druid . 网络游戏in World of. 网游 Warcraft entails.
    .
    Feralas. 2GB MP3 PLAYER ogres down south . 4GB MP3 PLAYER at the big ruin. buy wow gold 45-47 is their lvls. buying gold world of warcraft drop decent mageweave. cell phones and if u go down. cheap cell phones to the cave all . cheap wow gold the way down . cheap wow gold at the bottom. cheap wow gold u will almost only see . cheapest wow gold casters inside. eve isk Because of the packs u may get. mp3 players I suggest . phones cell not going here . portable mp3 player til the stated lvls. portable mp3 players instead of when. sell wow gold ur the same lvl . world of warcraft gold as the mobs. wow which would usually . wow gold be my suggested lvl. wow gold range to grind them. wow gold Also the warlords. wow gold call for help . wow gold when they get low. wow gold attracting other ogres. wow gold kaufen that are near. wow gold kaufen WoW's previous system. 魔兽of player versus player. 游戏 combat involved.
    游客 [2008-11-04 09:37:00]
    游客 [2008-10-30 14:16:55]
    wow power leveling
    wow power leveling
    Air Jordan Shoes
    Warhammer Gold
    Warhammer Online Gold
    Warhammer Accounts
    Warhammer Power Leveling
    Warhammer Online Key
    Warhammer Gold
    Warhammer Online Gold
    Warhammer Time Card
    Warhammer CD Key
    WAR gold
    warhammer online gold
    Buy WAR gold
    Buy warhammer gold
    Buy warhammer online gold
    warhammer gold
    WAR Accounts
    warhammer Accounts
    warhammer online Accounts
    Buy WAR Accounts
    warhammer Accounts for sale
    Warhammer Power Leveling
    Warhammer Online Power Leveling
    War Power Leveling
    Buy Warhammer Power Leveling
    Warhammer PowerLeveling
    Cheap Warhammer Power Leveling
    Cheap Warhammer Online Power Leveling
    Buy War Power Leveling
    Warhammer EU Power Leveling
    Cheap Warhammer Gold
    Cheap Warhammer online gold
    Buy Cheap Warhammer Gold
    Buy WAR Gold
    Warhammer EU Gold
    Warhammer EU Power Leveling
    Warhammer EU CD Key
    Warhammer EU Accounts
    Warhammer CD Key
    Warhammer online CD Key
    Warhammer Timecard
    Buy Warhammer Time Card
    Warhammer 60 days Time Card
    Cheap WAR Accounts
    Cheap warhammer Accounts
    Cheap warhammer online Accounts
    Buy Cheap WAR Accounts
    buy Warhammer CD Key
    buy Warhammer online CD Key
    cheap Warhammer CD key
    warhammer time card
    Warhammer prepaid time card
    Buy Lotro Gold | Lord Of The Rings Online Gold
    Lotro Accounts
    | Buy Lotro Accounts
    Lord Of The Rings Online Power Leveling | Lord Of The Rings Online PowerLeveling
    Lotro Cd Key | Lord Time Card
    Lotro Gold | Lotro Gold Instant Delivery
    lord of the rings online accounts | lord of the rings online accounts for sale
    Lotro Power Leveling | Lotro Powerleveling
    Lord Of The Rings Online Cd Key | Lord Of The Rings Online Time Card
    Lord of the Rings Online Gold
    Buy Lotro Gold
    Sell LoTRO Gold
    LoTRO CD Key
    LoTRO Europe Gold
    Cheap LoTRO Accounts
    Lord of the Rings Online Power Leveling
    Lord of the Rings online CD Key
    Cheap Lotro Gold
    游客 [2008-10-29 16:19:40]
    • 共有 6 条评论
    发表评论
    昵 称:  登录
    内 容:
    选 项:
    字数限制 1000 字 | UBB代码 开启 | [img]标签 开启