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("何先生"));
}
}
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("何先生"));
}
}
相关文章推荐
- 数据库设计案例分析
- 跟我学android之四 事件驱动模型
- 跟我学android之四 事件驱动模型
- 单纯觉得是篇好文——跨终端Web之Hybrid App
- 实现OC与JS的交互
- spring mvc interceptor
- 控制app字体大小不随手机字体大小影响
- 青蛙的烦恼
- 视频格式基础知识:让你了解MKV、MP4、H.265、码率\码流、多码流等等
- [置顶] Android-PullToRefresh上拉下拉刷新加载更多,以及gridview刷新功能的Library下载地址
- ugui基础 button控件(一)
- 工作队列
- Java中Set,List,Map的区别
- Matlab 进行两个体数据的配准 并显示配准误差 彩色化
- 浅谈web应用的负载均衡、集群、高可用(HA)解决方案
- 二叉排序树
- 在Windows系统下,搭建最简单的nginx服务器
- PHP整数转小数
- rpl 低功耗路由的实现 论文
- qt设置登录按钮点击后不可用变灰的立即执行