Hessian实例

试着写了一个Hessian的例子,是参考caucho官网上的一个example,很简单,也没什么实际的意义,但足以领会Hessian的用法。

1、建立一个Remote Interface

package com.hessian.test;

public interface MathService {

public int add(int a, int b);

}


2、Service Implementation

package com.hessian.test;

import com.caucho.hessian.server.HessianServlet;

public class HessianMathService extends HessianServlet
implements MathService {

public int add(int a, int b){
return a + b;
}

}

官网上的例子是没有实现MathService接口的,而且也能运行成功,但我觉得有点不合逻辑,不应该将该实现类作为MathService接口暴露给client端。

3、web.xml

<servlet>
<servlet-name>math</servlet-name>
<servlet-class>com.hessian.test.HessianMathService</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>math</servlet-name>
<url-pattern>/hessian/math</url-pattern>
</servlet-mapping>


4、Java client

public class HessianClientTest {
public static void main(String[] args){
String url = "http://localhost:8080/hessiantest/hessian/math";
HessianProxyFactory factory = new HessianProxyFactory();
MathService math = null;
try {
math = (MathService)factory.create(MathService.class, url);
} catch (MalformedURLException e) {
System.out.println("occur exception: " + e);
}
System.out.println("3 + 2 = " + math.add(3, 2));
}
}

使用java实现的client,通过HessianProxyFactory的create即可获取到服务接口。

5、python client

from hessianlib import Hessian

url = "http://localhost:8080/hessiantest/hessian/math"
proxy = Hessian(url)
print "2 + 3 =", proxy.add(2, 3)

使用python实现的client,需加入hessianlib.py。

以上就是一个完整的Hessian实现。

Spring也提供了对Hessian的集成,若使用spring,server端的service实现类则不需实现HessianServlet,使用Spring的DispatcherServlet来配置一个Servlet暴露你的服务。
web.xml

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

还需要在 WEB-INF 目录里创建一个名为 remote-servlet.xml(remote为你配置的servlet名)的应用上下文。
remote-servlet.xml

<bean id="mathService" class=" com.hessian.test.HessianMathService">
</bean>

<bean name="/math"
class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref=" mathService "/>
<property name="serviceInterface" value=" com.hessian.test.MathService "/>
</bean>

server端做以上操作即可。

client端可以延用之前的操作,若使用spring则可通过 HessianProxyFactoryBean在客户端连接服务,在spring的配置中加入:

<bean class="com.hessian.test.HessianClientTest">
<property name="mathService" ref="mathService"/>
</bean>

<bean id=" mathService "
class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl"
value="http://remotehost:8080/hessiantest/remote/math"/>
<property name="serviceInterface" value="com.hessian.test.MathService"/>
</bean>

加入以上的配置后,就可像使用其他的bean一样去操作了。原来实现一个webservice是可以这么简单的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值