OSGI学习笔记(六)

8 篇文章 0 订阅

 

SpringDM初步使用(一)

官方地址http://www.springsource.org/osgi

上面提到了仓库位置:

<repository>
 
<id>spring-maven-milestone</id>
 
<name>Springframework Maven Repository</name>
 
<url>http://maven.springframework.org/milestone</url>
</repository>

 

中文指南下载:http://forever-framework.googlecode.com/files/SpringDM_ZH.rar

先谈哈这篇内容能给你什么,通过springDM简化osgi开发。

通过maveneclipse的结合达到更方便的开发和测试。

谈哈这篇内容会使用到的东西:

apache-maven-3.0.3spring-osgi-1.2.1eclipse-jee-indigo-win32nexus-oss-webapp-1.9.1.1-bundle.zip

下面主要介绍spring-osgi-1.2.1下面的4个例子:

 

 

下面要做的就是把这4个例子正常跑起来。这几个项目需要maven的支持。

 如果直接导入这几个例子很有可能不能正常运行,大部分原因是jar不能够导入进来。所以我自己创建项目,建立pom信息。目录结构如下:

 

 将simple-service-bundle和simple-service-integration-test下面的src复制到项目中。

 配置pom.xml信息,主要注意相应的jar是否引入到项目,我这里自己在nexus创建了一个springdm仓库提供所有的jar。

 simple-service-bundle引入的jar:



 

 simple-service-integration-test引入的jar图:


整个项目很简单,普通的接口,实现类,单元测试,加入了spring进行bean的管理。

只是多了simpleservice-osgi.xml这么一个文件。为什么要分开放,方便做测试。

<osgi:service id="simpleServiceOsgi" ref="simpleService"

interface="org.springframework.osgi.samples.simpleservice.MyService" />

告诉我们这个接口作为osgi里面的一个服务在运行。在simple-service-integration-test里面将看到在osgi环境里怎么获取该服务。

先将simple-service-bundle打包到本地仓库,
 对测试类SimpleServiceBundleTest说明一哈:

public class SimpleServiceBundleTest extends AbstractConfigurableBundleCreatorTests {

	protected String[] getTestBundlesNames() {
		return new String[] {
			"org.springframework.osgi.samples, simple-service-bundle,1.2.1"
		};
	} 
	
	public Resource getTestingFrameworkBundlesConfiguration() {
		return new InputStreamResource(
			AbstractDependencyManagerTests.class.getResourceAsStream("/boot-bundles.properties"));
	}
	
	public void testOSGiStartedOk() {
		assertNotNull(bundleContext); 
	} 
	 
	public void testSimpleServiceExported() {
        ServiceReference ref = bundleContext.getServiceReference(MyService.class.getName());
        assertNotNull("Service Reference is null", ref);
        try {  
            MyService simpleService = (MyService) bundleContext.getService(ref);
            assertNotNull("Cannot find the service", simpleService);
            assertEquals("simple service at your service", simpleService.stringValue());
        } finally {
            bundleContext.ungetService(ref);
        }
	}
	
}

 

  如果要让测试类正常运行,必须先将simple-service-bundle安装在本地仓库,还有就是测试框架必须的jar,从测试运行的路径可以看出来:

startup()里面Resource[] bundleResources = locateBundles();

会加载路径文件:/org/springframework/osgi/test/internal/boot-bundles.properties 

这个文件内容:

 #
# Properties file indicating the boot (or mandatory) bundles that are loaded
# by the testing framework.
#
# Normally, this file should not be edited since it is used by the testing infrastructure.
# Users that want to install bundles before starting a test, should use #bundles() method.
#

#
# format: <groupId,artifactId,version>=+/-15
# - the optional value is used to install/remove bundles if running on JDK >= 1.5
# - see Spring org.springframework.core.JdkVersion for jdk major version codes.

# elements that have to be ignored should star with
# ignore

# Note: inner placeholders are not supported.

#
# common properties
#

# versioning
ignore.backport.version=3.1.0
ignore.junit.version=3.8.2
ignore.log4j.version=1.2.15-SNAPSHOT

ignore.spring.version=2.5.6.SEC01
ignore.spring.osgi.version=1.2.1
ignore.slf4j.version=1.5.0
ignore.asm.version=2.2.3

# groupIds
ignore.spring.groupId=org.springframework
ignore.spring.osgi.groupId=org.springframework.osgi
ignore.slf4j.groupId=org.slf4j

#
# actual libraries
#
# listed in dependency order to ease deployment


# dependencies

# junit
org.junit,com.springsource.junit,${ignore.junit.version}=
# log4j
${ignore.spring.osgi.groupId},log4j.osgi,${ignore.log4j.version}=
# slf4j (commons-logging API)
#${ignore.slf4j.groupId},slf4j-api,${ignore.slf4j.version}=
#${ignore.slf4j.groupId},slf4j-log4j12,${ignore.slf4j.version}=
#${ignore.slf4j.groupId},jcl104-over-slf4j,${ignore.slf4j.version}=
# slf4j (BRITS)
${ignore.slf4j.groupId},com.springsource.slf4j.api,${ignore.slf4j.version}=
${ignore.slf4j.groupId},com.springsource.slf4j.log4j,${ignore.slf4j.version}=
${ignore.slf4j.groupId},com.springsource.slf4j.org.apache.commons.logging,${ignore.slf4j.version}=
# aop alliance
org.aopalliance,com.springsource.org.aopalliance,1.0.0=
# asm
org.objectweb.asm,com.springsource.org.objectweb.asm,${ignore.asm.version}=
# backport concurrent
edu.emory.mathcs.backport,com.springsource.edu.emory.mathcs.backport,${ignore.backport.version}=-15

# spring libs
${ignore.spring.groupId},org.springframework.beans,${ignore.spring.version}=
${ignore.spring.groupId},org.springframework.core,${ignore.spring.version}=
${ignore.spring.groupId},org.springframework.context,${ignore.spring.version}=
${ignore.spring.groupId},org.springframework.aop,${ignore.spring.version}=
${ignore.spring.groupId},org.springframework.test,${ignore.spring.version}=


# spring osgi libs
${ignore.spring.osgi.groupId},spring-osgi-io,${ignore.spring.osgi.version}=
${ignore.spring.osgi.groupId},spring-osgi-core,${ignore.spring.osgi.version}=
${ignore.spring.osgi.groupId},spring-osgi-annotation,${ignore.spring.osgi.version}=+15
${ignore.spring.osgi.groupId},spring-osgi-extender,${ignore.spring.osgi.version}=
${ignore.spring.osgi.groupId},spring-osgi-test,${ignore.spring.osgi.version}=

如果想修改框架默认加载的bundle,只需要重写getTestingFrameworkBundlesConfiguration方法:

这里我就改写了log4j.osgi-1.2.15-SNAPSHOT为log4j.osgi-1.2.15,你甚至可以将你自己的bundle写在里面,就不用在代码里面写了。

 完整的代码地址:http://chenjun-java.googlecode.com/svn/spring-osgi/

 

 下面说哈怎么在eclipse的控制台运行该服务:

1.将spring-osgi-1.2.1下的lib和dist的jar放在eclipse-jee-indigo-SR2-win32\dropins\spring-osgi-1.2.1\eclipse\plugins目录下,然后重启eclipse。

2.新建一个osgi运行配置:



 

 勾上所需bundle然后run:会看到控制台出现如下界面,说明已经成功启动:



 可以尝试输入stop 10和start10试试看效果。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值