org.ehcache.spi.loaderwriter.BulkCacheLoadingException Java Examples

The following examples show how to use org.ehcache.spi.loaderwriter.BulkCacheLoadingException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SorLoaderWriter.java    From ehcache3-samples with Apache License 2.0 5 votes vote down vote up
@Override
public Map<Long, byte[]> loadAll(final Iterable<? extends Long> iterable) throws BulkCacheLoadingException, Exception {
  Map<Long, byte[]> results = new HashMap<>();
  for (Long key : iterable) {
    results.put(key, soRDao.loadData(key));
  }
  return results;
}
 
Example #2
Source File: LoaderWriterErrorEhcacheTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetAllWithLoaderException() throws Exception {
  when(cacheLoaderWriter.loadAll(ArgumentMatchers.<Iterable<Number>>any())).thenAnswer(invocation -> {
    @SuppressWarnings("unchecked")
    Iterable<Integer> iterable = (Iterable<Integer>) invocation.getArguments()[0];

    Map<Number, CharSequence> result = new HashMap<>();

    for (int i : iterable) {
      switch (i) {
        case 1:
          result.put(1, "one");
          break;
        case 2:
          throw new Exception("Mock Exception: cannot load 2");
        case 3:
          result.put(3, "three");
          break;
        case 4:
          result.put(4, null);
          break;
        default:
          throw new AssertionError("should not try to load key " + i);
      }
    }

    return result;
  });

  try {
    testCache.getAll(new HashSet<Number>(Arrays.asList(1, 2, 3, 4)));
    fail("expected BulkCacheLoadingException");
  } catch (BulkCacheLoadingException ex) {
    assertThat(ex.getFailures().size(), is(1));
    assertThat(ex.getFailures().get(2), is(notNullValue()));
    assertThat(ex.getSuccesses().size(), is(lessThan(4)));
    assertThat(ex.getSuccesses().containsKey(2), is(false));
  }
}
 
Example #3
Source File: EhcacheBulkMethodsITest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetAll_cache_loader_throws_exception() throws Exception {
  CacheConfigurationBuilder cacheConfigurationBuilder = CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class,
      heap(100));

  CacheLoaderWriterProvider cacheLoaderWriterProvider = mock(CacheLoaderWriterProvider.class);
  CacheLoaderWriter cacheLoaderWriter = mock(CacheLoaderWriter.class);
  when(cacheLoaderWriter.load(ArgumentMatchers.any())).thenThrow(new RuntimeException("We should not have called .load() but .loadAll()"));
  when(cacheLoaderWriter.loadAll(ArgumentMatchers.any(Iterable.class))).thenThrow(new Exception("Simulating an exception from the cache loader"));
  when(cacheLoaderWriterProvider.createCacheLoaderWriter(anyString(), ArgumentMatchers.any(CacheConfiguration.class))).thenReturn(cacheLoaderWriter);
  CacheManagerBuilder<CacheManager> managerBuilder = CacheManagerBuilder.newCacheManagerBuilder().using(cacheLoaderWriterProvider);
  CacheConfiguration<String, String> cacheConfiguration = cacheConfigurationBuilder.withLoaderWriter(cacheLoaderWriter).build();
  CacheManager cacheManager = managerBuilder.withCache("myCache", cacheConfiguration).build(true);

  Cache<String, String> myCache = cacheManager.getCache("myCache", String.class, String.class);

  Set<String> fewKeysSet = new HashSet<String>() {
    {
      add("key0");
      add("key2");
    }
  };


  // the call to getAll
  try {
    myCache.getAll(fewKeysSet);
    fail();
  } catch (BulkCacheLoadingException bcwe) {
    // since onHeapStore.bulkComputeIfAbsent sends batches of 1 element,
    assertThat(bcwe.getFailures().size(), is(2));
    assertThat(bcwe.getSuccesses().size(), is(0));
  }

}
 
Example #4
Source File: WrappedCacheLoaderWriter.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
@Override
public Map<K, V> loadAllAlways(Iterable<? extends K> keys) throws BulkCacheLoadingException, Exception {
  return delegate.loadAll(keys);
}
 
Example #5
Source File: WrappedCacheLoaderWriter.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
@Override
public Map<K, V> loadAll(Iterable<? extends K> keys) throws BulkCacheLoadingException, Exception {
  return delegate.loadAll(keys);
}
 
Example #6
Source File: LocalLoaderWriterStore.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({ "unchecked" })
private void collectSuccessesAndFailures(BulkCacheLoadingException bcle, Map<K, V> successes, Map<K, Exception> failures) {
  successes.putAll((Map<K, V>)bcle.getSuccesses());
  failures.putAll((Map<K, Exception>)bcle.getFailures());
}
 
Example #7
Source File: PersistentUserManagedEhcache.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Map<K, V> getAll(Set<? extends K> keys) throws BulkCacheLoadingException {
  return cache.getAll(keys);
}
 
Example #8
Source File: EhcacheBase.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Map<K, V> getAll(Set<? extends K> keys) throws BulkCacheLoadingException {
  return getAllInternal(keys, true);
}
 
Example #9
Source File: Cache.java    From ehcache3 with Apache License 2.0 2 votes vote down vote up
/**
 * Retrieves all values associated with the given key set.
 *
 * @param keys keys to query for, may not contain {@code null}
 * @return a map from keys to values or {@code null} if the key was not mapped
 *
 * @throws NullPointerException if the {@code Set} or any of the contained keys are {@code null}.
 * @throws BulkCacheLoadingException if loading some or all values failed
 */
Map<K, V> getAll(Set<? extends K> keys) throws BulkCacheLoadingException;
 
Example #10
Source File: Jsr107CacheLoaderWriter.java    From ehcache3 with Apache License 2.0 votes vote down vote up
Map<K, V> loadAllAlways(Iterable<? extends K> keys) throws BulkCacheLoadingException, Exception;