当前位置:   article > 正文

hessian序列化区别_hessian序列化为什么快

hessian序列化为什么快

背景:

之前一篇文章介绍了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方式提交到服务器端。

  1. package hessian;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.InputStream;
  4. import java.util.Date;
  5. import org.apache.http.HttpEntity;
  6. import org.apache.http.client.methods.CloseableHttpResponse;
  7. import org.apache.http.client.methods.HttpPost;
  8. import org.apache.http.entity.ByteArrayEntity;
  9. import org.apache.http.entity.ContentType;
  10. import org.apache.http.impl.client.CloseableHttpClient;
  11. import org.apache.http.impl.client.HttpClients;
  12. import com.caucho.hessian.io.Hessian2Input;
  13. import com.caucho.hessian.io.Hessian2Output;
  14. public class Test {
  15. public static String urlName = "http://localhost:8080/springmvc-chapter6/hello";
  16. public static void main(String[] args) throws Throwable {
  17. // 序列化
  18. ByteArrayOutputStream os = new ByteArrayOutputStream();
  19. Hessian2Output h2o = new Hessian2Output(os);
  20. h2o.startMessage();
  21. h2o.writeObject(getPerson());
  22. h2o.writeString("I am client.");
  23. h2o.completeMessage();
  24. h2o.close();
  25. byte[] buffer = os.toByteArray();
  26. os.close();
  27. ByteArrayEntity byteArrayEntity = new ByteArrayEntity(buffer,
  28. ContentType.create("x-application/hessian", "UTF-8"));
  29. CloseableHttpClient client = HttpClients.createDefault();
  30. HttpPost post = new HttpPost(urlName);
  31. post.setEntity(byteArrayEntity);
  32. CloseableHttpResponse response = client.execute(post);
  33. System.out.println("response status:\n"
  34. + response.getStatusLine().getStatusCode());
  35. HttpEntity body = response.getEntity();
  36. System.out.println("body:"+body);
  37. // InputStream is = body.getContent();
  38. // Hessian2Input h2i = new Hessian2Input(is);
  39. // h2i.startMessage();
  40. //
  41. // Person person = (Person) h2i.readObject();
  42. // System.out.println("response:\n" + person.toString());
  43. // System.out.println(h2i.readString());
  44. //
  45. // h2i.completeMessage();
  46. // h2i.close();
  47. // is.close();
  48. }
  49. public static Person getPerson() {
  50. Person person = new Person();
  51. person.setAddress(new String[] { "Beijing", "TaiWan", "GuangZhou" });
  52. person.setBrithday(new Date());
  53. person.setGender(false);
  54. person.setHeight(168.5D);
  55. person.setId(300);
  56. person.setName("Jack");
  57. person.setPhone(188888888);
  58. person.setWeight(55.2F);
  59. return person;
  60. }
  61. }

接受:

  1. package cn.javass.chapter6.web.controller;
  2. import hessian.Person;
  3. import java.io.IOException;
  4. import javax.servlet.ServletInputStream;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import org.springframework.stereotype.Controller;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.servlet.ModelAndView;
  10. import com.caucho.hessian.io.Hessian2Input;
  11. //@RequestMapping
  12. @Controller
  13. public class HelloWorldController {
  14. @RequestMapping(value = "/hello")
  15. public String helloWorld(HttpServletRequest request, HttpServletResponse response) throws IOException {
  16. // 处理请求
  17. ServletInputStream sis = request.getInputStream();
  18. Hessian2Input h2i = new Hessian2Input(sis);
  19. h2i.startMessage();
  20. Person person = (Person) h2i.readObject();
  21. System.out.println("receive:\n" + person.toString());
  22. System.out.println(h2i.readString());
  23. h2i.completeMessage();
  24. h2i.close();
  25. sis.close();
  26. return person.toString();
  27. }
  28. }
  1. package hessian;
  2. import java.io.Serializable;
  3. import java.util.Date;
  4. public class Person implements Serializable {
  5. private static final long serialVersionUID = -1923645274767028479L;
  6. private String[] address;
  7. private Date brithday;
  8. private boolean gender;
  9. private double height;
  10. private int id;
  11. private String name;
  12. private int phone;
  13. private float weight;
  14. public String[] getAddress() {
  15. return address;
  16. }
  17. public void setAddress(String[] address) {
  18. this.address = address;
  19. }
  20. public Date getBrithday() {
  21. return brithday;
  22. }
  23. public void setBrithday(Date brithday) {
  24. this.brithday = brithday;
  25. }
  26. public boolean isGender() {
  27. return gender;
  28. }
  29. public void setGender(boolean gender) {
  30. this.gender = gender;
  31. }
  32. public double getHeight() {
  33. return height;
  34. }
  35. public void setHeight(double height) {
  36. this.height = height;
  37. }
  38. public int getId() {
  39. return id;
  40. }
  41. public void setId(int id) {
  42. this.id = id;
  43. }
  44. public String getName() {
  45. return name;
  46. }
  47. public void setName(String name) {
  48. this.name = name;
  49. }
  50. public int getPhone() {
  51. return phone;
  52. }
  53. public void setPhone(int phone) {
  54. this.phone = phone;
  55. }
  56. public float getWeight() {
  57. return weight;
  58. }
  59. public void setWeight(float weight) {
  60. this.weight = weight;
  61. }
  62. }
运行结果如下:

  1. 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]
  2. 2016-04-22 17:49:23 [http-8080-1] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /hello
  3. 2016-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]
  4. 2016-04-22 17:49:24 [http-8080-1] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'helloWorldController'
  5. receive:
  6. 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]
  7. I am client.
  8. 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]'
  9. 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'
  10. 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]'
  11. 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

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号