Convert Coherence POF Object byte array Vice Versa

Convert Coherence POF Object byte array Vice Versa

If you worked with Oracle Coherence you might be aware about POF (Portable Object Fomat). In this example I will show you how to convert Oracle POF object to byte and array so that object data can be send over the network to other server. In my case I had to send POF object to Solace publish queue and on the other end subscriber will deserialize to convert byte array to object and perform business logic on it:

  • Jar needed: coherence-12.1.3.0.2.jar (I used but you could use any latest compatible version)
  • To run below java class you will have to pass VM argument as below:
-Dwells.ccu.clientconfig=extend-access.xml
-Dwells.ccu.url=http://techiworks.com/clusterclient/javahonk/server/
-Dwells.cluster.extend.access=true
-Dwells.ccu.serializer.config=http://techiworks.com/clusterclient/javahonk
-Dtangosol.pof.enabled=true
-Dtangosol.pof.config=custom-types-pof-config.xml
  • custom-types-pof-config.xml:
<?xml version="1.0"?>

<!DOCTYPE pof-config SYSTEM "pof-config.dtd">

<pof-config>
	<user-type-list>
		<include>coherence-pof-config.xml</include>
		<user-type>
			<type-id>150</type-id>
			<class-name>com.javahonk.ChangeControlInfoModel</class-name>
			<serializer>
				<class-name>com.javahonk.serializers.ChangeControlInfoModelSerializer</class-name>
			</serializer>
		</user-type>		
	</user-type-list>
</pof-config>
  • SerializeObjectToCoherenceFormat.java
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 java.util.Date;

import com.tangosol.io.WrapperBufferInput;
import com.tangosol.io.WrapperBufferOutput;
import com.tangosol.io.pof.ConfigurablePofContext;
import com.tangosol.io.pof.PofContext;
import com.wfs.otc.enums.ChangeControlTypes;
import com.wfs.otc.model.common.ChangeControlInfoModel;

public class SerializeObjectToCoherenceFormat {
	
	private static PofContext context = new ConfigurablePofContext();
	
	public static void main(String[] args) throws Exception {
		
		ChangeControlInfoModel model = new ChangeControlInfoModel(0, "JavaHonk",
				ChangeControlTypes.Product, "JavaHonk", "JavaHonk", "JavaHonk", "JavaHonk",
				new Date(), "JavaHonk", new Date(), "JavaHonk", "JavaHonk", "JavaHonk",
				"JavaHonk");
		
		byte [] serializedByteArrayObject = Serialize(context, model);
		
		System.out.println(Deserialize(context, serializedByteArrayObject));
		
		
	}
	
	public static Object Deserialize(PofContext context, byte[] byteArray) throws Exception{
		
		InputStream in = new ByteArrayInputStream(byteArray);
		
		DataInput dataInput = new DataInputStream(in);
		
		WrapperBufferInput ip = new WrapperBufferInput(dataInput);
		
		return context.deserialize(ip);
	}
	
	public static byte[] Serialize(PofContext context, Object object) throws Exception{
		
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		
		DataOutput dataOut = new DataOutputStream(out);
		
		WrapperBufferOutput op = new WrapperBufferOutput(dataOut);
		
		context.serialize(op, object);
		
		return out.toByteArray(); 
	}
	

}
  • I assumed you already created object and serialized in Coherence if not below is an example:
  • ChangeControlInfoModel.java:
import java.util.Date;

import com.wfs.otc.enums.ChangeControlTypes;
import com.wfs.otc.interfaces.common.ChangeControlInfo;
import com.wfs.otc.interfaces.common.Identifier;

public class ChangeControlInfoModel extends VersionableModel  implements ChangeControlInfo, Identifier {

	private String id;
	
	private ChangeControlTypes changeType;	
	private String changeReason;	
	private String stagedProductId;	
	private String productId;	
	private String orderId;	
	private Date createTime;
	private String updateUser;
	private Date updateTime;
	private String allegeUser;	
	private String changes;	
	private String workflowStatus;	
	private String workflowInstanceId;	
	
	public ChangeControlInfoModel(int version, String id,
			ChangeControlTypes changeType, String changeReason,
			String stagedProductId, String productId, String orderId,
			Date createTime, String updateUser, Date updateTime,
			String allegeUser, String changes, String workflowStatus,
			String workflowInstanceId) {
		super(version);
		this.id = id;
		this.changeType = changeType;
		this.changeReason = changeReason;
		this.stagedProductId = stagedProductId;
		this.productId = productId;
		this.orderId = orderId;
		this.createTime = createTime;
		this.updateUser = updateUser;
		this.updateTime = updateTime;
		this.allegeUser = allegeUser;
		this.changes = changes;
		this.workflowStatus = workflowStatus;
		this.workflowInstanceId = workflowInstanceId;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public ChangeControlTypes getChangeType() {
		return changeType;
	}
	public void setChangeType(ChangeControlTypes changeType) {
		this.changeType = changeType;
	}
	public String getChangeReason() {
		return changeReason;
	}
	public void setChangeReason(String changeReason) {
		this.changeReason = changeReason;
	}
	public String getStagedProductId() {
		return stagedProductId;
	}
	public void setStagedProductId(String stagedProductId) {
		this.stagedProductId = stagedProductId;
	}
	public String getProductId() {
		return productId;
	}
	public void setProductId(String productId) {
		this.productId = productId;
	}
	public String getOrderId() {
		return orderId;
	}
	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}
	public Date getCreateTime() {
		return createTime;
	}
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	public String getUpdateUser() {
		return updateUser;
	}
	public void setUpdateUser(String updateUser) {
		this.updateUser = updateUser;
	}
	public Date getUpdateTime() {
		return updateTime;
	}
	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}
	public String getAllegeUser() {
		return allegeUser;
	}
	public void setAllegeUser(String allegeUser) {
		this.allegeUser = allegeUser;
	}
	public String getChanges() {
		return changes;
	}
	public void setChanges(String changes) {
		this.changes = changes;
	}
	public String getWorkflowStatus() {
		return workflowStatus;
	}
	public void setWorkflowStatus(String workflowStatus) {
		this.workflowStatus = workflowStatus;
	}
	public String getWorkflowInstanceId() {
		return workflowInstanceId;
	}
	public void setWorkflowInstanceId(String workflowInstanceId) {
		this.workflowInstanceId = workflowInstanceId;
	}
	
	@Override
	public String toString() {
		return "ChangeControlInfoModel [id=" + id + ", changeType="
				+ changeType + ", changeReason=" + changeReason
				+ ", stagedProductId=" + stagedProductId + ", productId="
				+ productId + ", orderId=" + orderId + ", createTime="
				+ createTime + ", updateUser=" + updateUser + ", updateTime="
				+ updateTime + ", allegeUser=" + allegeUser + ", changes="
				+ changes + ", workflowStatus=" + workflowStatus
				+ ", workflowInstanceId=" + workflowInstanceId + "]" + super.toString();
	}
		
}
  • ChangeControlInfoModelSerializer.java:
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;
import com.wfs.otc.enums.ChangeControlTypes;
import com.wfs.otc.model.common.ChangeControlInfoModel;
import com.wfs.otc.serializer.enums.common.ChangeControlInfoConstant;
import com.wfs.otc.serializer.enums.common.VersionableConstant;
import com.wfs.otc.serializer.enums.product.ProductConstant;

public class ChangeControlInfoModelSerializer implements PofSerializer {
	
	@Override
	public Object deserialize(PofReader pofReader) throws IOException {
		
		int version = pofReader.readInt(VersionableConstant.VERSION.getValue());
		
		String id = pofReader.readString(ProductConstant.ID.getValue());
		
		//Global index		
		String productId = pofReader.readString(ChangeControlInfoConstant.PRODUCTID.getValue());	
		Date createTime = pofReader.readDate(ChangeControlInfoConstant.CREATETIME.getValue());
		String updateUser = pofReader.readString(ChangeControlInfoConstant.UPDATEUSER.getValue());
		Date updateTime = pofReader.readDate(ChangeControlInfoConstant.UPDATETIME.getValue());
		String workflowInstanceId = pofReader.readString(ChangeControlInfoConstant.WORKFLOWINSTANCEID.getValue());	
		String workflowStatus = pofReader.readString(ChangeControlInfoConstant.WORKFLOWSTATUS.getValue());
		String orderId = pofReader.readString(ChangeControlInfoConstant.ORDERID.getValue());	
		
		//Local index
		ChangeControlTypes changeType = ChangeControlTypes.valueOf(pofReader.readString(ChangeControlInfoConstant.CHANGETYPE.getValue()));	
		String changeReason = pofReader.readString(ChangeControlInfoConstant.CHANGEREASON.getValue());	
		String stagedProductId = pofReader.readString(ChangeControlInfoConstant.STAGEDPRODUCTID.getValue());
		String allegeUser = pofReader.readString(ChangeControlInfoConstant.ALLEGEUSER.getValue());	
		String changes = pofReader.readString(ChangeControlInfoConstant.CHANGES.getValue());
		
		pofReader.readRemainder();
		
		return new ChangeControlInfoModel(version, id, changeType,
				changeReason, stagedProductId, productId, orderId, createTime,
				updateUser, updateTime, allegeUser, changes, workflowStatus,
				workflowInstanceId);
	}

	@Override
	public void serialize(PofWriter pofWriter, Object o) throws IOException {
		
		ChangeControlInfoModel changeControlInfoModel = (ChangeControlInfoModel) o;
		
		pofWriter.writeInt(VersionableConstant.VERSION.getValue(), changeControlInfoModel.getVersion());
		
		pofWriter.writeString(ProductConstant.ID.getValue(), changeControlInfoModel.getId());
		
		//Global index	
		pofWriter.writeString(ChangeControlInfoConstant.PRODUCTID.getValue(), changeControlInfoModel.getProductId());
		pofWriter.writeDate(ChangeControlInfoConstant.CREATETIME.getValue(), changeControlInfoModel.getCreateTime());
		pofWriter.writeString(ChangeControlInfoConstant.UPDATEUSER.getValue(), changeControlInfoModel.getUpdateUser());
		pofWriter.writeDate(ChangeControlInfoConstant.UPDATETIME.getValue(), changeControlInfoModel.getUpdateTime());
		pofWriter.writeString(ChangeControlInfoConstant.WORKFLOWINSTANCEID.getValue(), changeControlInfoModel.getWorkflowInstanceId());
		pofWriter.writeString(ChangeControlInfoConstant.WORKFLOWSTATUS.getValue(), changeControlInfoModel.getWorkflowStatus());
		pofWriter.writeString(ChangeControlInfoConstant.ORDERID.getValue(), changeControlInfoModel.getOrderId());
		
		//Local index
		pofWriter.writeString(ChangeControlInfoConstant.CHANGETYPE.getValue(), changeControlInfoModel.getChangeType().name());
		pofWriter.writeString(ChangeControlInfoConstant.CHANGEREASON.getValue(), changeControlInfoModel.getChangeReason());
		pofWriter.writeString(ChangeControlInfoConstant.STAGEDPRODUCTID.getValue(), changeControlInfoModel.getStagedProductId());
		pofWriter.writeString(ChangeControlInfoConstant.ALLEGEUSER.getValue(), changeControlInfoModel.getAllegeUser());
		pofWriter.writeString(ChangeControlInfoConstant.CHANGES.getValue(), changeControlInfoModel.getChanges());
		
		pofWriter.writeRemainder(null);
		
	}
}

Leave a Reply

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