Search in sources :

Example 1 with CacheOperationCompletionListener

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);
    }
}
Also used : CacheOperationCompletionListener(org.cache2k.CacheOperationCompletionListener) CacheLoaderException(javax.cache.integration.CacheLoaderException) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheLoaderException(javax.cache.integration.CacheLoaderException) CacheWriterException(org.cache2k.integration.CacheWriterException) NoSuchElementException(java.util.NoSuchElementException)

Example 2 with CacheOperationCompletionListener

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();
        }
    }
}
Also used : CacheOperationCompletionListener(org.cache2k.CacheOperationCompletionListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 3 with CacheOperationCompletionListener

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();
        }
    }
}
Also used : CacheOperationCompletionListener(org.cache2k.CacheOperationCompletionListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 4 with CacheOperationCompletionListener

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();
        }
    }
}
Also used : CacheOperationCompletionListener(org.cache2k.CacheOperationCompletionListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Aggregations

CacheOperationCompletionListener (org.cache2k.CacheOperationCompletionListener)4 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 NoSuchElementException (java.util.NoSuchElementException)1 CacheLoaderException (javax.cache.integration.CacheLoaderException)1 EntryProcessorException (javax.cache.processor.EntryProcessorException)1 CacheWriterException (org.cache2k.integration.CacheWriterException)1