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

hessian远程调用及spring中使用hessian

2016-09-26 19:24 387 查看

hessian远程调用及spring中使用hessian

官方文档:

使用方法:http://hessian.caucho.com/#TheServiceAPI

序列化协议:http://hessian.caucho.com/doc/hessian-serialization.html

hessian远程调用

接口API:

public interface BasicAPI {
public String hello();
}


接口API实现:

public class BasicService extends HessianServlet implements BasicAPI {
private String _greeting = "Hello, world";

public void setGreeting(String greeting)
{
_greeting = greeting;
}

public String hello()
{
return _greeting;
}
}


调用方式:

String url = "http://hessian.caucho.com/test/test";

HessianProxyFactory factory = new HessianProxyFactory();
BasicAPI basic = (BasicAPI) factory.create(BasicAPI.class, url);

System.out.println("hello(): " + basic.hello());


另外还需要有servlet定义,这个需要自己查看HessianServlet,官方没给出,这里不说明,因为重点是spring的。

spring中使用hessian

除了上面的接口API及其实现类定义外,需要使用spring来定义。

客户端配置代理类

<bean id="userLocaleService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl" value="http://127.0.0.1:8081/support/serviceUrl" />
<property name="serviceInterface" value="service.UserLocaleService" />
<property name="allowNonSerializable" value="true" />
<property name="hessian2" value="true" />
</bean>


服务端配置:

单独定义一个文件叫,spring-remote-servlet.xml文件

<?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"> <!--userLocaleServiceImp是接口service.UserLocaleService的实现,通过注解@Service定义的-->
<bean name="/userLocaleService" class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="userLocaleServiceImp"/>
<property name="serviceInterface" value="service.UserLocaleService"/>
<property name="allowNonSerializable" value="true"/>
</bean>
</beans>


web.xml配置

<servlet>
<servlet-name>spring-remote</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-remote-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>spring-remote</servlet-name>
<url-pattern>/support/*</url-pattern>
</servlet-mapping>


这样即可,spring只处理 /support/* 的url,访问时只需要使用 /support/userLocaleService 即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring hessian hessian2