ImageVerifierCode 换一换
格式:DOCX , 页数:30 ,大小:263.34KB ,
资源ID:20736474      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/20736474.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(利用felix和struts2实现osgi webWord文档格式.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

利用felix和struts2实现osgi webWord文档格式.docx

1、通过该示例,可以体现出基于 OSGi 开发的项目具有良好的模块化以及 OSGi 的 bundle 动态部署的能力,从而说明了 OSGi 适用于开发持续运行且需要动态更新的系统。在这个示例中,一共包括五个工程,一个 Web Application 工程和四个 OSGi bundle 工程。Web Application 工程是用于 Web 部署。四个 OSGi bundle 中,包括一个 Web bundle,用于 Web 交互;一个 time service bundle,包含一个获取时间信息的接口服务;一个 local time service bundle,实现接口服务,用于获取本地时间信

2、息;一个 utc time service bundle,用于获取世界标准时间(Universal Time Coordinated,UTC)信息。本示例的结构原理如图 1 所示。在 Web Container 中注册了 Struts OSGi 的监听器,该监听器会去启动或停止 Apache Felix Host,Apache Felix Host 是 Struts OSGi Plugin 和 Felix Framework 的连接点。Felix Host 会去创建和初始化 Felix Framework,Felix Framework 负责管理系统中的其余的所有 bundle,Struts

3、OSGi Plugin 会监听 bundle 的变化,如果发生变化的 bundle 是一个 Struts bundle, 则会去加载 Struts 的配置。图 1. 示例结构原理图建立 OSGi 的 Web 开发环境本文示例使用的 Web 开发环境包括如下组件,部分框架可以 参考资料中下载。 Eclipse 3.5 for Java EE Developers Sun JDK 1.6.0 Tomcat 6.0.24 Struts 2.1.8.1 (Essential Dependencies Only) spring-osgi-1.1.2-with-dependencies (Only Nee

4、d when adding Spring Support)Web Application 工程的创建方式与通常的 Web 工程类似,但是需要加入 Felix 的支持和 Struts2 OSGi Plugin. Felix 是 OSGi 的平台,用于管理整个系统中的所有的 bundle,而 Struts2 OSGi Plugin 是 Struts2 和 OSGi 连接的桥梁,通过 Struts2 OSGi Plugin 将 Felix 融入到 Struts2 框架中。另外,还需要加入 Struts2 OSGi Admin bundle,这个 bundle 向管理人员提供基于 Web 的管理 OS

5、Gi 平台中的 bundle 的操作入口。同时在 web.xml 中需要加入 Struts OSGi 监听器,这样 OSGi 平台中的 bundle 发生变化时,会触发该监听器去做一些与 Struts 相关测操作,例如增加 Action 或使 Action 失效。web.xml 中过滤器和监听器部分的配置内容如清单 1:清单 1. web.xml 过滤器和监听器配置 filter-namestruts2-preparefilter-classorg.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter/filterstruts2-exe

6、cuteurl-pattern/*/filter-mappinglistenerlistener-classorg.apache.struts2.osgi.StrutsOsgiListener/listenerorg.apache.struts2.dispatcher.ng.listener.StrutsListener Web Application 工程的目录结构如图 2 所示:图 2. Web Application 工程的目录结构将 Web Application 部署到 Tomcat 上并启动 Tomcat,然后在浏览器中输入 http:/localhost:8080/webapp/

7、osgi/admin/bundles.action, (webapp 是项目部署到 Tomcat 中的名字 ) 如果看到了类似于 图 5的 bundles 列表,说明 OSGi 环境配置成功。开发获取时间消息接口服务 bundle消息接口服务 bundle 是提供消息服务的接口,该接口将被 Web bundle 所使用,其他 bundle 可以不同的形式实现该接口。在这里利用 eclipse 新建插件工程的功能来创建 OSGi bundle。需要特别设置 an OSGI framework 为 standard 方式,这种方式允许部署项目到标准的 OSGI 容器中。新建 OSGi 工程的向导如

8、图 3 所示。图 3. 新建 OSGi 工程向导图在该项目中开发一个用于获取时间信息的接口,通过该接口可以获取字符串形式的时间信息。清单 2. 获取时间服务接口代码 package com.example.time.service; public interface TimeService public String getTime(); 需要将该 bundle中的服务包的类和接口就暴露给了其他的 bundle,其他的 bundle可以通过 import这个包来使用其中的类和接口。开发获取本地时间消息实现服务 bundle获取本地时间消息服务 bundle 实现了时间消息接口服务。在该 bun

9、dle 种返回的时间消息是当前时区的时间信息。因为用到了接口服务包,所以需要在 Import-Package 中加入接口服务包。清单 3. 获取本地时间实现代码 package com.example.time.local.service; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import com.example.time.service.TimeService; public class LocalTimeService implements TimeServ

10、ice Override public String getTime() Calendar calendar = Calendar.getInstance(); Date date = calendar.getTime(); SimpleDateFormat formatter = new SimpleDateFormat(yyyy-MM-dd HH:mm:ss); return The local time: + formatter.format(date);OSGi bundle 中的服务要能够被其他 bundle 使用,使用将服务发布出来。在该 bundle 的 Activator 的

11、start() 方法中注册该服务,可以发布这个服务。当这个 bundle 启动时,将获取本地时间发布为一个服务。服务发布的代码如清单 4 所示。清单 4. 服务发布 public void start(BundleContext context) throws Exception context.registerService(TimeService.class.getName(), new LocalTimeService(), null);开发获取 UTC 时间消息实现服务 bundle获取 UTC 时间消息实现服务同样实现了时间消息接口服务,该 bundle 主要是用于和上一个 bund

12、le 即获取本地时间消息服务进行动态的替换,用于表现 OSGi 的动态部署的能力。清单 5. 获取 UTC 时间服务实现 public class UTCTimeService implements TimeService public String getTime() int zoneOffset = calendar.get(Calendar.ZONE_OFFSET); int dstOffset = calendar.get(Calendar.DST_OFFSET); calendar.add(Calendar.MILLISECOND, -(zoneOffset + dstOffset)

13、;mm sThe UTC time:开发 Web bundleWeb bundle 是系统 Web 交互的入口。在该 bundle 中需要使用时间消息接口服务 bundle 中的接口,所有需要在 MANIFEST.MF 的 Import-Package 加入时间消息接口服务包,另外,为了能够识别出该 bundle 是一个 Struts bundle, 需要将该 bundle 设置为可被 Struts2 支持 , 即在 MANIFEST.MF 中加入 Struts2-Enabled: true, 这样该 bundle 中的 struts.xml 就会被加载。最终的 MANIFEST.MF 的配置

14、如清单 6。清单 6. Web Bundle 的 MANIFEST.MF 配置 Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: com-example-time-web Bundle-SymbolicName: com.example.time.web Bundle-Version: 1.0.0.qualifier Bundle-Vendor: keki Bundle-RequiredExecutionEnvironment: JavaSE-1.6 Struts2-Enabled: true Import-Package

15、: com.example.time.service, com.opensymphony.xwork2, org.apache.struts2.osgi.interceptor, org.osgi.framework;version=1.3.0为了实现用户交互,还需要创建一个获取时间消息响应的 action。该 Action 的 execute() 方法代码如清单 7 所示。清单 7. Action 实现方法 public String execute() ServiceReference ref = bundleContext.getServiceReference( TimeService

16、.class.getName(); TimeService timeService = (TimeService) bundleContext.getService(ref); timeMessage = timeService.getTime(); return SUCCESS;这个 Web bundle 中独立的建立一个 struts.xml,这个 struts.xml 将会为单独加载,需要注意的是 Struts 的 pacakge 继承 osgi-default 这个包,osgi-default 已在 struts2-osgi-plugin 这个 jar 包里面定义。清单 8. Web

17、Bundle struts.xml 的 action 定义strutspackage name=time-example namespace=/time extends=osgi-defaultaction name=time class=com.example.time.web.action.TimeActionresult type=freemarkertime.ftl/action/package/struts打包部署将开发好的四个 bundle 导出成 plugin 的包,并将它们放在 Web App 工程中 ,bundles 的目录结构如图 4 所示。图 4. Web Applica

18、tion 中的 bundles 目录结构运行演示启动 Tomcat,在浏览器地址栏输入 http:8080/webapp/osgi/admin/bundles.do, 可以看到所系统中所有的 bundle 的列表。图 5. 部署的 bundles 列表在浏览器地址栏输入 http:8080/webapp/time/time.do,可以获得时间信息,此时的时间信息为本地时间信息,当前 TimeService 这个服务有 local time service 和 UTC time service 两个实现,调用的是 local time service 这个实现。图 6. 获取本地时间页面显示此时

19、,在浏览器地址栏输入 http:8080/webapp/osgi/admin/shell.do,然后输入命令 stop 1, 将 Local time service 这个 bundle 停止掉,输入命令 ps, 可以看到 local time service 这个 bundle 的 state 已经变为 Resolved.图 7. OSGi Shell 管理页面在浏览器地址栏再次输入 http:8080/webapp/time/time.do 得到的结果如图 7 所示。图 8. 获取 UTC 时间显示页面通过上面的演示,我们可以看到 OSGi bundle 的动态部署能力。回页首bundle

20、 的管理通过 Felix 可以方便的管理项目中的 bundle,并且实现 bundle 的热部署,即插即用,即删即无的特性,特别适用于可持续运行的系统。添加 bundle输入命令 install ,然后输入 start 即可。如 $install file:/k:/plugins/com.example.time.local_1.0.0.qualifier.jar , $start 7更新 bundle输入命令 update 如$ update 1 file:/plugins/com.example.time.local_1.0.0.qualifier.jar启动和停止 bundle输入命令

21、start 启动 bundle;输入命令 stop 停止 bundle。$ start 2 , $ stop 1卸载 bundle若 bundle 处于 Installed 或 Resolve 状态,则直接输入命令 uninstall 。若 bundle 处于 Actived 状态,则先输入命令 stop 停止 bundle, 再输入命令 uninstall param-namestruts.osgi.runLevelparam-value5/context-param解决 Bundle 中的 struts.xml 的 Struts Configuration DTD 无法定位的问题Strut

22、s.xml 的头部有 Struts Configuration DTD 的引用定义,一般 DTD 文档的 URL 为 http:/struts.apache.org/dtds/XXX.dtd ,示例如下所示:清单 10. struts.xml 头部 dtd!DOCTYPE struts PUBLIC -/Apache Software Foundation/DTD Struts Configuration 2.0/EN/struts.apache.org/dtds/struts-2.0.dtd如果无法连接上 http:/struts.apache.org/,那么在加载 Struts 的 bun

23、dle 时也将会出错,因为 bundle 与 Web Application 的 lib 的加载路径不一致,无法从 Web Application 的 lib 下面找到 XXX.dtd 文件。此时可以通过修改 dtd 文件的 URL 来解决,可以改成一个本地文件系统的 URI,如 file:/c:/webapp/dtds/struts-2.0.dtd,也可以改为本地的 Web 服务器或一个可以连接上的服务器的 URL,如 http:/localhost/dtds/struts-2.0.dtd。如何使用 Spring 进行对象管理Spring DM 使得 Spring 和 OSGi 成为可能,在本文的开发环境中,也可以加入 Spring DM 来管理系统中的对象。首先加入 Spring DM 必要的 jar 包,如清单 11. Sp

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1