List of usage examples for com.google.common.util.concurrent ListenableFutureTask create
public static <V> ListenableFutureTask<V> create(Callable<V> callable)
From source file:com.vmware.photon.controller.dhcpagent.xenon.service.ReleaseIPService.java
/** * This method generates an async request to DHCP agent for * release IP for cleanup of network resources. * * @param currentState/* ww w .jav a 2s . co m*/ * @param postOperation */ private void handleReleaseIPRequest(ReleaseIPTask currentState, Operation postOperation) { initRequestId(currentState); try { ListenableFutureTask<DHCPDriver.Response> futureTask = ListenableFutureTask .create(new Callable<DHCPDriver.Response>() { @Override public DHCPDriver.Response call() throws Exception { return ((DHCPAgentXenonHost) getHost()).getDHCPDriver() .releaseIP(currentState.networkInterface, currentState.macAddress); } }); ((ListeningExecutorServiceProvider) getHost()).getListeningExecutorService().submit(futureTask); Futures.addCallback(futureTask, new FutureCallback<DHCPDriver.Response>() { @Override public void onSuccess(@javax.validation.constraints.NotNull DHCPDriver.Response response) { try { if (response.exitCode == 0) { ReleaseIPTask patchState = buildPatch(TaskState.TaskStage.FINISHED, currentState.taskState.isDirect, null); patchState.response = response; patchState.requestId = currentState.requestId; sendPatch(patchState, postOperation); } else { ReleaseIPTask patchState = buildPatch(TaskState.TaskStage.FAILED, currentState.taskState.isDirect, null); patchState.response = response; patchState.requestId = currentState.requestId; failTask(patchState, new Throwable(response.stdError), postOperation); } } catch (Throwable throwable) { failWithThrowable(currentState, throwable, postOperation); } } @Override public void onFailure(Throwable throwable) { failWithThrowable(currentState, throwable, postOperation); } }); } catch (Throwable throwable) { failWithThrowable(currentState, throwable, postOperation); } }
From source file:org.trinity.foundation.render.qt.impl.painter.PainterImpl.java
@Override public ListenableFuture<Void> raise() { final QWidget view = this.viewDiscovery.lookupView(this.model); final Callable<Void> callable = new Callable<Void>() { @Override//from w w w . j a v a 2s .co m public Void call() { LOG.debug("[view={}] raise.", view); view.raise(); return null; } }; final ListenableFutureTask<Void> futureTask = ListenableFutureTask.create(callable); QApplication.invokeLater(futureTask); return futureTask; }
From source file:com.afewmoreamps.JitCaskImpl.java
@Override public synchronized void open() throws IOException { if (m_readThreads.isShutdown()) { throw new IOException("Can't reuse a closed JitCask"); }/*from w ww.j a va2s. c o m*/ reloadJitCask(); m_outCask = new MiniCask(m_caskPath, m_nextMiniCaskIndex, null, m_maxValidValueSize); m_miniCasks.put(m_nextMiniCaskIndex, m_outCask); m_nextMiniCaskIndex++; m_syncTaskRunner = m_syncThread.scheduleAtFixedRate(new Runnable() { @Override public void run() { try { ListenableFutureTask<Long> currentSyncTask = m_nextSyncTask; m_nextSyncTask = ListenableFutureTask.create(new Callable<Long>() { @Override public Long call() { /* * Catch throwable since we don't ever want to stop syncing. */ try { final long start = System.currentTimeMillis(); sync(); final long end = System.currentTimeMillis(); final long delta = System.currentTimeMillis() - start; if (delta > m_syncInterval) { System.err.println("Missed sync interval by " + delta); } return end; } catch (Throwable t) { t.printStackTrace(); return Long.MIN_VALUE; } } }); if (currentSyncTask != null) { currentSyncTask.run(); } } catch (Throwable t) { t.printStackTrace(); } } }, 0, m_syncInterval, TimeUnit.MILLISECONDS); }
From source file:org.waveprotocol.box.server.waveletstate.block.BlockWaveletStateImpl.java
@Override public ListenableFuture flush() { checkOpenedOrClosing(); ListenableFutureTask future = ListenableFutureTask.create(savingTask); persistExecutor.execute(future); return future; }
From source file:com.twitter.common.net.UrlResolver.java
public Future<ResolvedUrl> resolveUrlAsync(final String url, final ResolvedUrlHandler handler) { Preconditions.checkNotNull("Asynchronous URL resolution cannot be performed without a valid handler.", handler);/* w w w . j ava 2s .co m*/ try { poolEntrySemaphore.acquire(); } catch (InterruptedException e) { LOG.log(Level.SEVERE, "Interrupted while waiting for thread to resolve URL: " + url, e); return null; } final ListenableFutureTask<ResolvedUrl> future = ListenableFutureTask.create(new Callable<ResolvedUrl>() { @Override public ResolvedUrl call() { return resolveUrl(url); } }); future.addListener(new Runnable() { @Override public void run() { try { handler.resolved(future); } finally { poolEntrySemaphore.release(); } } }, handlerExecutor); executor.execute(future); return future; }
From source file:org.waveprotocol.box.server.waveserver.LucenePerUserWaveViewHandlerImpl.java
@Override public ListenableFuture<Void> onParticipantRemoved(final WaveletName waveletName, final ParticipantId participant) { Preconditions.checkNotNull(waveletName); Preconditions.checkNotNull(participant); ListenableFutureTask<Void> task = ListenableFutureTask.create(new Callable<Void>() { @Override/*from ww w .j a v a2 s . com*/ public Void call() throws Exception { ReadableWaveletData waveletData; try { waveletData = waveletProvider.getReadableWaveletData(waveletName); try { removeParticipantfromIndex(waveletData, participant, nrtManager); } catch (IOException e) { LOG.log(Level.SEVERE, "Failed to update index for " + waveletName, e); throw e; } } catch (WaveServerException e) { LOG.log(Level.SEVERE, "Failed to update index for " + waveletName, e); throw e; } return null; } }); executor.execute(task); return task; }
From source file:com.vmware.photon.controller.core.DefaultDeployment.java
/** * Create default deployment after registering the client to Lotus, and producing the URL to access it. */// ww w. ja va 2 s . co m public void createDefaultDeployment(final ServiceHost host, final AuthConfig authConfig, final DeployerConfig deployerConfig, final String lbIpAddress) throws Throwable { DeployerServiceGroup deployerServiceGroup = (DeployerServiceGroup) ((PhotonControllerXenonHost) host) .getDeployer(); AuthHelperFactory authHelperFactory = deployerServiceGroup.getAuthHelperFactory(); final AuthHelper authHelper = authHelperFactory.create(); logger.info("Starting a thread to register client {} at {}:{} using user {} on tenant %s.", lbIpAddress, authConfig.getAuthServerAddress(), authConfig.getAuthServerPort(), authConfig.getAuthUserName(), authConfig.getAuthDomain()); // // Lightwave requires login name to be in format "domain/user" // ListenableFutureTask futureTask = ListenableFutureTask.create(new Callable() { @Override public Object call() throws Exception { return authHelper.getResourceLoginUri(authConfig.getAuthDomain(), authConfig.getAuthDomain() + "\\" + authConfig.getAuthUserName(), authConfig.getAuthPassword(), authConfig.getAuthServerAddress(), authConfig.getAuthServerPort(), String.format(DeployerDefaults.MGMT_UI_LOGIN_REDIRECT_URL_TEMPLATE, lbIpAddress), String.format(DeployerDefaults.MGMT_UI_LOGOUT_REDIRECT_URL_TEMPLATE, lbIpAddress)); } }); deployerServiceGroup.getListeningExecutorService().submit(futureTask); FutureCallback<AuthClientHandler.ImplicitClient> futureCallback = new FutureCallback<AuthClientHandler.ImplicitClient>() { @Override public void onSuccess(AuthClientHandler.ImplicitClient result) { loginURI = result.loginURI; logoutURI = result.logoutURI; if (authConfig.getAuthLoadBalancerAddress() != null) { loginURI = loginURI.replaceAll(authConfig.getAuthServerAddress(), authConfig.getAuthLoadBalancerAddress()); logoutURI = logoutURI.replaceAll(authConfig.getAuthServerAddress(), authConfig.getAuthLoadBalancerAddress()); } try { createDefaultDeployment(deployerConfig, authConfig, host); } catch (Throwable throwable) { throw new RuntimeException(throwable); } } @Override public void onFailure(Throwable t) { logger.error(t.getMessage()); try { createDefaultDeployment(deployerConfig, authConfig, host); } catch (Throwable throwable) { throw new RuntimeException(throwable); } } }; Futures.addCallback(futureTask, futureCallback); }
From source file:org.trinity.foundation.render.qt.impl.painter.PainterImpl.java
@Override public ListenableFuture<Void> setParent(final DisplayArea parent, final int x, final int y) { final QWidget parentView = this.viewDiscovery.lookupView(parent); final QWidget view = this.viewDiscovery.lookupView(this.model); final Callable<Void> callable = new Callable<Void>() { @Override// w w w . j a v a 2 s . c o m public Void call() { LOG.debug("[view={}] set parent parentView={}, x={}, y={}.", view, parentView, x, y); view.setParent(parentView); view.move(x, y); return null; } }; final ListenableFutureTask<Void> futureTask = ListenableFutureTask.create(callable); QApplication.invokeLater(futureTask); return futureTask; }
From source file:org.fcrepo.indexer.sparql.SparqlIndexer.java
private Callable<Void> exec(final UpdateRequest update, final boolean blocking) { if (update.getOperations().isEmpty()) { LOGGER.debug("Received empty update/remove operation."); return new Callable<Void>() { @Override// w ww . j a v a 2 s .c om public Void call() { return null; } }; } final Callable<Void> callable = new Callable<Void>() { @Override public Void call() { if (formUpdates) { // form updates final UpdateProcessor proc = createRemoteForm(update, updateBase); proc.execute(); } else { // normal SPARQL updates final UpdateProcessRemote proc = new UpdateProcessRemote(update, updateBase, emptyContext); try { proc.execute(); } catch (final Exception e) { LOGGER.error("Error executing Sparql update/remove!", e); } } return null; } }; if (blocking) { try { callable.call(); } catch (Exception e) { LOGGER.error("Error calling Sparql update/remove!, {}", e.getMessage()); } } else { final ListenableFutureTask<Void> task = ListenableFutureTask.create(callable); task.addListener(new Runnable() { @Override public void run() { LOGGER.debug("Completed Sparql update/removal."); if (LOGGER.isTraceEnabled()) { try (final OutputStream buffer = new ByteArrayOutputStream()) { final IndentedWriter out = new IndentedWriter(buffer); update.output(out); LOGGER.trace("Executed update/remove operation:\n{}", buffer.toString()); out.close(); } catch (final IOException e) { LOGGER.error("Couldn't retrieve execution of update/remove operation!", e); } } } }, executorService); executorService.submit(task); } return callable; }
From source file:com.vmware.photon.controller.deployer.dcp.task.DeleteVibTaskService.java
private void processDeleteVib(State currentState, DeploymentService.State deploymentState, HostService.State hostState) throws Throwable { HttpFileServiceClient httpFileServiceClient = HostUtils.getHttpFileServiceClientFactory(this) .create(hostState.hostAddress, hostState.userName, hostState.password); ListenableFutureTask<Integer> task = ListenableFutureTask.create( httpFileServiceClient.deleteFileFromDatastore(deploymentState.imageDataStoreNames.iterator().next(), currentState.vibPath)); HostUtils.getListeningExecutorService(this).submit(task); Futures.addCallback(task, new FutureCallback<Integer>() { @Override/*from w w w . j a v a 2s .co m*/ public void onSuccess(@Nullable Integer result) { sendStageProgressPatch(TaskState.TaskStage.FINISHED); } @Override public void onFailure(Throwable t) { failTask(t); } }); }