`
jacally
  • 浏览: 759820 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

在 Spring Web Flow 项目中应用 Hessian 服务

    博客分类:
  • JAVA
阅读更多
   
    原来作的一个项目因为页面跳转比较多,应用了Spring Web Flow 项目作视图的页面流转控制,关于Spring Web Flow的介绍可以参考我的另一篇文章:http://lib.iteye.com/blog/299142,最近需要在项目中引用WebService 远程服务的机制供其他系统调用,比较了一些这方面的开源项目,发现Hessian配置简单,性能优异,决定集成Hessian到这个项目中。
  
    本来Hessaion的配置是比较简单的,很多文章都有介绍,主要是在web.xml中配置Servlet,然后配置ServiceBean就可以了,这方面的文章Google一下会很多,不在这里介绍。我的项目中因为用到了Spring Web Flow,通过org.springframework.web.servlet.DispatcherServlet为 Spring Web Flow 控制器作了定义,与Hessian的servlet造成了冲突,使Hessian不能正常工作。经过研究发现需要通过Spring Web Flow的 UrlHandler 对Hessian的请求分配相应的控制器才可以正确的响应。

    以下Spring Web Flow中介绍的例子集成 Hessian 的例子:

服务端代码:
package samples.hessian;

public interface WebServiceSample {

	public String say(String hello);
	
}


package samples.hessian;

public class WebServiceSampleImpl implements WebServiceSample{

	public String say(String hello) {
		return hello + " world!";
	}

}


修改webmvc-config.xml文件,增加ServiceBean的定义,及 Hessian 对应的控制器:
<?xml version="1.0" encoding="UTF-8"?>
<beans 
  xmlns="http://www.springframework.org/schema/beans" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd"> 
  <bean 
    id="viewResolver" 
	class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
	<property name="viewClass" value="org.springframework.web.servlet.view.JstlView">
	</property> 
	<property name="prefix" value="/WEB-INF/jsp/">
	</property> 
	<property name="suffix" value=".jsp">
	</property> 
  </bean> 
  <bean 
    id="viewMappings" 
	class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <!-- /shopping.do 请求由 flowController 来处理 -->
    <property name="mappings"> 
	  <value> /shopping=flowController </value> 

	  <!-- hessian 测试实例 为Hessian的Url请求分配对应的处理器 -->
	  <value> /webServiceHessianSample=webServiceHessianExporter </value>

	</property> 
	<property name="defaultHandler"> 
	<!-- UrlFilenameViewController 会将 "/index" 这样的请求映射成名为 "index" 的视图 --> 
	  <bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController" /> 
	</property> 
  </bean> 
  <bean 
    id="flowController" 
	class="org.springframework.webflow.mvc.servlet.FlowController"> 
	<property name="flowExecutor" ref="flowExecutor"/> 
  </bean>
  
	<!-- hessian 测试实例 ServiceBean 的定义 -->
	<bean id="webServiceHessianSample" class="samples.hessian.WebServiceSampleImpl"></bean>
	<bean name="webServiceHessianExporter"
		class="org.springframework.remoting.caucho.HessianServiceExporter">
		<property name="service" ref="webServiceHessianSample" />
		<property name="serviceInterface" value="samples.hessian.WebServiceSample" />
	</bean>	
  
</beans>


单元测试代码:
package test.sample;

import java.net.MalformedURLException;

import junit.framework.Assert;
import junit.framework.TestCase;
import cn.org.coral.sample.hessian.WebServiceSample;

import com.caucho.hessian.client.HessianProxyFactory;

public class TestHessian extends TestCase {
	
	public void testSay() throws MalformedURLException {

		String url = "http://localhost:8080/coral/spring/webServiceHessianSample";

		HessianProxyFactory factory = new HessianProxyFactory();
		WebServiceSample sample = (WebServiceSample) factory.create(
				WebServiceSample.class, url);
		
		//System.out.print(sample.say("hello"));
		Assert.assertEquals(sample.say("hello"), "hello world!");
	}
}


另外,需要注意的地方,我使用的是 hessian 版本是hessian-3.1.5.jar,使用最新的有问题,可能api发生了变化,还未仔细研究。hessian 的下载地址是:http://hessian.caucho.com/

例子源代码见附件
分享到:
评论
1 楼 corelengine 2012-07-20  
好文,支持一下!

相关推荐

Global site tag (gtag.js) - Google Analytics