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

参考示例

osgi实战学习之路:3. osgi分层概念及相互合作demo

时间: 2024-08-04 00:08:20

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

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

通过ServiceTracker可以对查找的Service进行扩展 下面的demo引入装饰器模式对Service进行日志的扩展 demo: Provider student-manage/Activator.java package com.demo.service; import java.util.Dictionary; import java.util.HashMap; import java.util.Hashtable; import java.util.Map; import org

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

生命周期中关键3个类: BundleActivator 入口点,类似main方法 BundleContext Bundle上下文对象,在运行期间,为应用程序提供操作osgi框架的方法 Bundle 代表一个已安装的Bundle 接口说明: BundleActivator: public interface BundleActivator { public void start(BundleContext context) throws Exception; public void stop(Bu

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

什么是Service? 它是注册到osgi的一个java对象 Service注册: 通过BundleContext::registerService(java.lang.String[] clazzes, java.lang.Object service, java.util.Dictionary properties)  Service查找及使用: 通过BundleContext::getServiceReference(java.lang.String clazz),返回ServiceRef

osgi实战学习之路:7. Service-2之ServiceListener

ServiceListener三种状态: ServiceEvent.REGISTERED ServiceEvent.MODIFIED ServiceEvent.UNREGISTERING 基于ServiceListener实现服务查找的demo Provider student-manage/Activator.java package com.demo.service; import java.util.Dictionary; import java.util.HashMap; import

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://karaf.apache.org/ 配置本地仓库: 参考:http://blog.csdn.net/wobendiankun/article/details/25333113 启动karaf: karaf_home/bin/karaf.bat 启动成功如下: 安装 mvn-hello-provider 到本地仓

osgi实战学习之路:4.Bundle

源代码下载 Bundle是什么? Bundle是一个标准的jar,只是在META-INF/MANIFEST.MF中加入Bundle元数据的描述 Bundle元数据 标识 Bundle-ManifestVersion: 2 Bundle-Name: student-model Bundle-SymbolicName: com.demo.student-model Bundle-Version: 0.0.1.SNAPSHOT Export-Package 导出Bundle的某些包中的代码让其它Bun

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

OOP (Object Oriented Programming)面向对象程序设计/面向对象编程 对象是代码和数据的集合,对象的三要素:属性.事件.方法 对象的命令规则:必须以字母或汉字开头,不能以数字开头,的字符串组成,其长度<=127 属性:对象的属性用来表示对象的状态以及控制对象的外观和行为.属性的设置方法,[<对象名>.]<属性名>=<属性值> 事件:对象的事件是指由系统预先设置好的,能够被对象所识别和响应的动作,对象的事件分为三种 (1)用户事件    

OSGi 学习之路 - 概述什么是OSGi框架

现在越来越多的Java开发人员在谈论OSGi是有其道理的.在几年前上学的时候我进行了比较多的Eclipse插件开发,当时就亲身感觉到Eclipse插件体系的灵活与强大,而该体系与OSGi也可谓一脉相承.同气连枝.最近打算重新系统的研究一下OSGi,一边学习一边写一个OSGi探秘系列作为总结,和大家分享,也请大家多给评价和建议. 在这一篇里面,我想先回答一个基本的问题:什么是OSGi?其实如果要很好地回答这个问题,也许先明白是什么导致了OSGi的产生和发展会有些帮助. 相信每个Java程序员都知道

OSGI实战第一章

第一章         解开OSGI的面纱 OSGI是什么?是Java平台的一个模块化层. 模块化:软件应用程序的代码被分割为表示独立内容的逻辑单元,可简化开发,可通过强化逻辑模块的界限来提高可维护性. Java模块化的不足 a)         Java使用访问修饰符(如public.protected.private和包级私有),解决底层面向对象封装,而不是逻辑系统划分. 比如,如果需要代码在多个包之间课件,那么包内的代码必须声明为public,那么所有的模块都可以使用这个public类.这