`
xmong
  • 浏览: 258900 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

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。
如下图所示:




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-----

证明客户端服务服务器端服务成功。


  • 大小: 6.5 KB
分享到:
评论
2 楼 xmong 2013-01-23  
deeplyloving 写道
请问楼主访问路径是什么?


这几天忙项目,没怎么看博客,加了一下访问路径可见 “2.5.3 测试服务 ”。
顺便找了一下,将项目的所有插件都上共享了。
1 楼 deeplyloving 2013-01-07  
请问楼主访问路径是什么?

相关推荐

Global site tag (gtag.js) - Google Analytics