换一种角度,可以把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,参见附录安装插件
![](http://dl2.iteye.com/upload/attachment/0125/3023/c05b90e6-8ad7-34e6-acac-c1524f1a1fec.png)
![](http://dl2.iteye.com/upload/attachment/0125/3025/0f14bb74-7cb2-3125-ba0c-3fb9bbcd45a0.png)
![](http://dl2.iteye.com/upload/attachment/0125/3027/2c393fdd-89db-3f36-95be-1d904659c7e2.png)
![](http://dl2.iteye.com/upload/attachment/0125/3029/94da1728-59bb-33cf-9a2b-c195795efbe1.png)
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包
![](http://dl2.iteye.com/upload/attachment/0125/3338/a8025f78-1461-3459-b33b-62e6c159415b.png)
若没有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/
![](http://dl2.iteye.com/upload/attachment/0125/3340/7b78a4f9-5cb3-37ea-9001-780450b45119.png)
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/
![](http://dl2.iteye.com/upload/attachment/0125/3342/557de5bc-34ae-3404-a2d2-49edeaef6bd0.png)
1.5.2 右键项目 配置Run Configuration
新建osgi运行实例
![](http://dl2.iteye.com/upload/attachment/0125/3344/ab63c9f1-8f55-3f33-8532-82f5ef12d766.png)
配置bundles标签,选择
javax.servlet
org.eclipse.equinox.http
org.eclipse.osgi
org.eclipse.osgi.services
![](http://dl2.iteye.com/upload/attachment/0125/3346/edd57129-3f30-3e7e-ba28-b9a8cf0e17c3.png)
配置VM Arguments,添加http服务端口:
-Dorg.osgi.service.http.port=8080
![](http://dl2.iteye.com/upload/attachment/0125/3350/c62c2d76-b6a2-388c-99b6-eb871c8b79c8.png)
1.5.3 运行
![](http://dl2.iteye.com/upload/attachment/0125/3352/9e33ade1-d7dd-3ad2-8c48-cf68f33f6a34.png)
访问http://localhost:8080/demo/hello.html
![](http://dl2.iteye.com/upload/attachment/0125/3354/41b3c396-8bf3-3e11-ac69-1a55db926af6.png)
控制台会输出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
![](http://dl2.iteye.com/upload/attachment/0125/3362/cf3d524f-ea56-3ed8-b4a0-658b01c4e41c.png)
保存后 右键项目run as-OSGI Framework
![](http://dl2.iteye.com/upload/attachment/0125/3364/82fa3fd2-6ea3-33d3-9aba-a8db7d1d6914.png)
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