Search in sources :

Example 76 with UncheckedExecutionException

use of com.google.common.util.concurrent.UncheckedExecutionException in project mlib by myshzzx.

the class LocalCacheAspect method getLocalCache.

@Around("local()")
public Object getLocalCache(final ProceedingJoinPoint pjp) throws Throwable {
    MethodSignature signature = (MethodSignature) pjp.getSignature();
    Method method = pjp.getTarget().getClass().getMethod(signature.getName(), signature.getMethod().getParameterTypes());
    Object[] args = pjp.getArgs();
    LocalCache conf = method.getAnnotation(LocalCache.class);
    Cache<Object, AtomicReference<?>> cache = getCache(method, conf);
    if (conf.isMultiKey()) {
        // MultiKeyInvokeWrap keyInfo = parseMultiKey(conf.keyExp(), pjp.getTarget(), signature.getMethod(), args);
        return null;
    } else {
        // single key
        Object key;
        if (StringUtils.isEmpty(conf.keyExp())) {
            // 自动生成 key
            key = genKey(args);
        } else
            key = parseSingleKey(conf.keyExp(), method, args);
        try {
            AtomicReference<?> resultWrapper = cache.get(key, new Callable<AtomicReference<?>>() {

                @Override
                public AtomicReference<?> call() throws Exception {
                    try {
                        return new AtomicReference<>(pjp.proceed());
                    } catch (Throwable t) {
                        if (t instanceof Exception)
                            throw (Exception) t;
                        else
                            throw new Exception(t);
                    }
                }
            });
            return resultWrapper.get();
        } catch (ExecutionException | UncheckedExecutionException | ExecutionError e) {
            throw e.getCause();
        }
    }
}
Also used : ExecutionError(com.google.common.util.concurrent.ExecutionError) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) MethodSignature(org.aspectj.lang.reflect.MethodSignature) AtomicReference(java.util.concurrent.atomic.AtomicReference) Method(java.lang.reflect.Method) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) Around(org.aspectj.lang.annotation.Around)

Example 77 with UncheckedExecutionException

use of com.google.common.util.concurrent.UncheckedExecutionException in project guava by hceylan.

the class CacheLoadingTest method testConcurrentLoadingUncheckedException.

/**
 * On a concurrent computation that throws an unchecked exception, all threads should get the
 * (wrapped) exception, with the loader called only once. The result should not be cached (a later
 * request should call the loader again).
 */
private static void testConcurrentLoadingUncheckedException(CacheBuilder<Object, Object> builder) throws InterruptedException {
    int count = 10;
    final AtomicInteger callCount = new AtomicInteger();
    final CountDownLatch startSignal = new CountDownLatch(count + 1);
    final RuntimeException e = new RuntimeException();
    LoadingCache<String, String> cache = builder.build(new CacheLoader<String, String>() {

        @Override
        public String load(String key) throws InterruptedException {
            callCount.incrementAndGet();
            startSignal.await();
            throw e;
        }
    });
    List<Object> result = doConcurrentGet(cache, "bar", count, startSignal);
    assertEquals(1, callCount.get());
    for (int i = 0; i < count; i++) {
        // doConcurrentGet alternates between calling getUnchecked and calling get, but an unchecked
        // exception thrown by the loader is always wrapped as an UncheckedExecutionException.
        assertTrue(result.get(i) instanceof UncheckedExecutionException);
        assertSame(e, ((UncheckedExecutionException) result.get(i)).getCause());
    }
    // subsequent calls should call the loader again, not get the old exception
    try {
        cache.getUnchecked("bar");
        fail();
    } catch (UncheckedExecutionException expected) {
    }
    assertEquals(2, callCount.get());
}
Also used : UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 78 with UncheckedExecutionException

use of com.google.common.util.concurrent.UncheckedExecutionException in project guava by hceylan.

the class CacheLoadingTest method testReloadAfterFailure.

public void testReloadAfterFailure() throws ExecutionException {
    final AtomicInteger count = new AtomicInteger();
    final Exception e = new IllegalStateException("exception to trigger failure on first load()");
    CacheLoader<Integer, String> failOnceFunction = new CacheLoader<Integer, String>() {

        @Override
        public String load(Integer key) throws Exception {
            if (count.getAndIncrement() == 0) {
                throw e;
            }
            return key.toString();
        }
    };
    CountingRemovalListener<Integer, String> removalListener = countingRemovalListener();
    LoadingCache<Integer, String> cache = CacheBuilder.newBuilder().removalListener(removalListener).build(failOnceFunction);
    try {
        cache.getUnchecked(1);
        fail();
    } catch (UncheckedExecutionException ue) {
        assertSame(e, ue.getCause());
    }
    assertEquals("1", cache.getUnchecked(1));
    assertEquals(0, removalListener.getCount());
    count.set(0);
    cache.refresh(2);
    checkLoggedCause(e);
    assertEquals("2", cache.getUnchecked(2));
    assertEquals(0, removalListener.getCount());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) IOException(java.io.IOException) InvalidCacheLoadException(com.google.common.cache.CacheLoader.InvalidCacheLoadException) ExecutionException(java.util.concurrent.ExecutionException)

Example 79 with UncheckedExecutionException

use of com.google.common.util.concurrent.UncheckedExecutionException in project guava by hceylan.

the class CacheLoadingTest method testConcurrentLoadingCheckedException.

/**
 * On a concurrent computation that throws a checked exception, all threads should get the
 * (wrapped) exception, with the loader called only once. The result should not be cached (a later
 * request should call the loader again).
 */
private static void testConcurrentLoadingCheckedException(CacheBuilder<Object, Object> builder) throws InterruptedException {
    int count = 10;
    final AtomicInteger callCount = new AtomicInteger();
    final CountDownLatch startSignal = new CountDownLatch(count + 1);
    final IOException e = new IOException();
    LoadingCache<String, String> cache = builder.build(new CacheLoader<String, String>() {

        @Override
        public String load(String key) throws IOException, InterruptedException {
            callCount.incrementAndGet();
            startSignal.await();
            throw e;
        }
    });
    List<Object> result = doConcurrentGet(cache, "bar", count, startSignal);
    assertEquals(1, callCount.get());
    for (int i = 0; i < count; i++) {
        // doConcurrentGet alternates between calling getUnchecked and calling get. If we call get(),
        // we should get an ExecutionException; if we call getUnchecked(), we should get an
        // UncheckedExecutionException.
        int mod = i % 3;
        if (mod == 0 || mod == 2) {
            assertTrue(result.get(i) instanceof ExecutionException);
            assertSame(e, ((ExecutionException) result.get(i)).getCause());
        } else {
            assertTrue(result.get(i) instanceof UncheckedExecutionException);
            assertSame(e, ((UncheckedExecutionException) result.get(i)).getCause());
        }
    }
    // subsequent calls should call the loader again, not get the old exception
    try {
        cache.getUnchecked("bar");
        fail();
    } catch (UncheckedExecutionException expected) {
    }
    assertEquals(2, callCount.get());
}
Also used : UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) ExecutionException(java.util.concurrent.ExecutionException)

Example 80 with UncheckedExecutionException

use of com.google.common.util.concurrent.UncheckedExecutionException in project guava by hceylan.

the class AbstractLoadingCacheTest method testGetUnchecked_checked.

public void testGetUnchecked_checked() {
    final Exception cause = new Exception();
    final AtomicReference<Object> valueRef = new AtomicReference<Object>();
    LoadingCache<Object, Object> cache = new AbstractLoadingCache<Object, Object>() {

        @Override
        public Object get(Object key) throws ExecutionException {
            Object v = valueRef.get();
            if (v == null) {
                throw new ExecutionException(cause);
            }
            return v;
        }

        @Override
        public Object getIfPresent(Object key) {
            return valueRef.get();
        }
    };
    try {
        cache.getUnchecked(new Object());
        fail();
    } catch (UncheckedExecutionException expected) {
        assertEquals(cause, expected.getCause());
    }
    Object newValue = new Object();
    valueRef.set(newValue);
    assertSame(newValue, cache.getUnchecked(new Object()));
}
Also used : UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) AtomicReference(java.util.concurrent.atomic.AtomicReference) ExecutionException(java.util.concurrent.ExecutionException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException)

Aggregations

UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)101 ExecutionException (java.util.concurrent.ExecutionException)60 IOException (java.io.IOException)31 InvalidCacheLoadException (com.google.common.cache.CacheLoader.InvalidCacheLoadException)22 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 Test (org.junit.Test)9 Map (java.util.Map)8 ArrayList (java.util.ArrayList)7 File (java.io.File)6 TimeoutException (java.util.concurrent.TimeoutException)6 Stopwatch (com.google.common.base.Stopwatch)5 ImmutableMap (com.google.common.collect.ImmutableMap)5 HashMap (java.util.HashMap)5 List (java.util.List)5 NoSuchElementException (java.util.NoSuchElementException)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 InputStream (java.io.InputStream)4 Callable (java.util.concurrent.Callable)4 SirixIOException (org.sirix.exception.SirixIOException)4 TypeSignature (com.facebook.presto.common.type.TypeSignature)3