POF Coherence Serialization example

POF Coherence Serialization example

Serialization is the process where you encode object into binary format. Of-course if you are using Coherence to save/retrieve data will moved around the network. POF (Portable Object Format) basically is language agnostic binary format which has been designed to use efficiently to save and time and became cornerstone element while working with Coherence. There are many different wasy are available to serialize the object including standard java serialization also you could write you own custom serialize. Java serialization is easiest way to implement and preserve object identity. Once object got serialized then it can be read using any language. In below example we will use PofSerializer interface from Oracle Coherence which provide two abstract method “deserialize” and “serialize” which can be use to serialize the object. 

We will serialize below type of data:

  • Object
  • HashMap
  • List
  • String
  • Date
  • boolean
  • int
  • Enum
  • double
  • long

First create serializer for SampleModel.java because we will use this model to keep in HashMap while serializing big example:

  • SampleModel.java:
public class SampleModel {
	
	private String firstName;
	private String lastName;
	public SampleModel(String firstName, String lastName) {
		super();
		this.firstName = firstName;
		this.lastName = lastName;
	}
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	
	

}
  • SampleModelSerializer.java:
import java.io.IOException;

import com.tangosol.io.pof.PofReader;
import com.tangosol.io.pof.PofSerializer;
import com.tangosol.io.pof.PofWriter;

public class SampleModelSerializer implements PofSerializer{
	
	@Override
	public Object deserialize(PofReader pofReader) throws IOException {
		
		int count = 0;
		
		String firstName = pofReader.readString(count++);
		String lastName = pofReader.readString(count++);
				
		pofReader.readRemainder();
		
		return new SampleModel(firstName, lastName);

	}

	@Override
	public void serialize(PofWriter pofWriter, Object o) throws IOException {
		
		SampleModel sampleModel = (SampleModel) o;
		
		int count = 0;
		
		pofWriter.writeString(count++, sampleModel.getFirstName());
		pofWriter.writeString(count++, sampleModel.getLastName());
		
		pofWriter.writeRemainder(null);
		
	}
}
  • Create Enum class: EnumForSerializer.java:
public enum EnumForSerializer {
	
	JavaHonk,
	TestMe,
	CoherenceSerializtion

}
  • SerializationModelData.java: Main class with all type of data will be serialized:
import java.util.Date;
import java.util.List;
import java.util.Map;

public class SerializationModelData {
	
	private EnumForSerializer enumTest;
	private Map<String, String> testMap;
	private Map<String, SampleModel> sampleModelMap;
	private List<String> list;
	private List<SampleModel> sampleModelList;
	private boolean testBoolean;
	private String testString;
	private Date dateTest;
	private double doubleTest;
	private long longTest;
	private SampleModel sampleModel;
	private int intTest;
	
	public SerializationModelData(EnumForSerializer enumTest,
			Map<String, String> testMap,
			Map<String, SampleModel> sampleModelMap, List<String> list,
			List<SampleModel> sampleModelList, boolean testBoolean,
			String testString, Date dateTest, double doubleTest, long longTest,
			SampleModel sampleModel, int intTest) {
		super();
		this.enumTest = enumTest;
		this.testMap = testMap;
		this.sampleModelMap = sampleModelMap;
		this.list = list;
		this.sampleModelList = sampleModelList;
		this.testBoolean = testBoolean;
		this.testString = testString;
		this.dateTest = dateTest;
		this.doubleTest = doubleTest;
		this.longTest = longTest;
		this.sampleModel = sampleModel;
		this.intTest = intTest;
	}
	public EnumForSerializer getEnumTest() {
		return enumTest;
	}
	public void setEnumTest(EnumForSerializer enumTest) {
		this.enumTest = enumTest;
	}
	public Map<String, String> getTestMap() {
		return testMap;
	}
	public void setTestMap(Map<String, String> testMap) {
		this.testMap = testMap;
	}
	public Map<String, SampleModel> getSampleModelMap() {
		return sampleModelMap;
	}
	public void setSampleModelMap(Map<String, SampleModel> sampleModelMap) {
		this.sampleModelMap = sampleModelMap;
	}
	public List<String> getList() {
		return list;
	}
	public void setList(List<String> list) {
		this.list = list;
	}
	public List<SampleModel> getSampleModelList() {
		return sampleModelList;
	}
	public void setSampleModelList(List<SampleModel> sampleModelList) {
		this.sampleModelList = sampleModelList;
	}
	public boolean isTestBoolean() {
		return testBoolean;
	}
	public void setTestBoolean(boolean testBoolean) {
		this.testBoolean = testBoolean;
	}
	public String getTestString() {
		return testString;
	}
	public void setTestString(String testString) {
		this.testString = testString;
	}
	public Date getDateTest() {
		return dateTest;
	}
	public void setDateTest(Date dateTest) {
		this.dateTest = dateTest;
	}
	public double getDoubleTest() {
		return doubleTest;
	}
	public void setDoubleTest(double doubleTest) {
		this.doubleTest = doubleTest;
	}
	public long getLongTest() {
		return longTest;
	}
	public void setLongTest(long longTest) {
		this.longTest = longTest;
	}
	public SampleModel getSampleModel() {
		return sampleModel;
	}
	public void setSampleModel(SampleModel sampleModel) {
		this.sampleModel = sampleModel;
	}
	public int getIntTest() {
		return intTest;
	}
	public void setIntTest(int intTest) {
		this.intTest = intTest;
	}

	

}
  • SampleModelDataSerializer.java – Main class where we are serializing the object:
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.tangosol.io.pof.PofReader;
import com.tangosol.io.pof.PofSerializer;
import com.tangosol.io.pof.PofWriter;

public class SampleModelDataSerializer implements PofSerializer{
	
	@SuppressWarnings("unchecked")
	@Override
	public Object deserialize(PofReader pofReader) throws IOException {
		
		int count = 0;
		
		EnumForSerializer enumTest = EnumForSerializer.valueOf(pofReader.readString(count++));
		Map<String, String> testMap = pofReader.readMap(count++, new HashMap<String, String>());
		Map<String, SampleModel> sampleModelMap = pofReader.readMap(count++, new HashMap<String, String>());
		List<String> list = (List<String>) pofReader.readCollection(count++, new ArrayList<String>());
		List<SampleModel> sampleModelList = (List<SampleModel>) pofReader.readCollection(count++, new ArrayList<SampleModel>());
		boolean testBoolean = pofReader.readBoolean(count++);
		String testString = pofReader.readString(count++);
		Date dateTest = pofReader.readDate(count++);
		double doubleTest = pofReader.readDouble(count++);
		long longTest = pofReader.readLong(count++);
		SampleModel sampleModel = (SampleModel) pofReader.readObject(count++);
		int intTest = pofReader.readInt(count++);
		
		pofReader.readRemainder();
		
		return new SerializationModelData(enumTest, testMap, sampleModelMap,
				list, sampleModelList, testBoolean, testString, dateTest,
				doubleTest, longTest, sampleModel, intTest);

	}

	@Override
	public void serialize(PofWriter pofWriter, Object o) throws IOException {
		
		SerializationModelData serializationModelData = (SerializationModelData) o;
		
		int count = 0;
		
		pofWriter.writeString(count++, serializationModelData.getEnumTest().name());
		pofWriter.writeMap(count++, serializationModelData.getTestMap());
		pofWriter.writeMap(count++, serializationModelData.getSampleModelMap());
		pofWriter.writeCollection(count++, serializationModelData.getList());
		pofWriter.writeCollection(count++, serializationModelData.getSampleModelList());
		pofWriter.writeBoolean(count++, serializationModelData.isTestBoolean());
		pofWriter.writeString(count++, serializationModelData.getTestString());
		pofWriter.writeDate(count++, serializationModelData.getDateTest());
		pofWriter.writeDouble(count++, serializationModelData.getDoubleTest());
		pofWriter.writeLong(count++, serializationModelData.getLongTest());
		pofWriter.writeObject(count++, serializationModelData.getSampleModel());
		pofWriter.writeInt(count++, serializationModelData.getIntTest());
		
		pofWriter.writeRemainder(null);
		
	}
}

Reference:

Leave a Reply

Your email address will not be published. Required fields are marked *