osgi实战学习之路:3. osgi分层概念及相互合作demo
源码下载
分层:
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的更多相关文章
- osgi实战学习之路:8. Service-3之ServiceTracker
通过ServiceTracker能够对查找的Service进行扩展 以下的demo引入装饰器模式对Service进行日志的扩展 demo: Provider student-manage/Activa ...
- 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 ...
- osgi实战学习之路:5.生命周期及利用命令、装饰者模式实现基于socket交互Bundle命令demo
生命周期中关键3个类: BundleActivator 入口点,类似main方法 BundleContext Bundle上下文对象,在执行期间,为应用程序提供操作osgi框架的方法 Bundle 代 ...
- osgi实战学习之路:6. Service-1
什么是Service? 它是注冊到osgi的一个java对象 Service注冊: 通过BundleContext::registerService(java.lang.String[] clazze ...
- osgi实战学习之路:1. ant+bnd+felix搭建osgi之HelloWorld
开发环境分为三个部份 osgi_provider: bundle开发环境,对外提供服务 osgi_consumer: 引用其他bundle osgi_main: 执行測试 项目主要内容 : commo ...
- osgi实战学习之路:4.Bundle
</pre></h1><h1 style="margin:0 0 0 40px; border:none; padding:0px"><p ...
- 嵌入式Linux驱动学习之路(十七)驱动程序分层分离概念-平台设备驱动
平台设备驱动: 包含BUS(总线).DEVICE.DRIVER. DEVICE:硬件相关的代码 DRIVER:比较稳定的代码 BUS有一个driver链表和device链表. ①把device放入bu ...
- vb编程学习之路之基础与概念总结
OOP (Object Oriented Programming)面向对象程序设计/面向对象编程 对象是代码和数据的集合,对象的三要素:属性.事件.方法 对象的命令规则:必须以字母或汉字开头,不能以数 ...
- Python3学习之路~8.1 socket概念及参数介绍
一 socket介绍 TCP/IP 基于TCP/IP协议栈的网络编程是最基本的网络编程方式,主要是使用各种编程语言,利用操作系统提供的套接字网络编程接口,直接开发各种网络应用程序. socket概念 ...
随机推荐
- php数组转xml的递归实现
原文:php数组转xml的递归实现 PHP中奖数组转为xml的需求是常见的,而且实现方法也有很多种,百度找了一下各种实现方法,但是基本是借组一些组件啥的.我就自己写了一个字符串拼组的方法,支持多维数组 ...
- 设计模式(三)-- 适配器模式(Adapter)
适配器模式(Adapter) 考虑一个记录日志的应用,由于用户对日志记录的要求很高,使得开发人员不能简单地采用一些已有的日志工具或日志框架来满足用户的要求,而需要按照用户的要求重新开发新的日志管理系统 ...
- Java核心技术-高级特性(2)- SoftReference, WeakReference and PhantomReference
Java.lang.ref 是 Java 类库中比较特殊的一个包,它提供了与 Java 垃圾回收器密切相关的引用类.这些引用类对象可以指向其它对象,但它们不同于一般的引用,因为它们的存在并不防碍 Ja ...
- hdu 4715
#include<stdio.h> #include<string.h> int prime[1100000],p[1000000],ans; void pri() { ...
- 辛星PHP教程之yii和ci教程已经写完,望与朋友们交流
记得有个朋友给我说,你写的PHP框架是不是过于少了.我感觉仅仅有一个thinkphp确实不好,于是就又写了下yii和ci的教程,事实上我之前是研究过这两个框架的,因此写起来也还算得心应手吧.估计接下 ...
- Java原型模式之基础
一.是什么? 定义:用原型实例指定创建对象的种类,而且通过拷贝这些原型创建新的对象.(官方定义) 原型模式主要用于对象的复制,它的核心是就是类图中的原型类Prototype. Prototype类须要 ...
- DelphiXE7中创建WebService(服务端+客户端) good
相关资料:http://www.2ccc.com/news/Html/?1507.html DelphiXE7新建WebService具体操作:1.打开“DelphiXE7”->“File”-& ...
- linux内核基础(系统调用,简明)
内核基础(系统调用) 在说系统调用之前.先来说说内核是怎么和我们交互的.或者说是怎么和我们产生交集的. 首先,内核是用来控制硬件的仅仅有内核才干直接控制硬件,所以说内核非常重要,假设内核被控制那么电脑 ...
- Linux Shell 之 我的第一个Shell程序
这里我首先会介绍一个Shell是什么,再介绍我的第一个Shell程序和从中总结的经验. 一.Shell是什么 在说我的这个Shell程序之前,还是先跟大家说说什么是Shell吧,相信Shell这个 ...
- ArcGIS多面体(multipatch)解析——引
多面体(multipatch)结构在ArcGIS数据结构中是与点.线.面平行的一种数据结构,对于ArcGIS三维来说是一个很核心的结构,有了它,ArcGIS平台才可以灵活的描述规则和不规则的三维实体. ...