Example usage for java.util.concurrent Callable call

List of usage examples for java.util.concurrent Callable call

Introduction

In this page you can find the example usage for java.util.concurrent Callable call.

Prototype

V call() throws Exception;

Source Link

Document

Computes a result, or throws an exception if unable to do so.

Usage

From source file:org.fishwife.jrugged.spring.retry.ExtendedRetryTemplateTest.java

@Test
public void test_execute_callableWithRecoveryAndState() throws Exception {
    Callable<Long> callable = Mockito.mock(Callable.class);
    RetryState retryState = Mockito.mock(RetryState.class);
    RecoveryCallback<Long> recoveryCallback = Mockito.mock(RecoveryCallback.class);

    ExtendedRetryTemplate template = new ExtendedRetryTemplate();

    Mockito.when(callable.call()).thenReturn(10L);

    Assert.assertEquals(10L, template.execute(callable, recoveryCallback, retryState).longValue());

    Mockito.verify(callable, Mockito.times(1)).call();
}

From source file:och.service.AsyncService.java

public <T> Future<T> invoke(final Callable<T> task) {
    Future<T> future = workThreads.submit(new Callable<T>() {
        @Override/*from   ww  w  . java2s . c om*/
        public T call() throws Exception {
            try {

                return task.call();

            } catch (Throwable t) {
                ExpectedException.logError(log, t, "can't invoke async");
                throw getExceptionOrThrowError(t);
            }
        }
    });
    fireAsyncEvent(listeners, future);
    return future;
}

From source file:org.apache.brooklyn.util.core.internal.winrm.pywinrm.Winrm4jTool.java

private org.apache.brooklyn.util.core.internal.winrm.WinRmToolResponse exec(
        Callable<io.cloudsoft.winrm4j.winrm.WinRmToolResponse> task) {
    Collection<Throwable> exceptions = Lists.newArrayList();
    for (int i = 0; i < execTries; i++) {
        try {//w w w  . j  a  v  a  2  s  . com
            return wrap(task.call());
        } catch (Exception e) {
            Exceptions.propagateIfFatal(e);
            Duration sleep = Duration.millis(Math.min(Math.pow(2, i) * 1000, execRetryDelay.toMilliseconds()));
            if (i == (execTries + 1)) {
                LOG.info("Propagating WinRM exception (attempt " + (i + 1) + " of " + execTries + ")", e);
            } else if (i == 0) {
                LOG.warn("Ignoring WinRM exception and will retry after " + sleep + " (attempt " + (i + 1)
                        + " of " + execTries + ")", e);
                Time.sleep(sleep);
            } else {
                LOG.debug("Ignoring WinRM exception and will retry after " + sleep + " (attempt " + (i + 1)
                        + " of " + execTries + ")", e);
                Time.sleep(sleep);
            }
            exceptions.add(e);
        }
    }
    throw Exceptions.propagate("failed to execute command", exceptions);
}

From source file:net.orfjackal.retrolambda.test.LambdaTest.java

@Test
public void lambda_returning_a_value() throws Exception {
    Callable<String> lambda = () -> "some value";

    assertThat(lambda.call(), is("some value"));
}

From source file:net.orfjackal.retrolambda.test.LambdaTest.java

@Test
public void method_references_to_overridden_inherited_methods_with_super() throws Exception {
    Callable<String> ref = super::inheritedMethod;

    assertThat(ref.call(), is("superclass version"));
}

From source file:net.orfjackal.retrolambda.test.LambdaTest.java

@Test
public void method_references_to_static_methods() throws Exception {
    long expected = System.currentTimeMillis();
    Callable<Long> ref = System::currentTimeMillis;

    assertThat(ref.call(), is(greaterThanOrEqualTo(expected)));
}

From source file:net.orfjackal.retrolambda.test.LambdaTest.java

@Test
public void method_references_to_virtual_methods_on_instance_variables() throws Exception {
    Callable<String> ref = instanceVarFoo::toUpperCase;

    assertThat(ref.call(), is("FOO"));
}

From source file:net.orfjackal.retrolambda.test.LambdaTest.java

@Test
public void method_references_to_constructors() throws Exception {
    Callable<List<String>> ref = ArrayList<String>::new;

    assertThat(ref.call(), is(instanceOf(ArrayList.class)));
}

From source file:com.sworddance.util.CUtilities.java

/**
 * Get a value from a map. If the value returned is null, then if defaultValue is provided, the {@link Callable#call()} is made and that value is set.
 * If map is {@link ConcurrentMap} then the value supplied by default is set using {@link ConcurrentMap#putIfAbsent(Object, Object)}. Otherwise
 * {@link Map#put(Object, Object)} call is made and the defaultValue-supplied value is returned (and any synchronization issues are handled by the caller).
 * @param <K> key type in map/*from   w w  w  .  j av a  2s.c om*/
 * @param <V> value type in map
 * @param map if null then null is returned
 * @param key if null then null is returned
 * @param defaultValue if a {@link ParameterizedCallable} then map and key are passed to {@link ParameterizedCallable#executeCall(Object...)}(map,key)
 * see also {@link com.sworddance.util.AbstractParameterizedCallableImpl}
 * @return the value in the map.
 */
@SuppressWarnings("unchecked")
public static <K, V> V get(Map<K, V> map, Object key, Callable<V> defaultValue) {
    V value;
    if (map != null && key != null) {
        value = map.get(key);
    } else {
        value = null;
    }
    if (value == null && defaultValue != null) {
        V callValue;
        try {
            if (defaultValue instanceof ParameterizedCallable<?>) {
                callValue = ((ParameterizedCallable<V>) defaultValue).executeCall(map, key);
            } else {
                callValue = defaultValue.call();
            }
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new ApplicationGeneralException(e);
        }
        if (callValue != null && map != null && key != null) {
            if (map instanceof ConcurrentMap<?, ?>) {
                ((ConcurrentMap<K, V>) map).putIfAbsent((K) key, callValue);
            } else {
                map.put((K) key, callValue);
            }
            // another thread may beat us to assigning the value.
            value = map.get(key);
        } else {
            value = callValue;
        }
    }
    return value;
}

From source file:acmi.l2.clientmod.xdat.XdatEditor.java

public void execute(Callable<Void> r, Consumer<Exception> exceptionConsumer, Runnable finallyCallback) {
    executor.execute(() -> {// w w  w  .j a va 2 s .  c  o  m
        Platform.runLater(() -> working.set(true));
        try {
            r.call();
        } catch (Exception e) {
            if (exceptionConsumer != null)
                exceptionConsumer.accept(e);
        } finally {
            try {
                if (finallyCallback != null)
                    finallyCallback.run();
            } finally {
                Platform.runLater(() -> working.set(false));
            }
        }
    });
}