use of javax.cache.integration.CompletionListener in project caffeine by ben-manes.
the class LoadingCacheProxy method loadAll.
@Override
public void loadAll(Set<? extends K> keys, boolean replaceExistingValues, CompletionListener completionListener) {
requireNotClosed();
keys.forEach(Objects::requireNonNull);
CompletionListener listener = (completionListener == null) ? NullCompletionListener.INSTANCE : completionListener;
executor.execute(() -> {
try {
if (replaceExistingValues) {
int[] ignored = { 0 };
Map<K, V> loaded = cacheLoader.get().loadAll(keys);
for (Map.Entry<? extends K, ? extends V> entry : loaded.entrySet()) {
putNoCopyOrAwait(entry.getKey(), entry.getValue(), /* publishToWriter */
false, ignored);
}
} else {
getAll(keys, /* updateAccessTime */
false);
}
listener.onCompletion();
} catch (Exception e) {
listener.onException(e);
} finally {
dispatcher.ignoreSynchronous();
}
});
}
use of javax.cache.integration.CompletionListener in project hazelcast by hazelcast.
the class AbstractClientCacheProxyBase method submitLoadAllTask.
protected void submitLoadAllTask(ClientMessage request, CompletionListener completionListener, final Set<Data> keys) {
final CompletionListener compListener = completionListener != null ? completionListener : NULL_COMPLETION_LISTENER;
ClientDelegatingFuture<V> delegatingFuture = null;
try {
injectDependencies(completionListener);
final long start = System.nanoTime();
ClientInvocationFuture future = new ClientInvocation((HazelcastClientInstanceImpl) clientContext.getHazelcastInstance(), request).invoke();
SerializationService serializationService = clientContext.getSerializationService();
delegatingFuture = new ClientDelegatingFuture<V>(future, serializationService, LOAD_ALL_DECODER);
final Future delFuture = delegatingFuture;
loadAllCalls.put(delegatingFuture, compListener);
delegatingFuture.andThen(new ExecutionCallback<V>() {
@Override
public void onResponse(V response) {
loadAllCalls.remove(delFuture);
onLoadAll(keys, response, start, System.nanoTime());
compListener.onCompletion();
}
@Override
public void onFailure(Throwable t) {
loadAllCalls.remove(delFuture);
handleFailureOnCompletionListener(compListener, t);
}
});
} catch (Throwable t) {
if (delegatingFuture != null) {
loadAllCalls.remove(delegatingFuture);
}
handleFailureOnCompletionListener(compListener, t);
}
}
use of javax.cache.integration.CompletionListener in project cache2k by cache2k.
the class CacheTest method load_noLoaderNoCompletionListener.
@Test
public void load_noLoaderNoCompletionListener() {
// Added for code coverage.
Set<Long> keys = new HashSet<Long>();
keys.add(1L);
CompletionListener NULL_COMPLETION_LISTENER = null;
cache.loadAll(keys, true, NULL_COMPLETION_LISTENER);
}
use of javax.cache.integration.CompletionListener in project hazelcast by hazelcast.
the class ClientCacheProxySupport method submitLoadAllTask.
private void submitLoadAllTask(ClientMessage request, CompletionListener completionListener, final List<Data> binaryKeys) {
final CompletionListener listener = completionListener != null ? injectDependencies(completionListener) : NULL_COMPLETION_LISTENER;
ClientDelegatingFuture<V> delegatingFuture = null;
try {
final long startNanos = nowInNanosOrDefault();
ClientInvocationFuture future = new ClientInvocation(getClient(), request, getName()).invoke();
delegatingFuture = newDelegatingFuture(future, clientMessage -> Boolean.TRUE);
final Future delFuture = delegatingFuture;
loadAllCalls.put(delegatingFuture, listener);
delegatingFuture.whenCompleteAsync((response, t) -> {
if (t == null) {
loadAllCalls.remove(delFuture);
onLoadAll(binaryKeys, startNanos);
listener.onCompletion();
} else {
loadAllCalls.remove(delFuture);
handleFailureOnCompletionListener(listener, t);
}
});
} catch (Throwable t) {
if (delegatingFuture != null) {
loadAllCalls.remove(delegatingFuture);
}
handleFailureOnCompletionListener(listener, t);
}
}
use of javax.cache.integration.CompletionListener in project hazelcast by hazelcast.
the class CacheLoadAllTest method testLoadAll.
@Test
public void testLoadAll() throws InterruptedException {
ICache<String, String> cache = createCache();
String cacheName = cache.getName();
Map<String, String> entries = createAndFillEntries();
final CountDownLatch latch = new CountDownLatch(1);
cache.loadAll(entries.keySet(), true, new CompletionListener() {
@Override
public void onCompletion() {
latch.countDown();
}
@Override
public void onException(Exception e) {
latch.countDown();
}
});
assertTrue(latch.await(60, TimeUnit.SECONDS));
// Verify that load-all works
for (Map.Entry<String, String> entry : entries.entrySet()) {
String key = entry.getKey();
String expectedValue = entries.get(key);
String actualValue = cache.get(key);
assertEquals(expectedValue, actualValue);
}
Node node = getNode(hazelcastInstance);
InternalPartitionService partitionService = node.getPartitionService();
SerializationService serializationService = node.getSerializationService();
// Verify that backup of load-all works
for (Map.Entry<String, String> entry : entries.entrySet()) {
String key = entry.getKey();
String expectedValue = entries.get(key);
Data keyData = serializationService.toData(key);
int keyPartitionId = partitionService.getPartitionId(keyData);
for (int i = 0; i < INSTANCE_COUNT; i++) {
Node n = getNode(hazelcastInstances[i]);
ICacheService cacheService = n.getNodeEngine().getService(ICacheService.SERVICE_NAME);
ICacheRecordStore recordStore = cacheService.getRecordStore("/hz/" + cacheName, keyPartitionId);
assertNotNull(recordStore);
String actualValue = serializationService.toObject(recordStore.get(keyData, null));
assertEquals(expectedValue, actualValue);
}
}
}
Aggregations