List of usage examples for java.util.concurrent Callable call
V call() throws Exception;
From source file:org.nuxeo.common.utils.JDBCUtils.java
/** * Tries to do a JDBC call even when the server is overloaded. * <p>//from w ww . ja v a 2s .c om * Oracle has problems opening and closing many connections in a short time span (ORA-12516, ORA-12519). It seems to * have something to do with how closed sessions are not immediately accounted for by Oracle's PMON (process * monitor). When we get these errors, we retry a few times with exponential backoff. * * @param callable the callable * @return the returned value */ public static <V> V callWithRetry(Callable<V> callable) throws SQLException { for (int tryNo = 1;; tryNo++) { try { return callable.call(); } catch (SQLException e) { if (tryNo >= MAX_TRIES) { throw e; } int errorCode = e.getErrorCode(); if (errorCode != 12516 && errorCode != 12519) { throw e; } // Listener refused the connection with the following error: // ORA-12519, TNS:no appropriate service handler found // ORA-12516, TNS:listener could not find available handler with matching protocol stack if (log.isDebugEnabled()) { log.debug(String.format("Connections open too fast, retrying in %ds: %s", Integer.valueOf(tryNo), e.getMessage().replace("\n", " "))); } try { Thread.sleep(1000 * tryNo); } catch (InterruptedException ie) { // deals with interrupt below throw ExceptionUtils.runtimeException(e); } } catch (Exception e) { // deals with interrupt below throw ExceptionUtils.runtimeException(e); } } }
From source file:com.yihaodian.architecture.zkclient.TestUtil.java
/** * This waits until the provided {@link Callable} returns an object that is equals to the given expected value or * the timeout has been reached. In both cases this method will return the return value of the latest * {@link Callable} execution.//w w w. j av a2 s.c om * * @param expectedValue * The expected value of the callable. * @param callable * The callable. * @param <T> * The return type of the callable. * @param timeUnit * The timeout timeunit. * @param timeout * The timeout. * @return the return value of the latest {@link Callable} execution. * @throws Exception * @throws InterruptedException */ public static <T> T waitUntil(T expectedValue, Callable<T> callable, TimeUnit timeUnit, long timeout) throws Exception { long startTime = System.currentTimeMillis(); do { T actual = callable.call(); if (expectedValue.equals(actual)) { return actual; } if (System.currentTimeMillis() > startTime + timeUnit.toMillis(timeout)) { return actual; } Thread.sleep(50); } while (true); }
From source file:com.google.inject.PerformanceComparison.java
static void validate(Callable<Foo> t) throws Exception { Foo foo = t.call(); assertEquals(5, foo.i);//from w ww. j ava2 s . c om assertEquals("test", foo.s); assertSame(foo.bar.getTee(), foo.copy.getTee()); assertEquals(5, foo.bar.getI()); assertEquals("test", foo.bar.getTee().getS()); }
From source file:shiver.me.timbers.junit.runner.tomcat.TomcatContainer.java
static <T> T withException(Callable<T> callable) { try {//w w w. java 2s . c o m return callable.call(); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:org.eclipse.jubula.rc.javafx.driver.EventThreadQueuerJavaFXImpl.java
/** * Executes the given Callable on the JavaFX-Thread and waits for the * termination//from w w w .jav a2 s. c o m * * @param name * a name to identifier which Callable is being executed * @param <V> * return value type * @param call * the Callable * @return * @return the return value of the given Callable * @throws ExecutionException * @throws InterruptedException */ public static <V> V invokeAndWait(String name, Callable<V> call) { if (Platform.isFxApplicationThread()) { try { return call.call(); } catch (Exception e) { // the run() method from IRunnable has thrown an exception // -> log on info // -> throw a StepExecutionException Throwable thrown = e.getCause(); if (thrown instanceof StepExecutionException) { if (log.isInfoEnabled()) { log.info(e); } throw (StepExecutionException) thrown; } // any other (unchecked) Exception from IRunnable.run() log.error("exception thrown by '" + name //$NON-NLS-1$ + "':", thrown); //$NON-NLS-1$ throw new StepExecutionException(thrown); } } try { FutureTask<V> task = new FutureTask<>(call); Platform.runLater(task); return task.get(); } catch (InterruptedException ie) { // this (the waiting) thread was interrupted -> error log.error(ie); throw new StepExecutionException(ie); } catch (ExecutionException ee) { // the run() method from IRunnable has thrown an exception // -> log on info // -> throw a StepExecutionException Throwable thrown = ee.getCause(); if (thrown instanceof StepExecutionException) { if (log.isInfoEnabled()) { log.info(ee); } throw (StepExecutionException) thrown; } // any other (unchecked) Exception from IRunnable.run() log.error("exception thrown by '" + name //$NON-NLS-1$ + "':", thrown); //$NON-NLS-1$ throw new StepExecutionException(thrown); } }
From source file:com.anrisoftware.globalpom.threads.listenablefuture.DefaultListenableFuture.java
private static <V> Callable<V> createExceptionCallable(final Callable<V> callable) { return new Callable<V>() { @Override/* w w w . j av a 2 s . c om*/ public V call() throws Exception { try { return callable.call(); } catch (Exception e) { logger.error("", e); throw e; } } }; }
From source file:com.sworddance.util.WeakProxy.java
/** * @param actual// ww w.j av a 2 s . c o m * @return */ private static <T> T invokeCallable(Callable<T> restoreCallable) { if (restoreCallable == null) { return null; } try { return restoreCallable.call(); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new ApplicationGeneralException(e); } }
From source file:com.hazelcast.qasonar.utils.GitHubUtils.java
private static Object execute(TimeTrackerLabel label, Callable callable) { long started = System.nanoTime(); try {/*w ww . j av a 2 s.c o m*/ while (true) { try { return callable.call(); } catch (Throwable t) { int counter = EXCEPTION_COUNTER.incrementAndGet(); if (counter % GITHUB_EXCEPTION_LOG_FREQUENCY == 0) { counter = 0; EXCEPTION_COUNTER.set(0); t.printStackTrace(); } sleepMillis(GITHUB_EXCEPTION_DELAY_MILLIS * counter); } } } finally { record(label, System.nanoTime() - started); } }
From source file:org.geowebcache.util.FileMatchers.java
/** * Executes the given {@link Callable} and then returns a matcher for values of * {@link System.currentTimeMillis} during the execution. * @param stuffToDo//from w w w .j a v a2 s.c o m * @return * @throws any exceptions thrown by stuffToDo */ public static Matcher<Long> whileRunning(Callable<Void> stuffToDo) throws Exception { final long start = System.currentTimeMillis(); stuffToDo.call(); final long end = System.currentTimeMillis(); return both(greaterThan(start)).and(lessThan(end)); }
From source file:org.apache.ranger.knox.client.KnoxClient.java
public static <T> T timedTask(Callable<T> callableObj, long timeout, TimeUnit timeUnit) throws Exception { return callableObj.call(); }