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概念 ...
 
随机推荐
- HDU4452Running Rabbits(模拟)
			
HDU4452Running Rabbits(模拟) pid=4452" target="_blank" style="">题目链接 题目大意: ...
 - Linux Shell 之 Shell中的函数调用
			
说起函数调用,相信大家也不会陌生,然而对于初学Shell的我来说,Shell中函数调用方式却有点让我不太习惯,自己也走了不少的弯路,因为传递参数时出了一个很“自然”的错误,也让我吃了不少的苦头,所以总 ...
 - UVA 10313(完全背包变形)
			
Problem B Pay the Price Input: standard input Output: standard output Time Limit: 2 seconds Memory L ...
 - 用Python对体积较大的CSV文件进行比较的经验
			
用Python对体积较大的CSV文件进行比较的经验 » 进化的测试 | 进化的测试 用Python对体积较大的CSV文件进行比较的经验 python Add comments 八 032010 ...
 - Oracle12C 怎样导入scott用户
			
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaG9uZ2thbmd3bA==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
 - poj2826(线段相交)
			
传送门:An Easy Problem?! 题意:用两条线段接雨水,雨水是垂直落下的,问我们用给定的两条线段能接到多少水. 分析:看起来很简单,写起来略麻烦,先排除不能接到水的情况: 1. 两条线段不 ...
 - 8月30号周五香港接单ING~~化妆品只加10元!!!!!!
			
8月30号周五香港接单ING~~化妆品只加10元!!!!!! 8月30号周五香港接单ING~~化妆品只加10元!!!!!!
 - jQuery EasyUI API 中文文档 - 布局(Layout)
			
<html> <head> <script src="jquery-easyui/jquery.min.js"></script> ...
 - jquery实现鼠标拖动
			
<html> <head> <script type='text/javascript' src='js/jquery-1.5.1.js'></scr ...
 - 斯坦福ML公开课笔记15—隐含语义索引、神秘值分解、独立成分分析
			
斯坦福ML公开课笔记15 我们在上一篇笔记中讲到了PCA(主成分分析). PCA是一种直接的降维方法.通过求解特征值与特征向量,并选取特征值较大的一些特征向量来达到降维的效果. 本文继续PCA的话题, ...