List of usage examples for java.util.concurrent Callable call
V call() throws Exception;
From source file:it.codingjam.lifecyclebinder.LifeCycleBinderFragment.java
@Override public <R> R addRetainedFactory(String key, Callable<R> factory, boolean addInLifeCycleAwareList) { R listener = (R) retainedObjects.get(key); if (listener == null) { try {/* www . j a v a 2 s.co m*/ listener = factory.call(); retainedObjects.put(key, listener); } catch (Exception e) { throw new RuntimeException(e); } } if (addInLifeCycleAwareList) { addLifeCycleAware((LifeCycleAware<? super T>) listener); } return listener; }
From source file:com.netflix.nicobar.groovy2.plugin.Groovy2PluginTest.java
@Test public void testLoadScriptWithInterface() throws Exception { ScriptModuleLoader moduleLoader = createGroovyModuleLoader().build(); Path scriptRootPath = GroovyTestResourceUtil.findRootPathForScript(TestScript.IMPLEMENTS_INTERFACE); ScriptArchive scriptArchive = new PathScriptArchive.Builder(scriptRootPath).setRecurseRoot(false) .addFile(TestScript.IMPLEMENTS_INTERFACE.getScriptPath()) .setModuleSpec(createGroovyModuleSpec(TestScript.IMPLEMENTS_INTERFACE.getModuleId()).build()) .build();/*w ww. j a va 2s . c om*/ moduleLoader.updateScriptArchives(Collections.singleton(scriptArchive)); // locate the class file in the module and execute it via the executor ScriptModuleExecutable<String> executable = new ScriptModuleExecutable<String>() { @SuppressWarnings({ "rawtypes", "unchecked" }) @Override public String execute(ScriptModule scriptModule) throws Exception { Class<Callable> callable = (Class<Callable>) ScriptModuleUtils.findAssignableClass(scriptModule, Callable.class); assertNotNull(callable, "couldn't find Callable for module " + scriptModule.getModuleId()); Callable<String> instance = callable.newInstance(); String result = instance.call(); return result; } }; HystrixScriptModuleExecutor<String> executor = new HystrixScriptModuleExecutor<String>( "TestModuleExecutor"); List<String> results = executor.executeModules( Collections.singletonList(TestScript.IMPLEMENTS_INTERFACE.getModuleId()), executable, moduleLoader); assertEquals(results, Collections.singletonList("I'm a Callable<String>")); }
From source file:SingleThreadRequestExecutor.java
public <V> ScheduledFuture<V> schedule(final Callable<V> callable, long delay, TimeUnit unit) { return executor.schedule(new Callable<V>() { @Override//from w w w . j a v a2 s . c om public V call() throws Exception { try { return callable.call(); } catch (Throwable e) { // This normally bad code of catch on Exception is here for a *reason*. // Future *eats* all exceptions *silently*. This clause at least allows // the exception to emit noise for debugging. This is particularly pernicious // if you have something like a NullPointerException e.printStackTrace(); throw new RuntimeException(e); } } }, delay, unit); }
From source file:org.jspringbot.keyword.expression.ExpressionHelper.java
public Object variableScope(List<Object> variables, Callable<Object> callable) throws Exception { try {//from w ww.ja va 2s. com ScopedVariableHolder.push(new HashMap<String, Object>()); initVariables(variables); return callable.call(); } finally { ScopedVariableHolder.pop(); } }
From source file:org.apache.hadoop.hbase.quotas.TestSuperUserQuotaPermissions.java
private <T> T doAsUser(UserGroupInformation ugi, Callable<T> task) throws Exception { return ugi.doAs(new PrivilegedExceptionAction<T>() { public T run() throws Exception { return task.call(); }/*from w w w. ja va 2s . co m*/ }); }
From source file:com.wolvereness.overmapped.lib.MultiProcessor.java
@Override public <T> Future<T> submit(final Callable<T> task) { super.checkShutdown(); try {/* w w w .j ava 2 s .c om*/ final T object = task.call(); return new Future<T>() { @Override public boolean cancel(final boolean mayInterruptIfRunning) { return false; } @Override public boolean isCancelled() { return false; } @Override public boolean isDone() { return true; } @Override public T get() throws InterruptedException, ExecutionException { return object; } @Override public T get(final long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { return object; } }; } catch (final Exception exception) { final Exception ex = exception; return new Future<T>() { @Override public boolean cancel(final boolean mayInterruptIfRunning) { return false; } @Override public boolean isCancelled() { return false; } @Override public boolean isDone() { return true; } @Override public T get() throws InterruptedException, ExecutionException { throw new ExecutionException(ex); } @Override public T get(final long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { throw new ExecutionException(ex); } }; } }
From source file:org.polymap.core.runtime.DefaultSessionContext.java
public <T> T execute(final Callable<T> task) throws Exception { checkDestroyed();/*w ww . j ava 2s . co m*/ SessionContext current = DefaultSessionContextProvider.currentContext.get(); if (current != null) { if (current.getSessionKey().equals(sessionKey)) { return task.call(); // throw new IllegalStateException( "Un/mapping same session context more than once is not supported yet." ); } else { throw new IllegalStateException( "Another context is mapped to this thread: " + current.getSessionKey()); } } try { DefaultSessionContextProvider.currentContext.set(this); return task.call(); } finally { DefaultSessionContextProvider.currentContext.set(null); } }
From source file:org.polymap.rhei.batik.toolkit.SimpleDialog.java
/** * Adds a 'OK' action to the button bar that just closes the dialog. * //from w ww.jav a2 s .c o m * @param task The task to perform when 'OK' is pressed. The dialog is closed * afterwards if the task returns {@link Boolean#TRUE} or * <code>null</code>. Otherwise it stays open. */ public SimpleDialog addOkAction(String label, Callable<Boolean> task) { return addAction(new Action(label) { public void run() { try { Boolean result = task.call(); if (result == null || result.booleanValue()) { SimpleDialog.this.close(); } } catch (Exception e) { StatusDispatcher.handleError("Unable to perform task.", e); } } }); }
From source file:forge.CardStorageReader.java
private void executeLoadTask(final Collection<CardRules> result, final List<Callable<List<CardRules>>> tasks, final CountDownLatch cdl) { try {// w w w . j av a 2 s. co m if (useThreadPool) { final ExecutorService executor = ThreadUtil.getComputingPool(0.5f); final List<Future<List<CardRules>>> parts = executor.invokeAll(tasks); executor.shutdown(); cdl.await(); for (final Future<List<CardRules>> pp : parts) { result.addAll(pp.get()); } } else { for (final Callable<List<CardRules>> c : tasks) { result.addAll(c.call()); } } } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } catch (final Exception e) { // this clause comes from non-threaded branch throw new RuntimeException(e); } }
From source file:io.fabric8.elasticsearch.plugin.PluginClient.java
/** * Execute a callable action directly against Elasticsearch * bypassing authorization restrictions//from ww w . j av a2 s .c o m */ public <T> T execute(Callable<T> callable) { try (StoredContext context = threadContext.stashContext()) { addCommonHeaders(); return callable.call(); } catch (Exception e) { throw new ElasticsearchException(e); } }