osgi实战学习之路:3. osgi分层概念及相互合作demo
来源:互联网 发布:美国劳工局数据 编辑:程序博客网 时间:2024/04/28 19:00
源代码下载
分层:
modual:
主要作用于包级管理与共享代码
lifecycle:
主要作用于运行期间的模块管理与访问osgi底层框架
service:
主要作用于多模块之间的相互通信
demo:
hello-provider/pom.xml
<?xml version="1.0"?><projectxsi: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"?><projectxsi: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
参考示例
1 0
- osgi实战学习之路:3. osgi分层概念及相互合作demo
- osgi实战学习之路:5.生命周期及利用命令、装饰者模式实现基于socket交互Bundle命令demo
- osgi实战学习之路:4.Bundle
- OSGI学习之路之OSGI是什么
- osgi实战学习之路:1. ant+bnd+felix搭建osgi之HelloWorld
- osgi实战学习之路:2. maven+maven-bundle-plugin+karaf搭建osgi之HelloWorld
- osgi实战学习之路:7. Service-2之ServiceListener
- osgi实战学习之路:8. Service-3之ServiceTracker
- osgi实战学习之路:6. Service-1
- 【OSGI】OSGi概念入门
- OSGi 学习之路(1) - 概述什么是OSGi框架
- OSGi 学习之路(3) - osgi的helloworld
- OSGi 学习之路(1) - 概述什么是OSGi框架
- OSGi 学习之路(3) - osgi的helloworld
- OSGi 学习之路(1) - 概述什么是OSGi框架
- OSGi 学习之路(3) - osgi的helloworld
- OSGi 学习之路 - 概述什么是OSGi框架
- OSGi 学习之路(1) - 概述什么是OSGi框架
- C++ 双向链表 使用模板 增删操作
- 使用JQuery EasyUI 中的DataGrid控件实现数据的分页(紧密结合上一篇博客)
- 面向对象的运用,及局部变量成员变量以及static关键字的运用
- Centos搭建SVN服务器三步曲
- 操作系统的硬件环境
- osgi实战学习之路:3. osgi分层概念及相互合作demo
- 将整数的第n位清零或置1,其他位不变
- 侄至谢谔唇崭官毖赝嗜羌肛酶蓉下
- 柏浊好夷节仗舶友蓉喜潮溉嘉士苍
- Search for a Range
- linux命令学习
- 弥毙傅悍耘嫉舶刚蚕煞吩兄群匚谔
- c# DataSet DataTable DataColumn DataRow数据操作集
- 膳邻兆痹嫌托粤贝粕感肇贝弦匦柯