Search in sources :

Example 21 with CacheLoaderException

use of javax.cache.integration.CacheLoaderException in project caffeine by ben-manes.

the class JCacheLoaderAdapter method loadAll.

@Override
public Map<K, Expirable<V>> loadAll(Iterable<? extends K> keys) {
    try {
        boolean statsEnabled = statistics.isEnabled();
        long start = statsEnabled ? ticker.read() : 0L;
        Map<K, Expirable<V>> result = delegate.loadAll(keys).entrySet().stream().filter(entry -> (entry.getKey() != null) && (entry.getValue() != null)).collect(Collectors.toMap(Map.Entry::getKey, entry -> new Expirable<>(entry.getValue(), expireTimeMS())));
        for (Map.Entry<K, Expirable<V>> entry : result.entrySet()) {
            dispatcher.publishCreated(cache, entry.getKey(), entry.getValue().get());
        }
        if (statsEnabled) {
            // Subtracts the load time from the get time
            statistics.recordGetTime(start - ticker.read());
        }
        return result;
    } catch (CacheLoaderException e) {
        throw e;
    } catch (RuntimeException e) {
        throw new CacheLoaderException(e);
    }
}
Also used : Ticker(com.github.benmanes.caffeine.cache.Ticker) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) Collectors(java.util.stream.Collectors) CacheLoader(javax.cache.integration.CacheLoader) CacheLoaderException(javax.cache.integration.CacheLoaderException) TimeUnit(java.util.concurrent.TimeUnit) CacheProxy(com.github.benmanes.caffeine.jcache.CacheProxy) Objects.requireNonNull(java.util.Objects.requireNonNull) Map(java.util.Map) Expirable(com.github.benmanes.caffeine.jcache.Expirable) JCacheStatisticsMXBean(com.github.benmanes.caffeine.jcache.management.JCacheStatisticsMXBean) Duration(javax.cache.expiry.Duration) EventDispatcher(com.github.benmanes.caffeine.jcache.event.EventDispatcher) CacheLoaderException(javax.cache.integration.CacheLoaderException) Expirable(com.github.benmanes.caffeine.jcache.Expirable) Map(java.util.Map)

Example 22 with CacheLoaderException

use of javax.cache.integration.CacheLoaderException in project hazelcast by hazelcast.

the class CallbackAwareClientDelegatingFutureTest method test_CallbackAwareClientDelegatingFuture.

private void test_CallbackAwareClientDelegatingFuture(boolean timeout, boolean error) throws ExecutionException, InterruptedException {
    if (timeout && error) {
        throw new IllegalArgumentException("Only one of the `timeout` and `error` parameters can be enabled at the same time!");
    }
    int timeoutMillis = timeout ? 5000 : -1;
    createCache(timeoutMillis, error);
    ClientMessage getRequest = createGetRequest(1);
    ClientMessageDecoder decoder = CACHE_GET_RESPONSE_DECODER;
    ClientInvocation invocation = new ClientInvocation(client, getRequest, 0);
    ClientInvocationFuture invocationFuture = invocation.invoke();
    final AtomicBoolean responseCalled = new AtomicBoolean();
    final AtomicBoolean failureCalled = new AtomicBoolean();
    OneShotExecutionCallback callback = new OneShotExecutionCallback() {

        @Override
        protected void onResponseInternal(Object response) {
            responseCalled.set(true);
        }

        @Override
        protected void onFailureInternal(Throwable t) {
            failureCalled.set(true);
        }
    };
    CallbackAwareClientDelegatingFuture callbackAwareInvocationFuture = new CallbackAwareClientDelegatingFuture(invocationFuture, client.getSerializationService(), decoder, callback);
    if (timeoutMillis > 0) {
        try {
            callbackAwareInvocationFuture.get(timeoutMillis / 2, TimeUnit.MILLISECONDS);
            fail("Timeout expected!");
        } catch (TimeoutException e) {
            // Timeout expected
            assertTrue(failureCalled.get());
            assertFalse(responseCalled.get());
        }
    } else {
        if (error) {
            try {
                callbackAwareInvocationFuture.get();
                fail("CacheLoaderException expected!");
            } catch (ExecutionException e) {
                // Exception expected
                assertTrue(e.getCause() instanceof CacheLoaderException);
                assertTrue(failureCalled.get());
                assertFalse(responseCalled.get());
            }
        } else {
            try {
                callbackAwareInvocationFuture.get();
                assertTrue(responseCalled.get());
                assertFalse(failureCalled.get());
            } catch (CacheLoaderException e) {
                fail("CacheLoaderException not expected!");
            }
        }
    }
}
Also used : ClientInvocation(com.hazelcast.client.spi.impl.ClientInvocation) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) ClientInvocationFuture(com.hazelcast.client.spi.impl.ClientInvocationFuture) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClientMessageDecoder(com.hazelcast.client.impl.ClientMessageDecoder) CacheLoaderException(javax.cache.integration.CacheLoaderException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 23 with CacheLoaderException

use of javax.cache.integration.CacheLoaderException in project redisson by redisson.

the class JCache method loadValue.

private V loadValue(K key) {
    V value = null;
    try {
        value = cacheLoader.load(key);
    } catch (Exception ex) {
        throw new CacheLoaderException(ex);
    }
    if (value != null) {
        long startTime = currentNanoTime();
        putValueLocked(key, value);
        cacheManager.getStatBean(this).addGetTime(currentNanoTime() - startTime);
    }
    return value;
}
Also used : CacheLoaderException(javax.cache.integration.CacheLoaderException) CacheWriterException(javax.cache.integration.CacheWriterException) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheLoaderException(javax.cache.integration.CacheLoaderException)

Example 24 with CacheLoaderException

use of javax.cache.integration.CacheLoaderException 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 25 with CacheLoaderException

use of javax.cache.integration.CacheLoaderException in project gora by apache.

the class JCacheCacheLoader method loadAll.

@Override
public Map<K, T> loadAll(Iterable<? extends K> keys) throws CacheLoaderException {
    try {
        Map<K, T> loaded = new HashMap<K, T>();
        for (K key : keys) {
            T persistent = dataStore.get(key);
            LOG.info("Loaded data bean from persistent datastore on key {}.", key.toString());
            if (persistent != null) {
                loaded.put(key, persistent);
            }
        }
        return loaded;
    } catch (GoraException e) {
        throw new CacheLoaderException(e);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) HashMap(java.util.HashMap) CacheLoaderException(javax.cache.integration.CacheLoaderException)

Aggregations

CacheLoaderException (javax.cache.integration.CacheLoaderException)34 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)12 SQLException (java.sql.SQLException)11 Connection (java.sql.Connection)8 ResultSet (java.sql.ResultSet)8 PreparedStatement (java.sql.PreparedStatement)7 CacheWriterException (javax.cache.integration.CacheWriterException)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 NoSuchElementException (java.util.NoSuchElementException)5 Nullable (org.jetbrains.annotations.Nullable)5 Person (org.apache.ignite.examples.model.Person)4 ArrayList (java.util.ArrayList)3 ExecutorService (java.util.concurrent.ExecutorService)3 EntryProcessorException (javax.cache.processor.EntryProcessorException)3 IgniteException (org.apache.ignite.IgniteException)3 Expirable (com.github.benmanes.caffeine.jcache.Expirable)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ExecutionException (java.util.concurrent.ExecutionException)2 Future (java.util.concurrent.Future)2