`

osgi学习-equinox

    博客分类:
  • osgi
 
阅读更多
刚开始学习时,总是想把osgi放在tomcat运行,然后通过http访问osgi中的bundle服务。

换一种角度,可以把osgi框架当做类似于tomcat的容器,区别在于tomcat运行的是普通的java程序或javaweb程序。osgi框架上运行的就是bundle。

对于习惯开发tomcat上运行的程序的开发人员,学习osgi框架时,需要用有别于普通java程序的开发模式


1.eclipse开发osgi应用



1.eclipse开发osgi应用-equinox
1.1新建plug-in project
若找不到plug-in project,参见附录安装插件








1.2 添加页面
在src下面新建html页面,如webroot/hello.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>a test page</title>
</head>
<body>Hello, This is a test page!</body>
</html>



1.3 添加依赖环境
“Required Plug-ins”栏 添加
javax.servlet包
org.eclipse.equinox.http包

“Imported Packages”栏 添加
org.osgi.service.http包



若没有org.eclipse.equinox.http包,则下载后将org.eclipse.equinox.http*.jar放大eclipse/plugins下,重启eclipse
org.eclipse.equinox.http包下载地址:http://archive.eclipse.org/equinox/drops/R-3.6.2-201102101200/



1.4 注册web页面-编码
在创建plug-in project工程中,生成了Activator,在该类中注册http服务
package firstosgiweb;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.http.HttpService;

public class Activator implements BundleActivator {
	 private ServiceReference serviceReference;
	 private HttpService httpService;
	 private static BundleContext bc;
	
	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		System.out.println("Hello World!!");
		bc = context;
		registerResource();
	}
	
	private void registerResource() {
		try {
			serviceReference = bc.getServiceReference(HttpService.class
					.getName());
			if (serviceReference != null) {
				httpService = (HttpService) bc.getService(serviceReference);
				httpService.registerResources("/demo", "webroot", null);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		System.out.println("Goodbye World!!");
		unregisterResource();
	}
	
	private void unregisterResource() {
		try {
			httpService.unregister("/demo");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}



1.5 配置运行环境并运行
1.5.1 runtime添加bin/




1.5.2 右键项目 配置Run Configuration
新建osgi运行实例


配置bundles标签,选择
javax.servlet
org.eclipse.equinox.http
org.eclipse.osgi
org.eclipse.osgi.services


配置VM Arguments,添加http服务端口:
-Dorg.osgi.service.http.port=8080



1.5.3 运行



访问http://localhost:8080/demo/hello.html


控制台会输出Hello World!! 信息,但可能回报org.osgi.framework.BundleException: Could not find bundle: org.eclipse.equinox.console
这是因为缺少相应的依赖环境,在Run Configurations中设置添加除org.eclipse.osgi以外的,如下4个bundles
org.eclipse.equinox.console 
org.apache.felix.gogo.command 
org.apache.felix.gogo.runtime 
org.apache.felix.gogo.shell 



保存后 右键项目run as-OSGI Framework


osgi>ss
显示的9个bundle即为Run Configuration-bundle标签中添加的9个bundle

其他osgi命令:
stop 3   // 停止bundle,访问页面提示404

start 3   // 启动bundle

之前学习servicemix时,这些命令或多或少接触一些,有种似曾相识的感觉。servicemix集成了osgi framework(Felix)



附录:
安装plug-in开发插件:
You need to install the Plugin Perspective
Usually you can try to get it via:
1. Help->Install New Software
2. "Work With:" -> "--All Available Sites--"
3. type "Plug-in搜索" into the filter box
4. Open "General Purpose Tools" (may be called differently in your eclipse version!)
5. install "Eclipse Plug-in Development Environment"
6. restart!






参考网站:
Explore Eclipse's OSGi console
https://www.ibm.com/developerworks/library/os-ecl-osgiconsole/index.html

利用OSGi开发WEB应用
http://blog.csdn.net/flanet/article/details/7921370

OSGi的几个命令
http://rickqin.blog.51cto.com/1096449/1077222

OSGi笔记-几个原理问题
http://blog.csdn.net/yanical/article/details/6087633

使用 Equinox 开发 OSGi 应用程序
https://www.ibm.com/developerworks/cn/education/opensource/os-eclipse-osgi/index.html
  • 描述: create plug-in project1
  • 大小: 50.9 KB
  • 描述: create plug-in project2
  • 大小: 61.7 KB
  • 描述: create plug-in project3
  • 大小: 45.3 KB
  • 描述: create plug-in project4
  • 大小: 52.8 KB
  • 描述: libeary
  • 大小: 92.6 KB
  • 大小: 81.1 KB
  • 大小: 58.7 KB
  • 大小: 46.8 KB
  • 大小: 124.5 KB
  • 大小: 106.8 KB
  • 大小: 111 KB
  • 大小: 2.7 KB
  • 大小: 29.5 KB
  • 大小: 56.3 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics