您的位置:首页 > 编程语言 > Java开发

Spring 的Hessian简单使用,快速理解Hessian运行方式

2015-02-02 13:50 309 查看
Spring 的 Hessian 简单使用
一、 服务器端配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet>
<servlet-name>remote</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>remote</servlet-name>
<url-pattern>/remote/*</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

配置好Servlet 和 servlet mapping的DispatcherServlet
然后再在 WEB-INF 下 创建 Spring 初始化 HessianService配置,如下:
该文件名一定是: 那个servlet名+“-servlet.xml” 这里我们创建”remote-servlet.xml”文件,内容如下:

<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="timeService" class="com.sosee.TimeServiceImpl"/>

<bean name="/HessianService"class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="timeService"/>
<property name="serviceInterface" value="com.sosee.TimeService" />
</bean>
</beans>
2.创建接口和实现类
接口 :
com.sosee.TimeService
package com.sosee;
public interface TimeService{
String getTime();
}
实现类:
package com.sosee;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TimeServiceImpl implements TimeService
{
public String getTime() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
}
}

OK,以上将服务器端的所有准备工作都搞定了,接下来就是客户端如何调用了。
二、 客户端Spring配置以及代码调用
1. 配置Spring 容器
<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean name="hessianService"class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl"value="http://localhost:8080/call/remote/HessianService" />
<property name="serviceInterface" value="com.sosee.TimeService" />
</bean></beans>

2. 调用代码:

package com.sosee;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[]
args){
ApplicationContext context = newClassPathXmlApplicationContext("applicationContext.xml");
TimeService service = (TimeService)context.getBean("hessianService");
System.out.println("get systime:"+service.getTime()); }
}

OK,测试成功.

有可能你初次做这个测试的时候,有出现以下的异常情况:
Exception in thread "main" org.springframework.remoting.RemoteAccessException: Cannot access Hessian remote service at [http://localhost:8080/call2/remote/HessianService];
nested exception is com.caucho.hessian.io.HessianProtocolException: 400:java.io.IOException: Server returned HTTP response code: 400
for URL: http://localhost:8080/call2/remote/HessianService at org.springframework.remoting.caucho.HessianClientInterceptor.convertHessianAccessException(HessianClientInterceptor.java:254)
at org.springframework.remoting.caucho.HessianClientInterceptor.invoke(HessianClientInterceptor.java:225)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy1.getTime(Unknown Source)
at com.sosee.Main.main(Main.java:14)
Caused by: com.caucho.hessian.io.HessianProtocolException: 400: java.io.IOException:
Server returned HTTP response code: 400 for URL: http://localhost:8080/call2/remote/HessianService at com.caucho.hessian.client.HessianProxy.invoke(HessianProxy.java:185)

这种异常一般都是由于 Spring配置文件中 的
<property name="serviceUrl"value="http://localhost:8080/call/remote/HessianService" />
(我这里配置的call 是webroot的名称,你如果直接放到服务器的根目录,就不需要这个了;remote/为servlet的定义规则)
出现的问题,一定把URL写正确就OK了!
另外需要注意的就是spring.jar的版本问题了,Srping版本更新较快,我这个用的2.0.7版本。

原博客地址:http://blog.csdn.net/jakemanse/article/details/4218322
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: