Example: Calling a client loader
You can use the preload method in the Loader interface to call a client loader.
Use the preload method in the Loader interface
to call a client loader:
void preloadMap(Session session, BackingMap backingMap) throws LoaderException;
This method signals the loader to preload the data into the map. A loader implementation can use a client loader to preload the data to all its partitions. For example, the JPA loader uses the client loader to preload data into the map. See Client-based JPA preload utility overview for more information.
Example: Calling a client loader with the preloadMap method
An example of how to preload the map using the client loader in the preloadMap method follows. The example first checks whether the current partition number is the same as the preload partition. If the partition number is not the same as the preload partition, no action occurs. If the partition numbers match, the client loader is called to load data into the maps. You must call the client loader in one and only one partition.void preloadMap (Session session, BackingMap backingMap) throws LoaderException {
....
ObjectGrid objectGrid = session.getObjectGrid();
int partitionId = backingMap.getPartitionId();
int numPartitions = backingMap.getPartitionManager().getNumOfPartitions();
// Only call client loader data in one partition
if (partitionId == preloadPartition) {
ClientLoader c = ClientLoaderFactory.getClientLoader();
// Call the client loader to load the data
try {
c.load(objectGrid, "CUSTOMER", "customerPU",
null, entityClass, null, null, true, null);
} catch (ObjectGridException e) {
LoaderException le = new LoaderException("Exception caught in ObjectMap " +
ogName + "." + mapName);
le.initCause(e);
throw le;
}
}
}
Remember: Configure the backingMap attribute
"preloadMode" to true, so the preload method
runs asynchronously. Otherwise, the preload method blocks the ObjectGrid
instance from being activated.