Hessian的使用
来源:互联网 发布:欠淘宝贷款1000 编辑:程序博客网 时间:2024/06/03 18:31
Hessian是一种高效简洁的远程调用框架,它采用的是二进制RPC协议(Binary),具有轻量、传输量小、平台无关的特点,特别适合于目前网络带宽比较小的手机网络应用项目。Hessian类似于WebService,不过不使用SOAP协议,而是用Binary RPC协议,相比webservice而言更简单、快捷。它把协议报文封装到http封包中,通过HTTP信道传输。Hessian是通过servlet提供远程服务,完全使用动态代理来实现的,推荐采用面向接口编程,因此,Hessian服务建议通过接口暴露。
下面是一个简单的Hessian服务搭建的配置详情:
前提说明:
项目A:hessian-server 服务项目
项目B:hessian-client 客户端项目
- 注意:若传输信息为实体对象,则实体类对象必须实现Serializable接口
hessian-server项目操作:
1、添加hessian-4.0.37.jar
2、创建业务接口:
public interface HelloService { public String helloWorld(String message); public String byteToString(byte[] data);}
3、实现接口方法:
public class HelloServiceImpl implements HelloService { public String helloWorld(String message) { return "hello," + message; } public String byteToString(byte[] data) { for(byte b : data) { System.out.println(b + " "); } try { return new String(data, "UTF-8"); } catch (UnsupportedEncodingException e) { return null; } }}
4、web.xml中通过Spring进行请求的配置:
<servlet> <servlet-name>hessianServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/conf/hessian-server.xml</param-value> </init-param> <load-on-startup>1</load-on-startup></servlet><servlet-mapping> <servlet-name>hessianServlet</servlet-name> <url-pattern>/hessian/*</url-pattern></servlet-mapping>
5、hessian-server.xml 配置:
<!--接口实现类型--><bean id="helloServiceImpl" class="com.slient.hessian.service.impl.HelloServiceImpl" /><!-- 使用HessianServiceExporter 将普通bean导出成Hessian服务 --><bean name="/service" class="org.springframework.remoting.caucho.HessianServiceExporter"> <property name="service" ref="helloServiceImpl" /> <!-- Hessian服务的接口 --> <property name="serviceInterface" value="com.slient.hessian.service.HelloService" /></bean>
6、将接口输出为.jar文件,并拷贝至客户端项目
hessian-client项目操作:
- 添加hessian-4.0.37.jar
- 添加 接口文件.jar
1、hessian-client.xml(spring配置文件):
<bean id="hessianClient" class="org.springframework.remoting.caucho.HessianProxyFactoryBean"> <property name="serviceUrl"> <value>http://localhost:8090/hessian-server/hessian/service</value> </property> <property name="serviceInterface"> <value>com.slient.hessian.service.HelloService</value> </property></bean>
2、调用测试代码:
public class HessianTest { @Test public void testHessianService() throws MalformedURLException { String url = "http://localhost:8090/hessian-server/hessian"; System.out.println(url); HessianProxyFactory factory = new HessianProxyFactory(); HelloService helloService = (HelloService) factory.create(HelloService.class, url); System.out.println(helloService.helloWorld("tanjin1")); } @Test public void testHessianSpringService() throws UnsupportedEncodingException { ApplicationContext context = new FileSystemXmlApplicationContext("/WebRoot/WEB-INF/conf/hessian-client.xml"); HelloService service = (HelloService) context.getBean("hessianClient"); String str = "我爱北京天安门"; byte[] data = str.getBytes("UTF-8"); for(byte b : data) { System.out.println(b + " "); } System.out.println(service.byteToString(data)); }}
阅读全文
0 0
- hessian技术的使用
- Hessian的使用简介
- hessian的使用
- Hessian的使用
- Hessian的使用介绍
- Hessian的使用
- hessian的简单使用
- Hessian的基本使用
- Spring 的 Hessian 简单使用
- Hessian的使用与介绍
- Hessian协议的使用例子
- hessian远程调用的使用
- Hessian的使用以及理解
- Spring 的Hessian简单使用,快速理解Hessian运行方式
- Hessian使用
- hessian 使用
- Hessian Spring相关使用的简单例子
- Hessian使用(与Spring的集成)
- Android SDK与API版本的对应关系
- Oracle错误,解决“ORA-00054”错误,资源被占用的解决方案
- 为什么开始写博客,怎么战胜拖延症,怎么避免沉迷在一些感兴趣的爱好上
- iOS使用NSMutableAttributedString 实现富文本(不同颜色字体、下划线等)
- android使用javaemail发送邮件常见错误
- Hessian的使用
- G
- 非IE判断
- mysql优化_建表字段类型varchar,char的选择
- 在Unity 5中如何减少Draw Call
- 洛谷 P1782 旅行商的背包(二进制优化下的DP)
- mybatis注解详解
- 【菜鸟福利】Linux入门到精通
- Leetcode文章模板