With the ObjectTransformer plug-in, you can serialize, deserialize, and copy objects in the cache for increased performance.
If you see performance issues with processor usage, add an ObjectTransformer plug-in to each map. If you do not provide an ObjectTransformer plug-in, up to 60-70 percent of the total processor time is spent serializing and copying entries.
If no ObjectTransformer plug-in is provided, you must be able to serialize the keys and values because the ObjectGrid uses a serialize and deserialize sequence to copy the objects. This method is expensive, so use an ObjectTransformer plug-in when performance is critical. The copying occurs when an application looks up an object in a transaction for the first time. You can avoid this copying by setting the copy mode of the Map to NO_COPY or reduce the copying by setting the copy mode to COPY_ON_READ. Optimize the copy operation when needed by the application by providing a custom copy method on this plug-in. Such a plug-in can reduce the copy overhead from 65−70 percent to 2/3 percent of total processor time.
The default copyKey and copyValue method implementations first attempt to use the clone method, if the method is provided. If no clone method implementation is provided, the implementation defaults to serialization.
Object serialization is also used directly when the eXtreme Scale is running in distributed mode. The LogSequence uses the ObjectTransformer plug-in to help serialize keys and values before transmitting the changes to peers in the ObjectGrid. You must take care when providing a custom serialization method instead of using the built-in Java™ developer kit serialization. Object versioning is a complex issue and you might encounter problems with version compatibility if you do not ensure that your custom methods are designed for versioning.
An ObjectTransformer object must implement the ObjectTransformer interface and follow the common ObjectGrid plug-in conventions.
Two approaches, programmatic configuration and XML configuration, are used to add an ObjectTransformer object into the BackingMap configuration as follows.
<?xml version="1.0" encoding="UTF-8"?> <objectGridConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ibm.com/ws/objectgrid/config ../objectGrid.xsd" xmlns="http://ibm.com/ws/objectgrid/config"> <objectGrids> <objectGrid name="myGrid"> <backingMap name="myMap" pluginCollectionRef="myMap" /> </objectGrid> </objectGrids> <backingMapPluginCollections> <backingMapPluginCollection id="myMap"> <bean id="ObjectTransformer" className="com.company.org.MyObjectTransformer" /> </backingMapPluginCollection> </backingMapPluginCollections> </objectGridConfig>
ObjectGridManager objectGridManager = ObjectGridManagerFactory.getObjectGridManager(); ObjectGrid myGrid = objectGridManager.createObjectGrid("myGrid", false); BackingMap backingMap = myGrid.getMap("myMap"); MyObjectTransformer myObjectTransformer = new MyObjectTransformer(); backingMap.setObjectTransformer(myObjectTransformer);
/** * Stock object for ObjectGrid demo * * */ public class Stock implements Cloneable { String ticket; double price; String company; String description; int serialNumber; long lastTransactionTime; /** * @return Returns the description. */ public String getDescription() { return description; } /** * @param description The description to set. */ public void setDescription(String description) { this.description = description; } /** * @return Returns the lastTransactionTime. */ public long getLastTransactionTime() { return lastTransactionTime; } /** * @param lastTransactionTime The lastTransactionTime to set. */ public void setLastTransactionTime(long lastTransactionTime) { this.lastTransactionTime = lastTransactionTime; } /** * @return Returns the price. */ public double getPrice() { return price; } /** * @param price The price to set. */ public void setPrice(double price) { this.price = price; } /** * @return Returns the serialNumber. */ public int getSerialNumber() { return serialNumber; } /** * @param serialNumber The serialNumber to set. */ public void setSerialNumber(int serialNumber) { this.serialNumber = serialNumber; } /** * @return Returns the ticket. */ public String getTicket() { return ticket; } /** * @param ticket The ticket to set. */ public void setTicket(String ticket) { this.ticket = ticket; } /** * @return Returns the company. */ public String getCompany() { return company; } /** * @param company The company to set. */ public void setCompany(String company) { this.company = company; } //clone public Object clone() throws CloneNotSupportedException { return super.clone(); } }
You can write a custom object transformer class for the Stock class:
/** * Custom implementation of ObjectGrid ObjectTransformer for stock object * */ public class MyStockObjectTransformer implements ObjectTransformer { /* (non−Javadoc) * @see * com.ibm.websphere.objectgrid.plugins.ObjectTransformer#serializeKey * (java.lang.Object, * java.io.ObjectOutputStream) */ public void serializeKey(Object key, ObjectOutputStream stream) throws IOException { String ticket= (String) key; stream.writeUTF(ticket); } /* (non−Javadoc) * @see com.ibm.websphere.objectgrid.plugins. ObjectTransformer#serializeValue(java.lang.Object, java.io.ObjectOutputStream) */ public void serializeValue(Object value, ObjectOutputStream stream) throws IOException { Stock stock= (Stock) value; stream.writeUTF(stock.getTicket()); stream.writeUTF(stock.getCompany()); stream.writeUTF(stock.getDescription()); stream.writeDouble(stock.getPrice()); stream.writeLong(stock.getLastTransactionTime()); stream.writeInt(stock.getSerialNumber()); } /* (non−Javadoc) * @see com.ibm.websphere.objectgrid.plugins. ObjectTransformer#inflateKey(java.io.ObjectInputStream) */ public Object inflateKey(ObjectInputStream stream) throws IOException, ClassNotFoundException { String ticket=stream.readUTF(); return ticket; } /* (non−Javadoc) * @see com.ibm.websphere.objectgrid.plugins. ObjectTransformer#inflateValue(java.io.ObjectInputStream) */ public Object inflateValue(ObjectInputStream stream) throws IOException, ClassNotFoundException { Stock stock=new Stock(); stock.setTicket(stream.readUTF()); stock.setCompany(stream.readUTF()); stock.setDescription(stream.readUTF()); stock.setPrice(stream.readDouble()); stock.setLastTransactionTime(stream.readLong()); stock.setSerialNumber(stream.readInt()); return stock; } /* (non−Javadoc) * @see com.ibm.websphere.objectgrid.plugins. ObjectTransformer#copyValue(java.lang.Object) */ public Object copyValue(Object value) { Stock stock = (Stock) value; try { return stock.clone(); } catch (CloneNotSupportedException e) { // display exception message } } /* (non−Javadoc) * @see com.ibm.websphere.objectgrid.plugins. ObjectTransformer#copyKey(java.lang.Object) */ public Object copyKey(Object key) { String ticket=(String) key; String ticketCopy= new String (ticket); return ticketCopy; } }
ObjectGridManager ogf=ObjectGridManagerFactory.getObjectGridManager(); ObjectGrid og = ogf.getObjectGrid("NYSE"); BackingMap bm = og.defineMap("NYSEStocks"); MyStockObjectTransformer ot = new MyStockObjectTransformer(); bm.setObjectTransformer(ot);