您的位置:首页 > 其它

Hessian的学习

2016-06-30 09:39 309 查看
Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能。 相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据。

1.下载相关jar包

<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.33</version>
</dependency>2.编写接口和实现类

IHelloService:
package com.yingze.hessian;

/**
* @ClassName: IHelloService
* @Description: TODO()
* @author liang
* @date 2016年6月29日 上午9:38:47
*
*/
public interface IHelloService {

public String sayHello(String name);

}HelloServiceImpl:
package com.yingze.hessian;

/**
* @ClassName: HelloServiceImpl
* @Description: TODO()
* @author liang
* @date 2016年6月29日 上午9:39:33
*
*/
public class HelloServiceImpl implements IHelloService {

public String sayHello(String name) {
return "hello:"+name;
}

}3.spring-web包里提供的org.springframework.remoting.caucho.HessianServiceExporter类,可以将普通方法导出成hessian服务,配置如下:
hessian-context.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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">

<!--hessian-->
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>

<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

<bean id="helloServiceImpl" class="com.yingze.hessian.HelloServiceImpl"/>

<bean name="/hello" class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="helloServiceImpl"/>
<property name="serviceInterface" value="com.yingze.hessian.IHelloService"/>
</bean>

</beans>web.xml:
<servlet>
<servlet-name>hessian</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:hessian-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hessian</servlet-name>
<url-pattern>/hessian/*</url-pattern>
</servlet-mapping>4.配置客户端信息
hessian-client.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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">

<!--hessian client-->
<bean id="hessianClient" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl">
<value>http://localhost:8080/Struts2EasyuiDemo-web/hessian/hello</value>
</property>
<property name="serviceInterface">
<value>com.yingze.hessian.IHelloService</value>
</property>
</bean>

</beans>5.启动tomcat,运行项目
6.测试的main方法

package com.yingze.hessian;

import java.net.MalformedURLException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.caucho.hessian.client.HessianProxyFactory;

/**
* @ClassName: TestMain
* @Description: TODO()
* @author liang
* @date 2016年6月29日 上午11:10:19
*
*/
public class TestMain {

/**
* @param args
* @throws ClassNotFoundException
* @throws MalformedURLException
*/
public static void main(String[] args) throws MalformedURLException, ClassNotFoundException {
/*String url = "http://localhost:8080/Struts2EasyuiDemo-web/hessian/hello";
HessianProxyFactory f=new HessianProxyFactory();
IHelloService hello=(IHelloService) f.create(IHelloService.class,url);
String str=hello.sayHello("何先生");
System.out.println(str);*/

ApplicationContext context = new ClassPathXmlApplicationContext("hessian-client.xml");
IHelloService hello = (IHelloService) context.getBean("hessianClient");
System.out.println(hello.sayHello("何先生"));

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: