- 浏览: 732744 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
luger:
谢谢 解决了我的问题
spring import jar中的bean配置文件 -
diamondy:
sunny80062951414 写道没有fieldinfo信 ...
利用ASM和Javassist动态生成Class 类(set和get) -
sunny80062951414:
没有fieldinfo信息啊。
利用ASM和Javassist动态生成Class 类(set和get) -
somefuture:
求解!求思路!完全迷茫中。
珍藏经典面试题目一个 -
xiao1227372602:
lionbule 写道xiao1227372602 写道请问下 ...
Linux 命令 alternatives和update-alternatives
1、概念介绍
把Java对象转换为字节序列的过程称为对象的序列化。
把字节序列恢复为Java对象的过程称为对象的反序列化。
对象的序列化主要有两种用途:
1) 数据介质存储
2)
数据网络传输
2、对象序列化实例
为了更好的理解hessian的序列化机制,所以把java和hessian的对象序列化实例都一一列出。
1)对象序列化--java
public byte[] serialize(Object obj) throws Exception { if(obj==null) throw new NullPointerException(); ByteArrayOutputStream os = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(os); out.writeObject(obj); return os.toByteArray(); } public Object deserialize(byte[] by) throws Exception { if(by==null) throw new NullPointerException(); ByteArrayInputStream is = new ByteArrayInputStream(by); ObjectInputStream in = new ObjectInputStream(is); return in.readObject(); }
2)对象序列化--hessian (hessian2的序列化方式在附件中)
public byte[] serialize(Object obj) throws IOException{ if(obj==null) throw new NullPointerException(); ByteArrayOutputStream os = new ByteArrayOutputStream(); HessianOutput ho = new HessianOutput(os); ho.writeObject(obj); return os.toByteArray(); } public Object deserialize(byte[] by) throws IOException{ if(by==null) throw new NullPointerException(); ByteArrayInputStream is = new ByteArrayInputStream(by); HessianInput hi = new HessianInput(is); return hi.readObject(); }
从以上代码不难看出,对象序列化的过程为:
先将对象转为字节码或其它,然后再将其还原为对象。在反序列化时,内存中必须有源对象的所属类。
3、对象序列化效率
hessian2在这方面有了很大的改进,所以优势十分明显。具体细节不再详述,在进阶篇中,我会详述序列化的实现细节。在此仅把实例执行结果公布出来:
java: 77 stxm hessian: 41 stxm hessian2: 30 stxm 说明: 1、数字为对象序列化后的字节长度。 2、‘stxm’为对象方法的执行结果。
hessian2的优点,谁用谁知道。待我再做深入研究之后,再把我自己认为的和大家认为的优点总结一下,并加以解释。
评论
6 楼
lionbule
2011-11-23
请参考如下性能测试结果:
http://code.google.com/p/thrift-protobuf-compare/wiki/Benchmarking
http://code.google.com/p/thrift-protobuf-compare/wiki/Benchmarking
5 楼
gaopengxiang417
2011-11-20
dongbiying 写道
我测试怎么java的序列化时间比hessian的少呢,
你测试的是对象吗 ?
public static void main(String[] args) throws IOException {
//hessian
Long startLong = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
byte[] bytes = javaSerialize(new User("dby"+i));
}
System.out.println("-----java--"+(System.currentTimeMillis()-startLong));
startLong = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
byte[] bytes = serialize2(new User("dby"+i));
}
System.out.println("-----hessian2--"+(System.currentTimeMillis()-startLong));
startLong = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
byte[] bytes = serialize(new User("dby"+i));
}
System.out.println("-----hessian--"+(System.currentTimeMillis()-startLong));
}
public static byte[] serialize(Object obj) throws IOException{
if(obj==null) throw new NullPointerException();
ByteArrayOutputStream os = new ByteArrayOutputStream();
HessianOutput ho = new HessianOutput(os);
ho.writeObject(obj);
return os.toByteArray();
}
public static Object deserialize(byte[] by) throws IOException{
if(by==null) throw new NullPointerException();
ByteArrayInputStream is = new ByteArrayInputStream(by);
HessianInput hi = new HessianInput(is);
return hi.readObject();
}
public static byte[] serialize2(Object obj) throws IOException{
if(obj==null) throw new NullPointerException();
ByteArrayOutputStream os = new ByteArrayOutputStream();
Hessian2Output ho = new Hessian2Output(os);
ho.writeObject(obj);
return os.toByteArray();
}
public static Object deserialize2(byte[] by) throws IOException{
if(by==null) throw new NullPointerException();
ByteArrayInputStream is = new ByteArrayInputStream(by);
Hessian2Input hi = new Hessian2Input(is);
return hi.readObject();
}
public static byte[] javaSerialize(Object obj) throws IOException{
if(obj==null) throw new NullPointerException();
ByteArrayOutputStream os = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(obj);
return os.toByteArray();
}
public static Object javaDeserialize(byte[] by) throws IOException, ClassNotFoundException{
if(by==null) throw new NullPointerException();
ByteArrayInputStream is = new ByteArrayInputStream(by);
ObjectInputStream in = new ObjectInputStream(is);
return in.readObject();
}
这是我的测试代码 ,请指教呀!
你测试的是对象吗 ?
public static void main(String[] args) throws IOException {
//hessian
Long startLong = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
byte[] bytes = javaSerialize(new User("dby"+i));
}
System.out.println("-----java--"+(System.currentTimeMillis()-startLong));
startLong = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
byte[] bytes = serialize2(new User("dby"+i));
}
System.out.println("-----hessian2--"+(System.currentTimeMillis()-startLong));
startLong = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
byte[] bytes = serialize(new User("dby"+i));
}
System.out.println("-----hessian--"+(System.currentTimeMillis()-startLong));
}
public static byte[] serialize(Object obj) throws IOException{
if(obj==null) throw new NullPointerException();
ByteArrayOutputStream os = new ByteArrayOutputStream();
HessianOutput ho = new HessianOutput(os);
ho.writeObject(obj);
return os.toByteArray();
}
public static Object deserialize(byte[] by) throws IOException{
if(by==null) throw new NullPointerException();
ByteArrayInputStream is = new ByteArrayInputStream(by);
HessianInput hi = new HessianInput(is);
return hi.readObject();
}
public static byte[] serialize2(Object obj) throws IOException{
if(obj==null) throw new NullPointerException();
ByteArrayOutputStream os = new ByteArrayOutputStream();
Hessian2Output ho = new Hessian2Output(os);
ho.writeObject(obj);
return os.toByteArray();
}
public static Object deserialize2(byte[] by) throws IOException{
if(by==null) throw new NullPointerException();
ByteArrayInputStream is = new ByteArrayInputStream(by);
Hessian2Input hi = new Hessian2Input(is);
return hi.readObject();
}
public static byte[] javaSerialize(Object obj) throws IOException{
if(obj==null) throw new NullPointerException();
ByteArrayOutputStream os = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(obj);
return os.toByteArray();
}
public static Object javaDeserialize(byte[] by) throws IOException, ClassNotFoundException{
if(by==null) throw new NullPointerException();
ByteArrayInputStream is = new ByteArrayInputStream(by);
ObjectInputStream in = new ObjectInputStream(is);
return in.readObject();
}
这是我的测试代码 ,请指教呀!
hession不是说序列化的时候时间减少,而是序列化以后在网络上传输的时候能够减少时间(因为流中的字节数减少了一半).
4 楼
dongbiying
2011-11-16
我测试怎么java的序列化时间比hessian的少呢,
你测试的是对象吗 ?
public static void main(String[] args) throws IOException {
//hessian
Long startLong = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
byte[] bytes = javaSerialize(new User("dby"+i));
}
System.out.println("-----java--"+(System.currentTimeMillis()-startLong));
startLong = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
byte[] bytes = serialize2(new User("dby"+i));
}
System.out.println("-----hessian2--"+(System.currentTimeMillis()-startLong));
startLong = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
byte[] bytes = serialize(new User("dby"+i));
}
System.out.println("-----hessian--"+(System.currentTimeMillis()-startLong));
}
public static byte[] serialize(Object obj) throws IOException{
if(obj==null) throw new NullPointerException();
ByteArrayOutputStream os = new ByteArrayOutputStream();
HessianOutput ho = new HessianOutput(os);
ho.writeObject(obj);
return os.toByteArray();
}
public static Object deserialize(byte[] by) throws IOException{
if(by==null) throw new NullPointerException();
ByteArrayInputStream is = new ByteArrayInputStream(by);
HessianInput hi = new HessianInput(is);
return hi.readObject();
}
public static byte[] serialize2(Object obj) throws IOException{
if(obj==null) throw new NullPointerException();
ByteArrayOutputStream os = new ByteArrayOutputStream();
Hessian2Output ho = new Hessian2Output(os);
ho.writeObject(obj);
return os.toByteArray();
}
public static Object deserialize2(byte[] by) throws IOException{
if(by==null) throw new NullPointerException();
ByteArrayInputStream is = new ByteArrayInputStream(by);
Hessian2Input hi = new Hessian2Input(is);
return hi.readObject();
}
public static byte[] javaSerialize(Object obj) throws IOException{
if(obj==null) throw new NullPointerException();
ByteArrayOutputStream os = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(obj);
return os.toByteArray();
}
public static Object javaDeserialize(byte[] by) throws IOException, ClassNotFoundException{
if(by==null) throw new NullPointerException();
ByteArrayInputStream is = new ByteArrayInputStream(by);
ObjectInputStream in = new ObjectInputStream(is);
return in.readObject();
}
这是我的测试代码 ,请指教呀!
你测试的是对象吗 ?
public static void main(String[] args) throws IOException {
//hessian
Long startLong = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
byte[] bytes = javaSerialize(new User("dby"+i));
}
System.out.println("-----java--"+(System.currentTimeMillis()-startLong));
startLong = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
byte[] bytes = serialize2(new User("dby"+i));
}
System.out.println("-----hessian2--"+(System.currentTimeMillis()-startLong));
startLong = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
byte[] bytes = serialize(new User("dby"+i));
}
System.out.println("-----hessian--"+(System.currentTimeMillis()-startLong));
}
public static byte[] serialize(Object obj) throws IOException{
if(obj==null) throw new NullPointerException();
ByteArrayOutputStream os = new ByteArrayOutputStream();
HessianOutput ho = new HessianOutput(os);
ho.writeObject(obj);
return os.toByteArray();
}
public static Object deserialize(byte[] by) throws IOException{
if(by==null) throw new NullPointerException();
ByteArrayInputStream is = new ByteArrayInputStream(by);
HessianInput hi = new HessianInput(is);
return hi.readObject();
}
public static byte[] serialize2(Object obj) throws IOException{
if(obj==null) throw new NullPointerException();
ByteArrayOutputStream os = new ByteArrayOutputStream();
Hessian2Output ho = new Hessian2Output(os);
ho.writeObject(obj);
return os.toByteArray();
}
public static Object deserialize2(byte[] by) throws IOException{
if(by==null) throw new NullPointerException();
ByteArrayInputStream is = new ByteArrayInputStream(by);
Hessian2Input hi = new Hessian2Input(is);
return hi.readObject();
}
public static byte[] javaSerialize(Object obj) throws IOException{
if(obj==null) throw new NullPointerException();
ByteArrayOutputStream os = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(obj);
return os.toByteArray();
}
public static Object javaDeserialize(byte[] by) throws IOException, ClassNotFoundException{
if(by==null) throw new NullPointerException();
ByteArrayInputStream is = new ByteArrayInputStream(by);
ObjectInputStream in = new ObjectInputStream(is);
return in.readObject();
}
这是我的测试代码 ,请指教呀!
3 楼
J-catTeam
2010-11-28
haiyupeter 写道
如果能把序列化后的字节减少,那么问一下会带来多少CPU的损耗。
不会的,hessian序列化快的原因是因为它的描述信息比java的少,他用简单的方式描述必要的信息。采取键值对的方式。 所以在序列化一些复杂对象上是有问题的
2 楼
lionbule
2010-08-29
haiyupeter 写道
如果能把序列化后的字节减少,那么问一下会带来多少CPU的损耗。
我坚信“能量守恒定律”。不管哪种方法,只要在实现原理上没有进行彻底的革新,就不会有太大的改观。
换句话说,不是用空间换时间,就是用时间换空间。
所以我们更多关注的应该是实现原理,多看源码就晓得其实每个都差不多。
如果有一天能是“汽车换马车”,就会有很大进步了。期望……
1 楼
haiyupeter
2010-08-16
如果能把序列化后的字节减少,那么问一下会带来多少CPU的损耗。
发表评论
-
spring import jar中的bean配置文件
2012-06-12 17:28 11477在spring中import resource file,有两 ... -
Adding OSGi metadata to existing projects without changing the packaging type
2012-06-05 10:20 1875http://felix.apache.org/site/ap ... -
比较If else 和 try catch 的jvm指令
2012-06-01 10:40 2689编码时过度关注某个点性能,有时候整体性能适得其反!关于if e ... -
java 类加载实例分析之tomcat6.x
2012-03-26 18:47 0[前言] 研究OSGI已经有一段时间,而OSGI的灵魂就在于 ... -
Java 数字转字符串 自动补0
2012-02-20 14:18 2994java中数字转字符,在确定位数的情况下,左边自动补0以对齐 ... -
Java HotSpot VM Options
2012-01-05 11:03 1861http://www.oracle.com/techne ... -
利用ASM和Javassist动态生成Class 类(set和get)
2011-12-30 21:20 11451利用jvm的指令集直接构造class,简单的bean clas ... -
jvm 指令集
2011-12-30 19:46 2211JVM指令助记符 变量到操作数栈:iload,iload_ ... -
switch 和 if elseif 哪个效率高?
2011-12-30 11:23 3264switch 和 if elseif 哪个效率高? ... -
列表和数组的区别
2011-12-27 10:59 4264在数据结构中有多种集合的表现形式!而集合其实就是一个真实 ... -
JVM 选择合适的GC算法
2011-12-19 15:17 1868Selecting a Collector Unles ... -
动态修改JVM 部分参数
2011-12-05 17:00 6921参考地址: http://rednaxelafx.iteye. ... -
Java 快捷读取CLASSPATH下的资源文件
2011-11-30 11:26 17362在JVM中查找classpath下的资源文件,使用Class. ... -
java main函数为非守护线程(实时线程)
2011-11-23 14:29 4373一直认为main为守护线程,google线程相关的资料时,发现 ... -
Solution to Eclipse Warning with SVN
2011-11-14 16:58 1428Solution to Eclipse Warning wit ... -
Java简单数据类型
2011-09-29 11:27 1383简单类型 大小 ... -
spring3.0 单元测试
2011-09-27 18:42 2137spring3.0.0之后的版本,官方极力推荐使用如下方式做单 ... -
logback 之 创建 RollingFileAppender
2011-09-24 17:01 8109最近有需求要将context信息写入文件,以备后续数据分析。本 ... -
logback 之encoder和layout
2011-09-24 16:49 6951encoder 和 layout 在作用上没有本质区别。但是自 ... -
JAVA 排序算法
2011-08-30 21:57 2149import java.util.Random; ...
相关推荐
介绍自己不会查吗?这里有一个点就是Hessian的序列化与反序列化与原生序列化与反序列化不同,就以使用的Resin链而言,其中的javax.naming.spi
NULL 博文链接:https://inter12.iteye.com/blog/1555678
Hessian 2.0序列化协议规范
Nacos JRaft Hessian 反序列化 RCE 分析.pdf
hessian序列化.pdf
主要通过对二者简单的实现方式的对比,介绍了Java序列化和hessian序列化的差异,具有一定参考价值,需要的朋友可以了解下。
NULL 博文链接:https://san-yun.iteye.com/blog/1688510
a --args gadget入参,多个参数使用多次该命令传入,例-a -a Calc-p --protocol [dubbo|http] 通讯协议名称,默认缺省dubbo-s --serialization [hessian|java] 序列化类型,默认缺省hessian-t --target 目标,例:...
想学习Hessian的同志们,这个是一个简单的demo。比较简单,不过能够加深对Hessian的理解
-a:生成exploit下的所有payload(例如:hessian下的SpringPartiallyComparableAdvisorHolder, SpringAbstractBeanFactoryPointcutAdvisor, Rome, XBean, Resin) -t:对生成的payloads进行解码测试 -v:verbose mode...
这是一个Hessian入门学习的实例,程序包是一个web工程,使用intellij idea + Maven开发,其中通过两个方式展示了使用Hessian实现RMI的原理,一个是访问jsp页面,一个是在代码中手工创建client。对于Hessian Servlet...
hessian学习实例,hessian框架例子,与Spring集成。包括了server端和client端
hessian.jar,Hessian的序列化输出 ,
hessian轻量级 rpc实现
默认就是⾛ dubbo 协议,单⼀⻓连接,进⾏的是 NIO 异步通信,基于 hessian 作为序列化协议。使⽤的场景是:传输数据量⼩ (每次请求在 100kb 以内),但是并发量很⾼。 为了要⽀持⾼并发场景,⼀般是服务提供者就⼏...
java hessian-3.0.38.jar。修改了原生的jar包,解决了hessian 序列化BigDecimal的精度问题。注意,请在hessian服务端和客户端中分别替换此jar包哦!! 只替换服务端hessian jar包还是会有问题。
spring aop hessian 基础demo 部署即可运行
SOFA-Hessian 基于原生Hessian v4.0.51进行改进,目前已经蚂蚁金服内部稳定运行多年。我们修复了一些bug,增强了一些功能,并且添加了一些特性。包括:增加泛化序列化。增加 ClassNameResolver 和 ClassNameFilter ...