use of org.cache2k.CacheOperationCompletionListener in project cache2k by cache2k.
the class JCacheAdapter method loadAll.
@Override
public void loadAll(final Set<? extends K> keys, final boolean replaceExistingValues, final CompletionListener completionListener) {
checkClosed();
if (!loaderConfigured) {
if (completionListener != null) {
completionListener.onCompletion();
}
return;
}
CacheOperationCompletionListener l = null;
if (completionListener != null) {
l = new CacheOperationCompletionListener() {
@Override
public void onCompleted() {
try {
for (K k : keys) {
cache.peek(k);
}
} catch (CacheLoaderException ex) {
completionListener.onException(ex);
return;
}
completionListener.onCompletion();
}
@Override
public void onException(Throwable _exception) {
if (_exception instanceof Exception) {
completionListener.onException((Exception) _exception);
} else {
completionListener.onException(new CacheLoaderException(_exception));
}
}
};
}
if (replaceExistingValues) {
cache.reloadAll(keys, l);
} else {
cache.loadAll(keys, l);
}
}
use of org.cache2k.CacheOperationCompletionListener in project cache2k by cache2k.
the class WiredCache method loadAll.
@Override
public void loadAll(final Iterable<? extends K> _keys, final CacheOperationCompletionListener l) {
checkLoaderPresent();
final CacheOperationCompletionListener _listener = l != null ? l : HeapCache.DUMMY_LOAD_COMPLETED_LISTENER;
Set<K> _keysToLoad = heapCache.checkAllPresent(_keys);
if (_keysToLoad.isEmpty()) {
_listener.onCompleted();
return;
}
final AtomicInteger _countDown = new AtomicInteger(_keysToLoad.size());
for (K k : _keysToLoad) {
final K key = k;
Runnable r = new HeapCache.RunWithCatch(this) {
@Override
public void action() {
try {
load(key);
} finally {
if (_countDown.decrementAndGet() == 0) {
_listener.onCompleted();
}
}
}
};
try {
heapCache.loaderExecutor.execute(r);
} catch (RejectedExecutionException ex) {
r.run();
}
}
}
use of org.cache2k.CacheOperationCompletionListener in project cache2k by cache2k.
the class WiredCache method prefetchAll.
@Override
public void prefetchAll(final Iterable<? extends K> _keys, final CacheOperationCompletionListener l) {
final CacheOperationCompletionListener _listener = l != null ? l : HeapCache.DUMMY_LOAD_COMPLETED_LISTENER;
if (loader == null) {
_listener.onCompleted();
return;
}
final AtomicInteger _count = new AtomicInteger(2);
try {
Set<K> _keysToLoad = heapCache.checkAllPresent(_keys);
for (K k : _keysToLoad) {
final K key = k;
Runnable r = new HeapCache.RunWithCatch(this) {
@Override
public void action() {
try {
load(key);
} finally {
if (_count.decrementAndGet() == 0) {
_listener.onCompleted();
}
}
}
};
try {
heapCache.getPrefetchExecutor().execute(r);
_count.incrementAndGet();
} catch (RejectedExecutionException ignore) {
}
}
} finally {
if (_count.addAndGet(-2) == 0) {
_listener.onCompleted();
}
}
}
use of org.cache2k.CacheOperationCompletionListener in project cache2k by cache2k.
the class WiredCache method reloadAll.
@Override
public void reloadAll(final Iterable<? extends K> _keys, final CacheOperationCompletionListener l) {
checkLoaderPresent();
final CacheOperationCompletionListener _listener = l != null ? l : HeapCache.DUMMY_LOAD_COMPLETED_LISTENER;
Set<K> _keySet = heapCache.generateKeySet(_keys);
final AtomicInteger _countDown = new AtomicInteger(_keySet.size());
for (K k : _keySet) {
final K key = k;
Runnable r = new HeapCache.RunWithCatch(this) {
@Override
public void action() {
try {
execute(key, SPEC.UNCONDITIONAL_LOAD);
} finally {
if (_countDown.decrementAndGet() == 0) {
_listener.onCompleted();
}
}
}
};
try {
heapCache.loaderExecutor.execute(r);
} catch (RejectedExecutionException ex) {
r.run();
}
}
}
Aggregations