Al cargar desde el lado del cliente, la utilización de un agente DataGrid podría aumentar el rendimiento. Al utilizar un agente DataGrid, todas las lecturas y escrituras de datos se producen en el proceso de servidor. También puede diseñar la aplicación para asegurarse de que los agentes DataGrid de varias particiones se ejecuten en paralelo para aumentar más el rendimiento.
El siguiente fragmento de código es un ejemplo de cómo realizar la carga mediante un agente DataGrid:
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import com.ibm.websphere.objectgrid.NoActiveTransactionException;
import com.ibm.websphere.objectgrid.ObjectGridException;
import com.ibm.websphere.objectgrid.ObjectGridRuntimeException;
import com.ibm.websphere.objectgrid.ObjectMap;
import com.ibm.websphere.objectgrid.Session;
import com.ibm.websphere.objectgrid.TransactionException;
import com.ibm.websphere.objectgrid.datagrid.ReduceGridAgent;
import com.ibm.websphere.objectgrid.em.EntityManager;
public class InsertAgent implements ReduceGridAgent, Externalizable {
private static final long serialVersionUID = 6568906743945108310L;
private List keys = null;
private List vals = null;
protected boolean isEntityMap;
public InsertAgent() {
}
public InsertAgent(boolean entityMap) {
isEntityMap = entityMap;
}
public Object reduce(Session sess, ObjectMap map) {
throw new UnsupportedOperationException(
"ReduceGridAgent.reduce(Session, ObjectMap)");
}
public Object reduce(Session sess, ObjectMap map, Collection arg2) {
Session s = null;
try {
s = sess.getObjectGrid().getSession();
ObjectMap m = s.getMap(map.getName());
s.beginNoWriteThrough();
Object ret = process(s, m);
s.commit();
return ret;
} catch (ObjectGridRuntimeException e) {
if (s.isTransactionActive()) {
try {
s.rollback();
} catch (TransactionException e1) {
} catch (NoActiveTransactionException e1) {
}
}
throw e;
} catch (Throwable t) {
if (s.isTransactionActive()) {
try {
s.rollback();
} catch (TransactionException e1) {
} catch (NoActiveTransactionException e1) {
}
}
throw new ObjectGridRuntimeException(t);
}
}
public Object process(Session s, ObjectMap m) {
try {
if (!isEntityMap) {
// En el caso POJO, es una operación directa,
// se coloca todo en la
// correlación mediante la operación de insertar
insert(m);
} else {
// 2. Caso Entity.
// En el caso Entity, pueden persistirse las entidades
EntityManager em = s.getEntityManager();
persistEntities(em);
}
return Boolean.TRUE;
} catch (ObjectGridRuntimeException e) {
throw e;
} catch (ObjectGridException e) {
throw new ObjectGridRuntimeException(e);
} catch (Throwable t) {
throw new ObjectGridRuntimeException(t);
}
}
/**
* Básicamente se trata de una nueva carga.
* @param s
* @param m
* @throws ObjectGridException
*/
protected void insert(ObjectMap m) throws ObjectGridException {
int size = keys.size();
for (int i = 0; i < size; i++) {
m.insert(keys.get(i), vals.get(i));
}
}
protected void persistEntities(EntityManager em) {
Iterator<Object> iter = vals.iterator();
while (iter.hasNext()) {
Object value = iter.next();
em.persist(value);
}
}
public Object reduceResults(Collection arg0) {
return arg0;
}
public void readExternal(ObjectInput in)
throws IOException, ClassNotFoundException {
int v = in.readByte();
isEntityMap = in.readBoolean();
vals = readList(in);
if (!isEntityMap) {
keys = readList(in);
}
}
public void writeExternal(ObjectOutput out) throws IOException {
out.write(1);
out.writeBoolean(isEntityMap);
writeList(out, vals);
if (!isEntityMap) {
writeList(out, keys);
}
}
public void setData(List ks, List vs) {
vals = vs;
if (!isEntityMap) {
keys = ks;
}
}
/**
* @return Devuelve isEntityMap.
*/
public boolean isEntityMap() {
return isEntityMap;
}
static public void writeList(ObjectOutput oo, Collection l)
throws IOException {
int size = l == null ? -1 : l.size();
oo.writeInt(size);
if (size > 0) {
Iterator iter = l.iterator();
while (iter.hasNext()) {
Object o = iter.next();
oo.writeObject(o);
}
}
}
public static List readList(ObjectInput oi)
throws IOException, ClassNotFoundException {
int size = oi.readInt();
if (size == -1) {
return null;
}
ArrayList list = new ArrayList(size);
for (int i = 0; i < size; ++i) {
Object o = oi.readObject();
list.add(o);
}
return list;
}
}