יום שישי, 12 ביולי 2013

Java Serialization

The pojo object

  1: package playwithserialization;
  2: 
  3: import java.io.Serializable;
  4: import java.lang.reflect.Field;
  5: import java.util.Date;
  6: import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
  7: 
  8: /**
  9:  *
 10:  * 
 11:  */
 12: public class LogMsg implements Serializable {
 13: 
 14:     public Date getDateTime() {
 15:         return DateTime;
 16:     }
 17: 
 18:     public void setDateTime(Date DateTime) {
 19:         this.DateTime = DateTime;
 20:     }
 21: 
 22:     public String getMsg() {
 23:         return Msg;
 24:     }
 25: 
 26:     public void setMsg(String Msg) {
 27:         this.Msg = Msg;
 28:     }
 29: 
 30:     public String getMessageGroup() {
 31:         return MessageGroup;
 32:     }
 33: 
 34:     public void setMessageGroup(String MessageGroup) {
 35:         this.MessageGroup = MessageGroup;
 36:     }
 37: 
 38:     public TraceEventType getSeverity() {
 39:         return Severity;
 40:     }
 41: 
 42:     public void setSeverity(TraceEventType Severity) {
 43:         this.Severity = Severity;
 44:     }
 45: 
 46:     public String getThreadID() {
 47:         return threadID;
 48:     }
 49: 
 50:     public void setThreadID(String threadID) {
 51:         this.threadID = threadID;
 52:     }
 53: 
 54:     public String getThreadName() {
 55:         return ThreadName;
 56:     }
 57: 
 58:     public void setThreadName(String ThreadName) {
 59:         this.ThreadName = ThreadName;
 60:     }
 61: 
 62:     public String getAdditionalInfo() {
 63:         return AdditionalInfo;
 64:     }
 65: 
 66:     public void setAdditionalInfo(String AdditionalInfo) {
 67:         this.AdditionalInfo = AdditionalInfo;
 68:     }
 69: 
 70:     public String getStackTrace() {
 71:         return StackTrace;
 72:     }
 73: 
 74:     public void setStackTrace(String StackTrace) {
 75:         this.StackTrace = StackTrace;
 76:     }
 77:     
 78:     // Summary:
 79:     //     Identifies the type of event that has caused the trace.
 80:     public enum TraceEventType
 81:     {
 82:         // Summary:
 83:         //     Fatal error or application crash.
 84:         Critical (1),
 85:         //
 86:         // Summary:
 87:         //     Recoverable error.
 88:         Error ( 2),
 89:                 
 90:         //
 91:         // Summary:
 92:         //     Noncritical problem.
 93:         Warning (4),
 94:         //
 95:         // Summary:
 96:         //     Informational message.
 97:         Information (8),
 98:         //
 99:         // Summary:
100:         //     Debugging trace.
101:         Verbose (16),
102:         //
103:         // Summary:
104:         //     Starting of a logical operation.
105:        
106:         Start (256),
107:       
108:         Stop ( 512),
109:       
110:         Suspend (1024),
111:       
112:         Resume ( 2048),
113:       
114:         Transfer (4096);
115:    
116:         private final int mOrder;
117:           
118:          TraceEventType(int pOrder )
119:         {
120:             mOrder = pOrder;    
121:         } 
122:     }
123:     
124:     
125:        private Date DateTime ;
126: 
127:         private String Msg ;
128: 
129:         private String MessageGroup ;
130: 
131:         private TraceEventType Severity ;
132: 
133:         private String threadID ;
134: 
135:         private String ThreadName ;
136: 
137:         private String AdditionalInfo ;
138: 
139:         private String StackTrace ;
140: 
141:         public LogMsg()
142:         {
143:             
144:         } 
145:         
146:             public String toString() {
147:      return (new ReflectionToStringBuilder(this) {
148:          protected boolean accept(Field f) {
149:              return true;
150:          }
151:      }).toString();
152:             }
153: }

The test project


  1: package playwithserialization;
  2: 
  3: import java.io.FileInputStream;
  4: import java.io.FileOutputStream;
  5: import java.io.ObjectInputStream;
  6: import java.io.ObjectOutputStream;
  7: 
  8: import java.util.Date;
  9: 
 10: /**
 11:  *
 12:  * @author Zvika Peer 
 13:  */
 14: public class PlayWithSerialization {
 15: 
 16:     /**
 17:      * @param args the command line arguments
 18:      */
 19:     public static void main(String[] args) {
 20:     
 21:          String filename = "LogMsg.ser";
 22:     LogMsg theLogMsg = new LogMsg();
 23: 
 24:     theLogMsg.setAdditionalInfo("Additional info");
 25:     
 26:     theLogMsg.setDateTime( new Date());
 27:     
 28:     theLogMsg.setMessageGroup("my Message group");
 29:     
 30:     theLogMsg.setMsg("This is the message ");
 31:     
 32:     theLogMsg.setSeverity(LogMsg.TraceEventType.Critical );
 33:     
 34:     // Save the object to file
 35:     FileOutputStream fos = null;
 36:     ObjectOutputStream out = null;
 37:     try {
 38:       fos = new FileOutputStream(filename);
 39:       out = new ObjectOutputStream(fos);
 40:       out.writeObject(theLogMsg);
 41: 
 42:       out.close();
 43:     } catch (Exception ex) {
 44:       ex.printStackTrace();
 45:     }
 46:     // Read the object from file
 47:     // Save the object to file
 48:     FileInputStream fis = null;
 49:     ObjectInputStream in = null;
 50:     try {
 51:       fis = new FileInputStream(filename);
 52:       in = new ObjectInputStream(fis);
 53:       theLogMsg = (LogMsg) in.readObject();
 54:       out.close();
 55:     } catch (Exception ex) {
 56:       ex.printStackTrace();
 57:     }
 58:     System.out.println(theLogMsg);
 59:   }
 60: }

The Result:
debug:
playwithserialization.LogMsg@1bb73b0[DateTime=Sun Jun 30 10:57:35 IDT 2013,Msg=This is the message ,MessageGroup=my Message group,Severity=Critical,threadID=<null>,ThreadName=<null>,AdditionalInfo=Additional info,StackTrace=<null>]
BUILD SUCCESSFUL (total time: 0 seconds)

References
http://javarevisited.blogspot.co.il/2011/04/top-10-java-serialization-interview.html
http://www.vogella.com/articles/JavaSerialization/article.html

אין תגובות:

הוסף רשומת תגובה