1.OSGi的概述:

1.1 什么是OSGiOSGi(Opean Service Gateway Initiative)是OSGi联盟发布的一组基于Java语言的服务(业务)规范

该规范和核心部分是一个框架,其中定义了应用程序的生命周期模式和服务注册

应用程序无需重新引导就可以远程安装,启动,升级和卸载

1.2 OSGi体系结构

1.3 OSGi开源框架

1.Equinox项目

2.Knopflerfish框架

3.Felix框架

 

2.OSGi与SCA的关系

2.1 OSGi与SCA的区别:

1.出发点不同:

SCA:解决企业之间的调用

OSGi:为了移动设备,考虑运行时框架和服务在运行时刻的动态匹配

 

2.所关注的目标不同

SCA;关注服务的使用和实现

OSGi:关注内部的模块化

 

3.面向服务的不同

OSGi技术上讲是细粒度

SCA业务上讲是粗粒度

 

4.面向的技术不同

SCA的实现可以是JAVA,BPEL,EJB,Web Service

而OSGi实现关注的是OSGi

 

3.2 OSGi与SCA结合的架构

 

SCA容器在OSGi容器中运行,SCA容器实现为OSGi的一个Bundle

 

 

 

3.OSGi在实际中的应用

3.1 OSGi与Eclipse和Equinox

Eclispse实现了OSGi框架,称为Equinox

 

3.2 OSGi在Spring上的应用

 

 

3.3 Eclipse下的OSGi开发样例-HelloWorld实例

 Eclipse3.1 版本org.eclipse.osgi_3.2.0.v20060510.jar
就是OSGi的一个实现

 

1.新建OSGi项目

File--》new--》plug-in Development--》plug-in Project

如果没有特别的设计可以忽略Provider和Classpath

不使用任何模版所以不用选择默认选项

创建完成后会创建相同路径的JAVAPACKAGE,其中包含了Activator

插件信息都在MANIFEST.MF文件中

2. 编辑代码:

输入欢迎信息“:

 1 package osgihelloworld;
 2 
 3 import org.osgi.framework.BundleActivator;
 4 import org.osgi.framework.BundleContext;
 5 
 6 public class Activator implements BundleActivator {
 7 
 8     private static BundleContext context;
 9 
10     static BundleContext getContext() {
11         return context;
12     }
13 
14     /*
15      * (non-Javadoc)
16      * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
17      */
18     public void start(BundleContext bundleContext) throws Exception {
19         System.out.println("Hello World");
20     }
21 
22     /*
23      * (non-Javadoc)
24      * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
25      */
26     public void stop(BundleContext bundleContext) throws Exception {
27         Activator.context = null;
28     }
29 
30 }

3. 运行项目:

右边有运行环境名字,default start level和选择依赖性插件

目前不需要第三方插件,只需选择org.eclipse.osgi插件

只有点击Validate Bundles

提示无问题,表明运行环境搭建成功

OSGi控制台输入HelloWorld

4.OSGi控制台

键入ss,可以查看bundle的状态

5.导出应用程序

右键Export--》Plug-in Development--》Development plug-ins and fragments

posted on 2016-03-13 23:02  Sharpest  阅读(341)  评论(0)    收藏  举报