源码下载


分层:

modual:

主要作用于包级管理与共享代码

lifecycle:

主要作用于执行期间的模块管理与訪问osgi底层框架

service:

主要作用于多模块之间的相互通信

demo:


hello-provider/pom.xml

<?

xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.demo</groupId>
<artifactId>pom</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>hello-provider</artifactId>
<packaging>bundle</packaging>
<name>hello-provider</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<configuration>
<instructions>
<Export-Package>org.hello.provider</Export-Package>
<Import-Package>org.osgi.framework</Import-Package>
<Bundle-Activator>org.hello.provider.impl.UserServiceActivator</Bundle-Activator>
<Private-Package>org.hello.*</Private-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>

hello-provider/BundleActivator:

package org.hello.provider.impl;

import org.hello.provider.IUserService;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext; public class UserServiceActivator implements BundleActivator { public void start(BundleContext context) throws Exception {
System.out.println("registerService......");
context.registerService(IUserService.class.getName(),
new UserService(), null);
} public void stop(BundleContext context) throws Exception { } }

hello-client/pom.xml:

<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.demo</groupId>
<artifactId>pom</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>hello-client</artifactId>
<packaging>bundle</packaging>
<name>hello-client</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
</dependency>
<dependency>
<groupId>com.demo</groupId>
<artifactId>hello-provider</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Import-Package>org.hello.provider,
org.osgi.framework
</Import-Package>
<Bundle-Activator>org.hello.client.Client</Bundle-Activator>
<Private-Package>com.demo.hello.*</Private-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>

hello-client/BundleActivator:

package org.hello.client;

import org.hello.provider.IUserService;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference; public class Client implements BundleActivator { public void start(BundleContext context) throws Exception {
ServiceReference
reference=context.getServiceReference(IUserService.class.getName());
System.out.println(((IUserService)context.getService(reference)).add());
} public void stop(BundleContext context) throws Exception { } }

将bundle安装到本地仓库且部署到karaf(參考前一篇)


启动bundle


通过下面命令查看bundle的id

list

通过下面命令,启动bundle

bundle:start 78

參考演示样例

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd29iZW5kaWFua3Vu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

osgi实战学习之路:3. osgi分层概念及相互合作demo的更多相关文章

  1. osgi实战学习之路:8. Service-3之ServiceTracker

    通过ServiceTracker能够对查找的Service进行扩展 以下的demo引入装饰器模式对Service进行日志的扩展 demo: Provider student-manage/Activa ...

  2. osgi实战学习之路:2. maven+maven-bundle-plugin+karaf搭建osgi之HelloWorld

    环境准备: jdk版本号 jdk:1.7 karaf: 版本号:apache-karaf-3.0.1 下载地址: http://pan.baidu.com/s/1qWM4Y1u http://kara ...

  3. osgi实战学习之路:5.生命周期及利用命令、装饰者模式实现基于socket交互Bundle命令demo

    生命周期中关键3个类: BundleActivator 入口点,类似main方法 BundleContext Bundle上下文对象,在执行期间,为应用程序提供操作osgi框架的方法 Bundle 代 ...

  4. osgi实战学习之路:6. Service-1

    什么是Service? 它是注冊到osgi的一个java对象 Service注冊: 通过BundleContext::registerService(java.lang.String[] clazze ...

  5. osgi实战学习之路:1. ant+bnd+felix搭建osgi之HelloWorld

    开发环境分为三个部份 osgi_provider: bundle开发环境,对外提供服务 osgi_consumer: 引用其他bundle osgi_main: 执行測试 项目主要内容 : commo ...

  6. osgi实战学习之路:4.Bundle

    </pre></h1><h1 style="margin:0 0 0 40px; border:none; padding:0px"><p ...

  7. 嵌入式Linux驱动学习之路(十七)驱动程序分层分离概念-平台设备驱动

    平台设备驱动: 包含BUS(总线).DEVICE.DRIVER. DEVICE:硬件相关的代码 DRIVER:比较稳定的代码 BUS有一个driver链表和device链表. ①把device放入bu ...

  8. vb编程学习之路之基础与概念总结

    OOP (Object Oriented Programming)面向对象程序设计/面向对象编程 对象是代码和数据的集合,对象的三要素:属性.事件.方法 对象的命令规则:必须以字母或汉字开头,不能以数 ...

  9. Python3学习之路~8.1 socket概念及参数介绍

    一 socket介绍 TCP/IP 基于TCP/IP协议栈的网络编程是最基本的网络编程方式,主要是使用各种编程语言,利用操作系统提供的套接字网络编程接口,直接开发各种网络应用程序. socket概念 ...

随机推荐

  1. OO alv report

    DATA: gr_alvgrid TYPE REF TO cl_gui_alv_grid ,"ALV对象 gt_fieldcat TYPE lvc_t_fcat , "ALV字段控 ...

  2. uva 1335 - Beijing Guards(二分)

    题目链接:uva 1335 - Beijing Guards 题目大意:有n个人为成一个圈,其中第i个人想要r[i]种不同的礼物,相邻的两个人可以聊天,炫耀自己的礼物.如果两个相邻的人拥有同一种礼物, ...

  3. Error:The SDK Build Tools revision (19.0.3) is too low for project ':app'. Minimum required is 19.1.

    今天更新了一下AndroidStudio, 结果编译程序时报错, 错误如下: Error:The SDK Build Tools revision (19.0.3) is too low for pr ...

  4. Mysql RR隔离更新列没有索引 会锁全表

    <pre name="code" class="html">mysql> show variables like '%tx_isolation ...

  5. C++ 可以多个函数声明

    c/c++可以有多个函数声明,但实现只能有一个 例子: //file t_defs.h #ifndef _T_DEFS_H_ #define _T_DEFS_H_ void say(void); #e ...

  6. 窗体的Alpha通道透明色支持(一旦 Form 被定义为利用 LayeredWindow ,窗口的绘图不再响应沿用多年的 WM_Paint 消息)

    参考: http://www.delphibbs.com/delphibbs/dispq.asp?lid=2190768 Windows 2000后,为了支持类似MAC界面的Alpha通道混合效果,提 ...

  7. html中的table在android端显示

    转载请注明出处:http://blog.csdn.net/u012338845/article/details/46773245 開始都是用Html.fromHtml(source).来显示html的 ...

  8. [Windows Phone]修改应用程序主题

    问题: Windows Phone模拟器默认情况下是黑色(Dark)主题,开发者往往都是在黑色主题下进行应用开发,加入自定义的颜色,样式等等,而当把手机操作系统主题设为白色(Light)主题之后会发现 ...

  9. C语言中scanf/fscanf 的%[]和%n说明符的使用方法

    标准输入输出函数%[]和%n说明符的使用方法     scanf fscanf,均从第一个非空格的可显示字符开始读起!         标准输入输出函数scanf具有相对较多的转换说明符,它常常作为入 ...

  10. 数据结构《17》---- 自己主动补齐之《二》----Ternary Search Tree

    一. 序言 上一篇文章中,给出了 trie 树的一个实现. 能够看到,trie 树有一个巨大的弊病,内存占用过大. 本文给出还有一种数据结构来解决上述问题---- Ternary Search Tre ...