Bulk-Load Sample Code
The following sample code shows how a clustered application with BigMemory Max can use the bulk-load API to optimize a bulk-load operation:
import net.sf.ehcache.Cache;
public class MyBulkLoader {
CacheManager cacheManager = new CacheManager(); // Assumes local ehcache.xml.
Cache cache = cacheManager.getEhcache(\"myCache\"); // myCache defined in ehcache.xml.
cache.setNodeBulkLoadEnabled(true); // myCache is now in bulk mode.
// Load data into myCache.
// Done, now set myCache back to its configured consistency mode.
cache.setNodeBulkLoadEnabled(false);
}
Note: There exists a potential error with
non-singleton CacheManagers. The Ehcache API does not allow multiple
CacheManagers with the same name to exist in the same JVM. CacheManager()
constructors creating non-singleton CacheManagers can violate this rule,
causing an error. If your code might create multiple CacheManagers of the same
name in the same JVM, avoid this error by using the static
CacheManager.create() methods, which always return the named (or default
unnamed) CacheManager if it already exists in that JVM. If the named (or
default unnamed) CacheManager does not exist, the CacheManager.create() methods
create it. For more information, see the Javadoc at
http://ehcache.org/apidocs/2.10.1/ for the
CacheManager.
On another node, application code that intends to touch myCache can run or wait, based on whether myCache is consistent or not:
...
if (!cache.isClusterBulkLoadEnabled()) {
// Do some work.
}
else {
cache.waitUntilBulkLoadComplete()
// Do the work when waitUntilBulkLoadComplete() returns.
}
...
Waiting may not be necessary if the code can handle potentially stale data:
...
if (!cache.isClusterBulkLoadEnabled()) {
// Do some work.
}
else {
// Do some work knowing that data in myCache may be stale.
}
...