Hessian的学习

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("何先生"));
		
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值