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:com.example.querybuilder.server.Jdbc.java

public static <V> V executeTransaction(Connection connection, Callable<V> callable) {
    try {//w w  w . jav  a 2s.c om
        try {
            connection.setAutoCommit(false);
            V value = callable.call();
            return value;
        } finally {
            connection.setAutoCommit(true);
        }
    } catch (SQLException e) {
        throw new SqlRuntimeException(e);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.hadoop.fs.s3a.S3ATestUtils.java

/**
 * Repeatedly attempt a callback until timeout or a {@link FailFastException}
 * is raised. This is modeled on ScalaTests {@code eventually(Closure)} code.
 * @param timeout timeout//  w  w  w.ja  v a 2 s . c  o  m
 * @param callback callback to invoke
 * @throws FailFastException any fast-failure
 * @throws Exception the exception which caused the iterator to fail
 */
public static void eventually(int timeout, Callable<Void> callback) throws Exception {
    Exception lastException;
    long endtime = System.currentTimeMillis() + timeout;
    do {
        try {
            callback.call();
            return;
        } catch (FailFastException e) {
            throw e;
        } catch (Exception e) {
            lastException = e;
        }
        Thread.sleep(500);
    } while (endtime > System.currentTimeMillis());
    throw lastException;
}

From source file:nl.strohalm.cyclos.utils.access.LoggedUser.java

public static <T> T runAs(final User user, final String remoteAddress, final Callable<T> callable) {
    final Map<String, Object> previousAttributes = ATTRIBUTES.get();
    try {//from   w  w  w. j  av a2 s  .co m
        init(user, remoteAddress);
        return callable.call();
    } catch (final RuntimeException e) {
        throw e;
    } catch (final Exception e) {
        throw new RuntimeException(e);
    } finally {
        ATTRIBUTES.set(previousAttributes);
    }
}

From source file:org.fcrepo.apix.integration.KarafIT.java

/**
 * Attempt a task a given number of times.
 *
 * @param times number of times/*from   w w  w .  j av a  2s .  c  o  m*/
 * @param it the task
 * @return the result.
 */
public static <T> T attempt(final int times, final Callable<T> it) {

    Throwable caught = null;

    for (int tries = 0; tries < times; tries++) {
        try {
            return it.call();
        } catch (final Throwable e) {
            caught = e;
            try {
                Thread.sleep(1000);
                System.out.println(".");
            } catch (final InterruptedException i) {
                Thread.currentThread().interrupt();
                return null;
            }
        }
    }
    throw new RuntimeException("Failed executing task", caught);
}

From source file:org.apache.cassandra.Util.java

public static void expectException(Callable<?> callable, Class<?> exception) {
    boolean thrown = false;

    try {/*from   w  ww  .ja va 2  s . c om*/
        callable.call();
    } catch (Throwable e) {
        assert e.getClass().equals(exception) : e.getClass().getName() + " is not " + exception.getName();
        thrown = true;
    }

    assert thrown : exception.getName() + " not received";
}

From source file:org.lgna.common.ComponentThread.java

static public <V> V invokeOnComponentThreadAndWait(final Callable<V> call) {
    if (ComponentThread.isComponentThread()) {
        try {/*from w  ww. ja v a  2s  .  c  om*/
            return call.call();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    } else {
        final ComponentThreadReturn<V> returnValue = new ComponentThreadReturn<>();
        ComponentThread thread = new ComponentThread(() -> {
            try {
                returnValue.returnValue = call.call();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }, "invoke-util");
        try {
            thread.start();
            thread.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return returnValue.returnValue;
    }
}

From source file:org.apache.jackrabbit.oak.plugins.document.persistentCache.BroadcastTest.java

private static boolean waitFor(Callable<Boolean> call, int timeout) {
    long start = System.currentTimeMillis();
    while (true) {
        try {/*from   w  w  w.  j a v  a2  s  .c  o m*/
            Thread.sleep(1);
        } catch (InterruptedException e1) {
            // ignore
        }
        long time = System.currentTimeMillis() - start;
        try {
            if (call.call()) {
                return true;
            }
        } catch (Exception e) {
            throw new AssertionError(e);
        }
        if (time > timeout) {
            return false;
        }
    }
}

From source file:och.util.Util.java

public static <T> T inLock(Lock lock, Callable<T> body) throws Exception {
    lock.lock();/*w  ww . j a  v  a2 s . c  om*/
    try {
        return body.call();
    } finally {
        lock.unlock();
    }
}

From source file:org.zenoss.zep.dao.impl.DaoUtils.java

/**
 * Attempts to execute the specified method, returning the result. If a deadlock exception is detected, then the
 * method is retried up to {@link #NUM_DEADLOCK_RETRIES} times.
 *
 * @param callable The callable to invoke.
 * @param <T> The return type of the callable.
 * @return The result of calling the callable method.
 * @throws Exception If an exception occurs (other than a deadlock exception) or if the maximum number of deadlock
 *                   retries is exhausted.
 *///from w  w  w. j  a v a 2  s  .co  m
public static <T> T deadlockRetry(Callable<T> callable) throws Exception {
    Exception lastException;
    int i = 0;
    do {
        ++i;
        try {
            return callable.call();
        } catch (Exception e) {
            if (!ZepUtils.isExceptionOfType(e, DeadlockLoserDataAccessException.class)) {
                throw e;
            }
            // Retry transaction.
            lastException = e;
        }
    } while (i < NUM_DEADLOCK_RETRIES);

    throw lastException;
}

From source file:io.github.retz.web.ClientHelper.java

public static Job waitForStart(Job job, Client c, Callable<Boolean> terminate)
        throws IOException, TimeoutException {
    Job current = job;//from w w w  .  ja  v a  2 s .  c  om
    int interval = INITAL_INTERVAL_MSEC;
    while (current.state() == Job.JobState.QUEUED) {
        maybeSleep(interval);
        interval = Math.min(interval * 2, MAX_INTERVAL_MSEC);

        try {
            if (terminate != null && terminate.call()) {
                throw new TimeoutException("Timeout at waitForStart");
            }
        } catch (TimeoutException e) {
            throw e;
        } catch (Exception e) {
            LOG.error(e.toString(), e);
            return null; // I don't know how to handle it
        }

        Response res = c.getJob(job.id());
        if (res instanceof GetJobResponse) {
            GetJobResponse getJobResponse = (GetJobResponse) res;
            if (getJobResponse.job().isPresent()) {
                current = getJobResponse.job().get();
                continue;
            }
        } else {
            LOG.error(res.status());
            throw new IOException(res.status());
        }
    }
    return current;
}