OSGI+SpringDM+Hessian

OSGI+SpringDM+Hessian



目录
1 简介 1
1.1 OSGI 1
1.2 SPRINGDM 1
1.3 HESSIAN 1
2 OSGI+SPRINGDM+HESSIAN 1
2.1 环境说明 1
2.2 创建服务组件 2
2.3 WEB配置 2
2.4 服务端实现 3
2.4.1 创建服务接口 3
2.4.2 实现服务接口 3
2.4.3 实现服务配置 3
2.5 客户端实现 4
2.5.1 创建客户端 4
2.5.2 实现客户端配置 5



1 简介
1.1 OSGI
OSGI(Open Service Gateway Initiative):面向Java的动态模型系统(The Dynamic Module System For Java)。

1.2 SpringDM
SpringDM(Spring Dynamic Modules):Spring DM的主要目的就是能够方便地将Spring和OSGI框架结合在一起。

1.3 Hessian
Hessian:Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能. 相比WebService,Hessian更简单、快捷。

2 OSGI+SpringDM+Hessian
2.1 环境说明
Eclipse3.5以上版本:Eclipse开发工具。
SpringDM1.2.1:从Spring网站下载SpringDM包。在dist和lib目录下导入Spring和OSGI组件:
com.springsource.org.aopalliance
org.springframework.aop
org.springframework.beans
org.springframework.context
org.springframework.core
org.springframework.web
org.springframework.web.servlet
com.springsource.javax.servlet
org.springframework.osgi.core
org.springframework.osgi.extender
org.springframework.osgi.io
org.springframework.osgi.web
org.springframework.osgi.web.extender
org.springframework.osgi.jasper.osgi
org.springframework.osgi.jsp-api.osgi
com.springsource.org.apache.commons.logging
org.springframework.osgi.catalina.osgi
org.springframework.osgi.catalina.start.osgi
com.springsource.com.caucho

有些组件在SpringDM中没有,可以到相应的网站上下载。

2.2 创建服务组件
在Eclipse中新建Plug-in Project工程(名为HessianService),该组件需要向外提供web服务需要创建成为web工程,在项目中创建WEB-INF目录,并在该目录下创建web.配置文件web.xml,Spring bean配置文件applicationContext.xml,OSGI servlet服务配置文件osgi-console-servlet.xml。
如下图所示:

[img]http://dl.iteye.com/upload/attachment/613938/49999c87-b452-37d5-b0a9-d4d26a5fd095.png[/img]


2.3 Web配置

<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.osgi.web.context.support.OsgiBundleXmlWebApplicationContext</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>osgi-console</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.osgi.web.context.support.OsgiBundleXmlWebApplicationContext</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>osgi-console</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>


2.4 服务端实现
2.4.1 创建服务接口
package com.hessian.service;

public interface HessianService {

public String getString();

}

2.4.2 实现服务接口
package com.hessian.service.impl;

import com.hessian.service.HessianService;
public class HessianServiceImpl implements HessianService{

@Override
public String getString() {
return "------hessian-service-----";
}
}


2.4.3 实现服务配置
在Spring bean配置文件applicationContext.xml中配置服务实现。
<bean id="hessianServiceImpl" class="com.hessian.service.impl.HessianServiceImpl"></bean>


在OSGI servlet服务配置文件osgi-console-servlet.xml中配置hessian服务。
<bean name="/hessianService" class="org.springframework.remoting.caucho.HessianServiceExporter">  
<property name="service" ref="hessianServiceImpl"/>
<property name="serviceInterface">
<value>
com.hessian.service.HessianService
</value>
</property>
</bean>


2.5 客户端实现
2.5.1 创建客户端
package com.hessian.client;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.hessian.service.HessianService;

public class HessianClient extends AbstractController{
HessianService hessianClientService;
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
System.out.println("hessianService:"+hessianClientService.getString());
return null;
}

public HessianService getHessianClientService() {
return hessianClientService;
}
public void setHessianClientService(HessianService hessianClientService) {
this.hessianClientService = hessianClientService;
}
}


2.5.2 实现客户端配置
在Spring bean配置文件applicationContext.xml中配置Hessian服务调用bean。
<bean id="hessianClientService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">  
<property name="serviceUrl" value="http://localhost:8080/HessianService/hessianService"/>
<property name="serviceInterface" value="com.hessian.service.HessianService"/>
</bean>

在OSGI servlet服务配置文件osgi-console-servlet.xml中配置hessian客户端。
<bean name="/hessianClient" class="com.hessian.client.HessianClient">
<property name="hessianClientService" ref="hessianClientService"></property>
</bean>


2.5.3 测试服务

访问地址:http://localhost:8080/HessianService/hessianClient

服务器输出:hessianService:------hessian-service-----

证明客户端服务服务器端服务成功。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值