@Override public ListenableFuture<?> submitListenable (Runnable task) { ExecutorService executor = getThreadPoolExecutor (); try { ListenableFutureTask<Object> future = new ListenableFutureTask<Object> (task, null); executor.execute (future); return future; } catch (RejectedExecutionException ex) { throw new TaskRejectedException ("Executor [" + executor + "] did not accept task: " + task, ex); } }
@Override public <T> ListenableFuture<T> submitListenable (Callable<T> task) { ExecutorService executor = getThreadPoolExecutor (); try { ListenableFutureTask<T> future = new ListenableFutureTask<T> (task); executor.execute (future); return future; } catch (RejectedExecutionException ex) { throw new TaskRejectedException ("Executor [" + executor + "] did not accept task: " + task, ex); } }
@Override public ListenableFuture<?> submitListenable(Runnable task) { ExecutorService executor = getThreadPoolExecutor(); try { ListenableFutureTask<Object> future = new ListenableFutureTask<Object>(task, null); executor.execute(future); return future; } catch (RejectedExecutionException ex) { throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex); } }
@Override public <T> ListenableFuture<T> submitListenable(Callable<T> task) { ExecutorService executor = getThreadPoolExecutor(); try { ListenableFutureTask<T> future = new ListenableFutureTask<T>(task); executor.execute(future); return future; } catch (RejectedExecutionException ex) { throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex); } }
@Override public ListenableFuture<?> submitListenable(Runnable task) { ExecutorService executor = getScheduledExecutor(); try { ListenableFutureTask<Object> future = new ListenableFutureTask<Object>(task, null); executor.execute(errorHandlingTask(future, false)); return future; } catch (RejectedExecutionException ex) { throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex); } }
@Override public <T> ListenableFuture<T> submitListenable(Callable<T> task) { ExecutorService executor = getScheduledExecutor(); try { ListenableFutureTask<T> future = new ListenableFutureTask<T>(task); executor.execute(errorHandlingTask(future, false)); return future; } catch (RejectedExecutionException ex) { throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex); } }
public static void main(String[] args) { ListenableFutureTask listenableFutureTask = new ListenableFutureTask(() -> { log.info("listenable future begin to execute"); TimeUnit.SECONDS.sleep(5); return "listenable future task done"; }); listenableFutureTask.addCallback(new ListenableFutureCallback() { @Override public void onFailure(Throwable ex) { log.info("listenable future task execute failed, cause: {}", ex.getMessage()); } @Override public void onSuccess(Object result) { log.info("listenable future task execute successfully, result: {}", result); } }); ExecutorService executor = Executors.newSingleThreadExecutor(); try { executor.execute(listenableFutureTask); //log.info(listenableFutureTask.get()); } finally { executor.shutdown(); } }
@MessageMapping("failure") public ListenableFutureTask<String> handleListenableFutureException() { this.future = new ListenableFutureTask<String>(() -> { throw new IllegalStateException(); }); return this.future; }
private static ListenableFutureTask<Void> getVoidFuture() { ListenableFutureTask<Void> futureTask = new ListenableFutureTask<>(new Callable<Void>() { @Override public Void call() throws Exception { return null; } }); futureTask.run(); return futureTask; }
private static ListenableFutureTask<Boolean> getBooleanFuture() { ListenableFutureTask<Boolean> futureTask = new ListenableFutureTask<>(new Callable<Boolean>() { @Override public Boolean call() throws Exception { return null; } }); futureTask.run(); return futureTask; }
@Override public ListenableFuture<?> submitListenable(Runnable task) { try { ListenableFutureTask<Object> future = new ListenableFutureTask<Object>(task, null); this.concurrentExecutor.execute(future); return future; } catch (RejectedExecutionException ex) { throw new TaskRejectedException( "Executor [" + this.concurrentExecutor + "] did not accept task: " + task, ex); } }
@Override public <T> ListenableFuture<T> submitListenable(Callable<T> task) { try { ListenableFutureTask<T> future = new ListenableFutureTask<T>(task); this.concurrentExecutor.execute(future); return future; } catch (RejectedExecutionException ex) { throw new TaskRejectedException( "Executor [" + this.concurrentExecutor + "] did not accept task: " + task, ex); } }
@Override protected ListenableFuture<WebSocketSession> doHandshakeInternal(WebSocketHandler webSocketHandler, HttpHeaders headers, final URI uri, List<String> protocols, List<WebSocketExtension> extensions, Map<String, Object> attributes) { int port = getPort(uri); InetSocketAddress localAddress = new InetSocketAddress(getLocalHost(), port); InetSocketAddress remoteAddress = new InetSocketAddress(uri.getHost(), port); final StandardWebSocketSession session = new StandardWebSocketSession(headers, attributes, localAddress, remoteAddress); final ClientEndpointConfig endpointConfig = ClientEndpointConfig.Builder.create() .configurator(new StandardWebSocketClientConfigurator(headers)) .preferredSubprotocols(protocols) .extensions(adaptExtensions(extensions)).build(); endpointConfig.getUserProperties().putAll(getUserProperties()); final Endpoint endpoint = new StandardWebSocketHandlerAdapter(webSocketHandler, session); Callable<WebSocketSession> connectTask = new Callable<WebSocketSession>() { @Override public WebSocketSession call() throws Exception { webSocketContainer.connectToServer(endpoint, endpointConfig, uri); return session; } }; if (this.taskExecutor != null) { return this.taskExecutor.submitListenable(connectTask); } else { ListenableFutureTask<WebSocketSession> task = new ListenableFutureTask<WebSocketSession>(connectTask); task.run(); return task; } }
@Override public ListenableFuture<WebSocketSession> doHandshakeInternal(WebSocketHandler wsHandler, HttpHeaders headers, final URI uri, List<String> protocols, List<WebSocketExtension> extensions, Map<String, Object> attributes) { final ClientUpgradeRequest request = new ClientUpgradeRequest(); request.setSubProtocols(protocols); for (WebSocketExtension e : extensions) { request.addExtensions(new WebSocketToJettyExtensionConfigAdapter(e)); } for (String header : headers.keySet()) { request.setHeader(header, headers.get(header)); } Principal user = getUser(); final JettyWebSocketSession wsSession = new JettyWebSocketSession(attributes, user); final JettyWebSocketHandlerAdapter listener = new JettyWebSocketHandlerAdapter(wsHandler, wsSession); Callable<WebSocketSession> connectTask = new Callable<WebSocketSession>() { @Override public WebSocketSession call() throws Exception { Future<Session> future = client.connect(listener, uri, request); future.get(); return wsSession; } }; if (this.taskExecutor != null) { return this.taskExecutor.submitListenable(connectTask); } else { ListenableFutureTask<WebSocketSession> task = new ListenableFutureTask<WebSocketSession>(connectTask); task.run(); return task; } }
@Override public ListenableFuture<WebSocketSession> doHandshake(WebSocketHandler handler, WebSocketHttpHeaders headers, URI uri) { this.webSocketHandler = handler; this.headers = headers; this.uri = uri; return new ListenableFutureTask<>(() -> null); }
public void onMessage(String name) { for (DeferredResult<Person> deferredResult : this.deferredResults) { deferredResult.setResult(new Person(name)); this.deferredResults.remove(deferredResult); } for (ListenableFutureTask<Person> futureTask : this.futureTasks) { futureTask.run(); this.futureTasks.remove(futureTask); } }
@Test public void shouldCreateExecutableFromEndpointMethodWithListenableFutureTaskReturnType() throws Exception { EndpointCallExecutable<ListenableFutureTask<String>, String> executable = factory .create(new SimpleEndpointMethod(SomeType.class.getMethod("future")), delegate); String result = "future result"; ListenableFutureTask<String> future = executable.execute(() -> result, null); assertEquals(result, future.get()); assertEquals(delegate.returnType(), executable.returnType()); verify(delegate).execute(any(), anyVararg()); }
@Override public ListenableFuture<?> submitListenable(Runnable task) { try { ListenableFutureTask<Object> future = new ListenableFutureTask<Object>(task, null); doExecute(this.concurrentExecutor, this.taskDecorator, future); return future; } catch (RejectedExecutionException ex) { throw new TaskRejectedException( "Executor [" + this.concurrentExecutor + "] did not accept task: " + task, ex); } }
@Override public <T> ListenableFuture<T> submitListenable(Callable<T> task) { try { ListenableFutureTask<T> future = new ListenableFutureTask<T>(task); doExecute(this.concurrentExecutor, this.taskDecorator, future); return future; } catch (RejectedExecutionException ex) { throw new TaskRejectedException( "Executor [" + this.concurrentExecutor + "] did not accept task: " + task, ex); } }
@Test public void shouldCancel() { final ListenableFuture<Void> original = new ListenableFutureTask<>(() -> {}, null); final CompletableFuture<Void> unit = preserveCancelability(original); original.addCallback(unit::complete, unit::completeExceptionally); unit.cancel(true); assertThat(original.isCancelled(), is(true)); }
@Override public ListenableFuture<?> submitListenable(Runnable task) { ListenableFutureTask<Object> future = new ListenableFutureTask<Object>(task, null); execute(future); return future; }
@Override public <T> ListenableFuture<T> submitListenable(Callable<T> task) { ListenableFutureTask<T> future = new ListenableFutureTask<T>(task); execute(future); return future; }
@Override public ListenableFuture<?> submitListenable(Runnable task) { ListenableFutureTask<Object> future = new ListenableFutureTask<Object>(task, null); execute(future, TIMEOUT_INDEFINITE); return future; }
@Override public <T> ListenableFuture<T> submitListenable(Callable<T> task) { ListenableFutureTask<T> future = new ListenableFutureTask<T>(task); execute(future, TIMEOUT_INDEFINITE); return future; }