`
ryanflyer
  • 浏览: 101039 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

OSGI笔记

 
阅读更多

1. OSGI bundle间的协作方式

类似SOA,每个bundle可以通过BundleContext注册对外提供的服务,同时也可以通过BundleContext来获得需要引用的服务。借助使用bundle的元数据来实现工程之间package的共享。export package和import package。

在OSGI框架中,Service是个实际的概念,只有通过BundleContext注册成Service才能使得一个POJO作为Service在OSGI框架中被使用,同时也只有通过BundleContext来获取发布到框架中的Service,通过Service的方式来实现Bundle之间实例级的依赖,和Import-Package、Require-Bundle不同的地方在于通过Service获取的是其他Bundle中类的实例。

2.bundle的classloader机制

每一个bundle都有自己的classloader


题外话:虚拟机的类加载机制

虚拟机把描述类的数据从Class文件加载到内存,并对数据进行校验、转换解析和初始化,最终形成可以被虚拟机直接使用的Java类型,这就是虚拟机的类加载机制。JVM类加载的双亲委派模型(Parents Delegation Model)。

OSGI没有采用该模型,使用了网状模型。

 

2.OSGI和Spring

使用Spring DM整合OSGI和Spring框架。

Spring-OSGI默认加载解析META-INF/spring目录下的xml文件作为spring bean配置文件.

问题1:如何把Spring中的bean发布为OSGI服务?

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:osgi="http://www.springframework.org/schema/osgi"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans.xsd
                      	http://www.springframework.org/schema/osgi 
                      	http://www.springframework.org/schema/osgi/spring-osgi.xsd">

	<bean id="multiplyImpl" class="org.flyer.multiply.bundle.MultiplyImpl">
	</bean>

	<osgi:service id="multiplyOsgiService" ref="multiplyImpl"
		interface="com.osgi.compute.ComputerInterface">
	</osgi:service>
</beans>
 

 问题2:如何引用OSGI服务?

 

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:osgi="http://www.springframework.org/schema/osgi"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans.xsd
                      	http://www.springframework.org/schema/osgi 
                      	http://www.springframework.org/schema/osgi/spring-osgi.xsd">

	<bean id="computeClient" class="org.flyer.compute.client.bundle.ComputeClient">
		<constructor-arg ref="computeService"></constructor-arg>
	</bean>
	<osgi:reference id="computeService" interface="com.osgi.compute.ComputeInterface"></osgi:reference>
</beans>

 

Spring DM 提供了spring-osgi-extender bundle,该bundle负责为你的应用bundle初始化Spring 的Application context,和Spring中的ContextLoadListener提供的功能类似。

extender bundle异步创建Application context,使用另外一个线程创建。如下图所示:


 

Application context的销毁:



 打包和部署基于Spring的OSGI应用


 

 

  • 大小: 40.4 KB
  • 大小: 16.9 KB
  • 大小: 58.9 KB
  • 大小: 21 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics