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

Getting Started with OSGi: Registering a Service

阅读更多
Welcome back to the EclipseZone OSGi mini-series. Finally, we're ready to get on to services. In my opinion, the service layer is the most exciting part of OSGi, so these next few installments should be fun.

Last time we looked at the example of a MovieFinder interface, which we said would be used by a MovieLister to search for movies. In fact you may recognize this example -- it is from Martin Fowler's famous paper on "Dependency Injection", also known as "Inversion of Control" or IoC.

Recall the problem that IoC tries to solve. A MovieLister doesn't particularly care where the raw movie data comes from, so we use the MovieFinder interface to hide such details from it. The idea is we can then substitute alternative implementations of MovieFinder , such as one that goes to a database or even one that calls an Amazon Web Service, since MovieLister only depends on the interface, not any particular implementation.

So far so good, but at some point we have to actually give a concrete implementation of MovieFinder to MovieLister . We do this by having an external container "push" a suitable object into it, rather than letting MovieLister go out and call a lookup method. Hence the term "Inversion of Control". Many such containers have been developed, for example PicoContainer, HiveMind, Spring, and even EJB 3.0. However there is one limiting factor of all these containers to date: they are mostly static. Once a MovieFinder is given to a MovieLister , it tends to be associated for the lifetime of the JVM.

OSGi also allows us to implement the IoC pattern, but in a dynamic way. It should be possible to dynamically supply implementations of MovieFinder to MovieLister and later remove them. Then we can hot-swap from an application that looks up movies in a flat text file to an application that looks them up with Amazon Web Services.

It is the Service Layer that helps us do this. Quite simply, we register a MovieFinder as a Service in the Service Registry. Later the MovieLister can be supplied with that MovieFinder Service. A Service therefore is nothing more than a Java object -- a POJO, if you will -- and it is registered under the name of a Java interface (a POJI?).

This time around, we will just look at registering the service with the registry. Later we will look at how to get the service out of the registry and supplied to a MovieLister .

We're going to add the BasicMovieFinder bundle that we built last time. We don't need to modify any existing classes, we just need to add a bundle activator. So copy this into osgitut/movies/impl/BasicMovieFinderActivator.java :

package osgitut.movies.impl;
 
import org.osgi.framework.*;
 
import osgitut.movies.*;
import java.util.Properties;
import java.util.Dictionary;
 
public class BasicMovieFinderActivator implements BundleActivator {
    private ServiceRegistration registration;
 
    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);
    }
 
    public void stop(BundleContext context) {
        registration.unregister();
    }
}




Now replace the content of BasicMovieFinder.mf :
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Basic Movie Finder
Bundle-SymbolicName: BasicMovieFinder
Bundle-Version: 1.0.0
Bundle-Activator: osgitut.movies.impl.BasicMovieFinderActivator
Import-Package: org.osgi.framework,
 osgitut.movies;version="[1.0.0,2.0.0)"
 




There are two things added to this manifest since last time. First is the Bundle-Activator line, which tells the framework about the new activator for our bundle -- we didn't need one last time. Also I have added org.osgi.framework to the imported packages. As our previous version of the bundle didn't interact with the framework, it didn't need to import the OSGi API packages.

Now you can rebuild BasicMovieFinder.jar :

> javac -classpath equinox.jar:MoviesInterface.jar osgitut/movies/impl/*.java
> jar cfm BasicMovieFinder.jar BasicMovieFinder.mf osgitut/movies/impl/*.class



Back in the OSGi console, you should still have BasicMovieFinder.jar installed from last time. So you just need to tell OSGi to update the bundle, by typing update N , where N is the numeric ID of the bundle (which you have found using ss ). Now start the bundle with the command start N and you should see... very little happen.

Actually we've just registered our first service with the OSGi service registry, but unfortunately there's nobody on the "other end", so the registration doesn't produce any visible effect. If we want to reassure ourselves that our code has actually done something, we're going to have to go digging, and we do that with the following command:

services (objectClass=*MovieFinder)



We should see the following output:

{osgitut.movies.MovieFinder}={category=misc, service.id=22}
  Registered by bundle: file:BasicMovieFinder.jar [4]
  No bundles using service.



Great, our service is registered! And I'd love to go on and tell you how to lookup that service and use it in another bundle, but that will have to wait until another day. In the meantime, see what you can do with the services command. For starters try typing services without the expression in parentheses afterwards -- that was actually a filter which reduced the number of services displayed to just the one we were interested in. Without the filter you will see all of the registered services. There are a surprisingly large number of them!
分享到:
评论

相关推荐

    Getting Started with OSGi_ Part1

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

    OSGi Technology 教程

    "Getting Started with OSGi 4 Registering a Service.doc"则关注服务的注册过程。在OSGi中,服务是可发现和可使用的对象,它们被注册到服务注册表中,以便其他组件可以查找和使用。文档将详细阐述如何创建和注册...

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

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

    Getting Started with OSGi_ Part3

    在OSGi(Open Service Gateway Initiative)框架中,理解并管理模块间的依赖关系是至关重要的。本篇教程将深入探讨这一主题,帮助开发者们更好地掌握OSGi环境下的程序设计。 一、OSGi概述 OSGi是一种动态模块系统,...

    Getting Started with OSGi_ Part2

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

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

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

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

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

    OSGi_Service

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

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

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

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

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

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

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

    《深入理解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应用程序以模块化的方式...

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

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

    OSGi Service Platform Service Compendium

    ### OSGi Service Platform Service Compendium 知识点解析 #### 一、OSGi Service Platform Service Compendium 概览 **OSGi Service Platform Service Compendium** 是由 **OSGi Alliance** 发布的一份关于 OSGi...

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

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

Global site tag (gtag.js) - Google Analytics