Coherence Save Retrieve Byte Array Cache Example

In this tutorial you will have to convert java POJO to byte array using Oracle coherence API and sate into the cache and retrieve byte array from cache and converted back to Java POJO object.

Orache Coherence used : coherence-12.1.3.0.2.jar:

  • SerializationHelper.java: This is main class which converts Object to byte array vice versa:
package com.javahonk.cache;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.InputStream;

import com.tangosol.io.WrapperBufferInput;
import com.tangosol.io.WrapperBufferOutput;
import com.tangosol.io.pof.PofContext;

public class SerializationHelper {

	public static Object Deserialize(PofContext context, byte[] payload) throws Exception{
		
		InputStream in = new ByteArrayInputStream(payload);
		
		DataInput dataInput = new DataInputStream(in);
		
		WrapperBufferInput ip = new WrapperBufferInput(dataInput);
		
		return context.deserialize(ip);
	}
	
	public static byte[] Serialize(PofContext context, Object payload) throws Exception{
		
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		
		DataOutput dataOut = new DataOutputStream(out);
		
		WrapperBufferOutput op = new WrapperBufferOutput(dataOut);
		
		context.serialize(op, payload);
		
		return out.toByteArray(); 
	}
	
}
  • SaveRetrieveByteArrayFromCache.java: Save and Retrieve data from cache example:
package com.javahonk.cache;

import java.util.Date;

import com.javahonk.cache.serializer.Employee;
import com.javahonk.cache.serializer.Person;
import com.tangosol.io.pof.ConfigurablePofContext;
import com.tangosol.net.NamedCache;
import com.javahonk.cib.spt.datautil.CacheRegistry;

public class SaveRetrieveByteArrayFromCache {

	public void saveRetrieveDate() {

		NamedCache cache = (NamedCache) CacheRegistry.getNamedCache("Products");
		
		try {
			
			Person person = new Person("Java Honk", new Date(), new Integer(882221));
			
			Employee employee = new Employee(SerializationHelper.Serialize(new ConfigurablePofContext(), person));
			cache.put("employee", employee);
			Employee employee2 =  (Employee) cache.get("employee");
			
			Person person2 = (Person) SerializationHelper.Deserialize(new ConfigurablePofContext(), employee2.getPerson());
			
			System.out.println("First Name: "+person2.getName());
			System.out.println("DOB: "+person2.getDob());
			System.out.println("Zip: "+person2.getZip());
			
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}

 

For reference purpose model and serializers:

  • Person.java:
package com.javahonk.cache.serializer;

import java.util.Date;

public class Person {
	
	private String name;
	private Date dob;
	private Integer zip;
	public Person(String name, Date dob, Integer zip) {
		super();
		this.name = name;
		this.dob = dob;
		this.zip = zip;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Date getDob() {
		return dob;
	}
	public void setDob(Date dob) {
		this.dob = dob;
	}
	public Integer getZip() {
		return zip;
	}
	public void setZip(Integer zip) {
		this.zip = zip;
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", dob=" + dob + ", zip=" + zip + "]";
	}
	
}
  • PersonSerializer.java:
package com.javahonk.cache.serializer;

import java.io.IOException;
import java.util.Date;

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

public class PersonSerializer implements PofSerializer {

	@Override
	public Object deserialize(PofReader pofReader) throws IOException {

		String name = pofReader.readString(1);
		Date dob = pofReader.readDate(2);
		Integer zip = pofReader.readInt(3);
		
		pofReader.readRemainder();

		return new Person(name, dob, zip);
	}

	@Override
	public void serialize(PofWriter pofWriter, Object o) throws IOException {

		Person person = (Person) o;

		pofWriter.writeString(1,person.getName());
		pofWriter.writeDateTime(2,person.getDob());
		pofWriter.writeInt(3,person.getZip());
		
		pofWriter.writeRemainder(null);

	}
}
  • Employee.java:
package com.javahonk.cache.serializer;

import java.util.Arrays;

public class Employee {
	
	private byte person[];
	public Employee(byte[] person) {
		super();
		this.person = person;
	}
	public byte[] getPerson() {
		return person;
	}
	public void setPerson(byte[] person) {
		this.person = person;
	}
	@Override
	public String toString() {
		return "Employee [person=" + Arrays.toString(person) + "]";
	}
	
}
  • EmployeeSerializer.java:
package com.javahonk.cache.serializer;

import java.io.IOException;

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

public class EmployeeSerializer implements PofSerializer {

	@Override
	public Object deserialize(PofReader pofReader) throws IOException {

		byte person[] = pofReader.readByteArray(1);
		
		pofReader.readRemainder();

		return new Employee(person);
	}

	@Override
	public void serialize(PofWriter pofWriter, Object o) throws IOException {

		Employee employee = (Employee) o;

		pofWriter.writeByteArray(1,employee.getPerson());
		
		pofWriter.writeRemainder(null);

	}
}

Leave a Reply

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