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方式提交到服务器端。

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
@Controller
public 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 /hello
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]
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
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值