hessian序列化区别
来源:互联网 发布:p图卖萌软件 编辑:程序博客网 时间:2024/05/16 12:45
背景:
之前一篇文章介绍了java的序列化,http://blog.csdn.net/bohu83/article/details/51124079
在java的序列化里面也是介绍rpc框架时候,在远程调用中,需要把参数和返回值通过网络传输,这个使用就要用到序列化将对象转变成字节流,从一端到另一端之后再反序列化回来变成对象。作用了说了,今天在看文章的时候发现别人贴出效率对比,hessian比java的序列化高出很多。特意补充下相关知识点。
百科:Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能。 相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据。
hessian序列化
hessian序列化比Java序列化高效很多,而且生成的字节流也要短很多。
测试的demo:客户端通过Hessian序列号协议序列化对象,通过Http Post方式提交到服务器端。
package hessian;import java.io.ByteArrayOutputStream;import java.io.InputStream;import java.util.Date;import org.apache.http.HttpEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.ByteArrayEntity;import org.apache.http.entity.ContentType;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients; import com.caucho.hessian.io.Hessian2Input;import com.caucho.hessian.io.Hessian2Output; public class Test { public static String urlName = "http://localhost:8080/springmvc-chapter6/hello"; public static void main(String[] args) throws Throwable { // 序列化 ByteArrayOutputStream os = new ByteArrayOutputStream(); Hessian2Output h2o = new Hessian2Output(os); h2o.startMessage(); h2o.writeObject(getPerson()); h2o.writeString("I am client."); h2o.completeMessage(); h2o.close(); byte[] buffer = os.toByteArray(); os.close(); ByteArrayEntity byteArrayEntity = new ByteArrayEntity(buffer, ContentType.create("x-application/hessian", "UTF-8")); CloseableHttpClient client = HttpClients.createDefault(); HttpPost post = new HttpPost(urlName); post.setEntity(byteArrayEntity); CloseableHttpResponse response = client.execute(post); System.out.println("response status:\n" + response.getStatusLine().getStatusCode()); HttpEntity body = response.getEntity(); System.out.println("body:"+body);// InputStream is = body.getContent();// Hessian2Input h2i = new Hessian2Input(is);// h2i.startMessage();// // Person person = (Person) h2i.readObject();// System.out.println("response:\n" + person.toString());// System.out.println(h2i.readString());// // h2i.completeMessage();// h2i.close();// is.close(); } public static Person getPerson() { Person person = new Person(); person.setAddress(new String[] { "Beijing", "TaiWan", "GuangZhou" }); person.setBrithday(new Date()); person.setGender(false); person.setHeight(168.5D); person.setId(300); person.setName("Jack"); person.setPhone(188888888); person.setWeight(55.2F); return person; }}
接受:
package cn.javass.chapter6.web.controller;import hessian.Person;import java.io.IOException;import javax.servlet.ServletInputStream;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;import com.caucho.hessian.io.Hessian2Input;//@RequestMapping@Controllerpublic class HelloWorldController { @RequestMapping(value = "/hello") public String helloWorld(HttpServletRequest request, HttpServletResponse response) throws IOException { // 处理请求 ServletInputStream sis = request.getInputStream(); Hessian2Input h2i = new Hessian2Input(sis); h2i.startMessage(); Person person = (Person) h2i.readObject(); System.out.println("receive:\n" + person.toString()); System.out.println(h2i.readString()); h2i.completeMessage(); h2i.close(); sis.close(); return person.toString();}}
package hessian;import java.io.Serializable;import java.util.Date;public class Person implements Serializable {private static final long serialVersionUID = -1923645274767028479L;private String[] address;private Date brithday; private boolean gender;private double height;private int id;private String name;private int phone;private float weight;public String[] getAddress() {return address;}public void setAddress(String[] address) {this.address = address;}public Date getBrithday() {return brithday;}public void setBrithday(Date brithday) {this.brithday = brithday;}public boolean isGender() {return gender;}public void setGender(boolean gender) {this.gender = gender;}public double getHeight() {return height;}public void setHeight(double height) {this.height = height;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getPhone() {return phone;}public void setPhone(int phone) {this.phone = phone;}public float getWeight() {return weight;}public void setWeight(float weight) {this.weight = weight;}}运行结果如下:
2016-04-22 17:49:23 [http-8080-1] DEBUG org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'chapter6' processing POST request for [/springmvc-chapter6/hello]2016-04-22 17:49:23 [http-8080-1] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /hello2016-04-22 17:49:24 [http-8080-1] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Returning handler method [public java.lang.String cn.javass.chapter6.web.controller.HelloWorldController.helloWorld(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) throws java.io.IOException]2016-04-22 17:49:24 [http-8080-1] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'helloWorldController'receive:Person [address=[Beijing, TaiWan, GuangZhou], brithday=Fri Apr 22 17:49:23 CST 2016, gender=false, height=168.5, id=300, name=Jack, phone=188888888, weight=55.2]I am client.2016-04-22 17:49:26 [http-8080-1] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'Person [address=[Beijing, TaiWan, GuangZhou], brithday=Fri Apr 22 17:49:23 CST 2016, gender=false, height=168.5, id=300, name=Jack, phone=188888888, weight=55.2]'2016-04-22 17:49:26 [http-8080-1] DEBUG org.springframework.web.servlet.DispatcherServlet - Rendering view [org.springframework.web.servlet.view.JstlView: name 'Person [address=[Beijing, TaiWan, GuangZhou], brithday=Fri Apr 22 17:49:23 CST 2016, gender=false, height=168.5, id=300, name=Jack, phone=188888888, weight=55.2]'; URL [/WEB-INF/jsp/Person [address=[Beijing, TaiWan, GuangZhou], brithday=Fri Apr 22 17:49:23 CST 2016, gender=false, height=168.5, id=300, name=Jack, phone=188888888, weight=55.2].jsp]] in DispatcherServlet with name 'chapter6'2016-04-22 17:49:26 [http-8080-1] DEBUG org.springframework.web.servlet.view.JstlView - Forwarding to resource [/WEB-INF/jsp/Person [address=[Beijing, TaiWan, GuangZhou], brithday=Fri Apr 22 17:49:23 CST 2016, gender=false, height=168.5, id=300, name=Jack, phone=188888888, weight=55.2].jsp] in InternalResourceView 'Person [address=[Beijing, TaiWan, GuangZhou], brithday=Fri Apr 22 17:49:23 CST 2016, gender=false, height=168.5, id=300, name=Jack, phone=188888888, weight=55.2]'2016-04-22 17:49:26 [http-8080-1] DEBUG org.springframework.web.servlet.DispatcherServlet - Successfully completed request
1、序列化对象要实现java.io.Serializable接口。
2、单文本传输没必要使用hessian,pojo适合用hessian,但是要注意序列化的规范,可能特殊类型支持的不够完善。
本文是是初步使用,背后的原理待整理。
参考:
http://aiilive.blog.51cto.com/1925756/1601574
0 0
- hessian序列化区别
- Java序列化和hessian序列化的区别
- Java序列化和Hessian序列化的区别
- Java序列化和hessian序列化的区别
- Java序列化和hessian序列化的区别
- Java序列化和Hessian序列化的区别
- Java序列化和hessian序列化的区别
- hessian序列化
- Hessian序列化详解
- Lucene 实现 hessian序列化
- hessian 序列化实现 初探
- Hessian 序列化,反序列
- hessian序列化反序列化util
- Hessian远程调用及序列化协议
- Hessian远程调用及序列化协议
- Kryo与Hessian的序列化比较
- Hessian 2.0序列化协议规范
- Hessian 2.0序列化协议规范
- C++第4次实验—输出星号图
- Cocoa Touch Layer
- C语言中的break、continue和goto三者的区别与用法
- on事件触发元素先后顺序
- Android中最简单的数据存储方式:SharedPreferences
- hessian序列化区别
- HDOJ1864(水的可怜)
- 决策树
- iOS巅峰之MVC(设计模式)详解
- gtest安装和使用
- DI 获取系统时间 用sql脚本插入数据库
- Android 比Timer更好方法
- 在分类中如何处理训练集中不平衡问题
- 计算机视觉、机器学习相关领域论文和源代码大集合