what:序列化是一種將java對象流化的機制java
how:將一個實現了Serializable接口的對象的狀態寫入byte[],傳輸到另一個地方,將其讀出進行反序列化得對象(含狀態)。狀態就是類中的屬性是含有值的。數據庫
why:方便對象在網絡間進行傳播,而且能夠隨時把對象持久化到數據庫、文件等系統裏緩存
when:對象須要遠程過程調用,緩存到文件或DB中(hessian,rmi,ejb)安全
where:發送接口處,寫入文件的入口處網絡
who:發送端序列化,接收端反序列化架構
how much:序列化自己是昂貴的,但軟件工程自己是複雜,在解藕與性能之間架構師要作一個判斷。ide
?工具
1性能 |
SerializeException 可自定義,繼承runtimeExceptionspa |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
package com.uet.common.utils;
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException;
import com.caucho.hessian.io.HessianSerializerInput; import com.caucho.hessian.io.HessianSerializerOutput; import com.uet.common.exception.SerializeException;
public class HessianObjectSerializeUtil { /** * * 純hessian序列化 * * @param <T> * * @param object * * @return * * @throws Exception */
public static <T> byte[] serialize(T object) { if (object == null) { throw new NullPointerException(); } byte[] results = null; ByteArrayOutputStream os = null; HessianSerializerOutput hessianOutput = null; try { os = new ByteArrayOutputStream(); hessianOutput = new HessianSerializerOutput(os); //write自己是線程安全的 hessianOutput.writeObject(object); os.close(); results = os.toByteArray(); } catch (Exception e) { throw new SerializeException(e); } finally { try { if (os != null) os.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return results; }
/** * * 純hessian反序列化 * * @param bytes * * @return * * @throws Exception */
@SuppressWarnings("unchecked") public static <T> T deserialize(Class<T> resultClass, byte[] bytes) { if (bytes == null) { throw new NullPointerException(); } T result = null; ByteArrayInputStream is = null; try { is = new ByteArrayInputStream(bytes); HessianSerializerInput hessianInput = new HessianSerializerInput(is); result = (T) hessianInput.readObject();
} catch (Exception e) { throw new SerializeException(e); } finally { try { if (is != null) is.close(); } catch (IOException e) { throw new SerializeException(e); } } return result;
} } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
package com.uet.common.utils;
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream;
import com.uet.common.exception.SerializeException;
public class ObjectsSerializeUtil{
public static <T> byte[] serialize(T value) { if (value == null) { throw new NullPointerException("Can't serialize null"); } byte[] result = null; ByteArrayOutputStream bos = null; ObjectOutputStream os = null; try { bos = new ByteArrayOutputStream(); os = new ObjectOutputStream(bos); os.writeObject(value); os.close(); bos.close(); result = bos.toByteArray(); } catch (IOException e) { throw new IllegalArgumentException("Non-serializable object", e); } finally { close(os); close(bos); } return result; }
@SuppressWarnings("unchecked") public static <T> T deserialize(Class<T> resultClass,byte[] in) { T result = null; ByteArrayInputStream bis = null; ObjectInputStream is = null; try { if (in != null) { bis = new ByteArrayInputStream(in); is = new ObjectInputStream(bis); result = (T) is.readObject(); is.close(); bis.close(); } } catch (IOException e) { throw new SerializeException(String.format("Caught IOException decoding %d bytes of data", in == null ? 0 : in.length) + e); } catch (ClassNotFoundException e) { throw new SerializeException(String.format("Caught CNFE decoding %d bytes of data", in == null ? 0 : in.length) + e); } finally { close(is); close(bis); } return result; } public static void close(Closeable closeable) { if (closeable != null) { try { closeable.close(); } catch (Exception e) { throw new SerializeException(e); } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
package com.uet.common.utils;
import com.uet.course.entity.BaseGrade;
public class SerializeTest { private static int count=10000; public static void main(String[] args) throws Exception { /*BaseGrade grade = new BaseGrade(); grade.setId(120L+10); grade.setName("唔年紀"); grade.init(); byte[] results=HessianObjectSerializeUtil.serialize(grade);*/ //System.out.println(results.length); hessianObjectSerialize(); javaObjectSerialize(); } public static void hessianObjectSerialize(){ long start = System.currentTimeMillis(); for(int i=0;i<count;i++){ BaseGrade grade = new BaseGrade(); grade.setId(120L+i); grade.setName("唔年紀"); grade.init(); byte[] results=HessianObjectSerializeUtil.serialize(grade); BaseGrade result=HessianObjectSerializeUtil.deserialize(BaseGrade.class,results); //System.out.println(result.getId()); } long end = System.currentTimeMillis(); System.out.println("hessianObjectSerialize耗時:"+ ((end - start) / 1000.0) + " seconds"); } public static void javaObjectSerialize(){ long start = System.currentTimeMillis(); for(int i=0;i<count;i++){ BaseGrade grade = new BaseGrade(); grade.setId(120L+i); grade.setName("唔年紀"); grade.init(); byte[] results=ObjectsSerializeUtil.serialize(grade); BaseGrade result=ObjectsSerializeUtil.deserialize(BaseGrade.class,results); //System.out.println(result.getId()); } long end = System.currentTimeMillis(); System.out.println("javaObjectSerialize耗時:"+ ((end - start) / 1000.0) + " seconds"); }
} |
序列化的字節260btye
循環1次(運行10次平均結果):
hessianObjectSerialize耗時:0.05 seconds
javaObjectSerialize耗時:0.01 seconds
循環10次(運行10次平均結果):
hessianObjectSerialize耗時:0.06 seconds
javaObjectSerialize耗時:0.015 seconds
循環100次(運行10次平均結果):
hessianObjectSerialize耗時:0.074 seconds
javaObjectSerialize耗時:0.04 seconds
循環1000次(運行10次平均結果):
hessianObjectSerialize耗時:0.162 seconds
javaObjectSerialize耗時:0.123 seconds
循環10000次(運行10次平均結果):
hessianObjectSerialize耗時:0.6 seconds
javaObjectSerialize耗時:0.47 seconds
循環100000次
hessianObjectSerialize耗時:4.668 seconds
javaObjectSerialize耗時:4.144 seconds
java自身所帶的方法明顯比hessian自帶的序列化效率更高。