org.springframework.util.concurrent.ListenableFutureTask Java Examples

The following examples show how to use org.springframework.util.concurrent.ListenableFutureTask. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FairThreadPoolTaskExecutor.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
@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);
   }
}
 
Example #2
Source File: PrefetchingDatabaseProvider.java    From embedded-database-spring-test with Apache License 2.0 6 votes vote down vote up
private ListenableFutureTask<DataSource> prepareDatabase(PipelineKey key, int priority) {
    PrefetchingTask task = new PrefetchingTask(key.provider, key.preparer, priority);
    DatabasePipeline pipeline = pipelines.get(key);

    task.addCallback(new ListenableFutureCallback<DataSource>() {
        @Override
        public void onSuccess(DataSource result) {
            pipeline.tasks.remove(task);
            pipeline.results.offer(PreparedResult.success(result));
        }

        @Override
        public void onFailure(Throwable error) {
            pipeline.tasks.remove(task);
            if (!(error instanceof CancellationException)) {
                pipeline.results.offer(PreparedResult.failure(error));
            }
        }
    });

    pipeline.tasks.add(task);
    taskExecutor.execute(task);
    return task;
}
 
Example #3
Source File: ThreadPoolTaskScheduler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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);
	}
}
 
Example #4
Source File: TaskExecutorAdapter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public <T> ListenableFuture<T> submitListenable(Callable<T> task) {
	try {
		ListenableFutureTask<T> future = new ListenableFutureTask<>(task);
		doExecute(this.concurrentExecutor, this.taskDecorator, future);
		return future;
	}
	catch (RejectedExecutionException ex) {
		throw new TaskRejectedException(
				"Executor [" + this.concurrentExecutor + "] did not accept task: " + task, ex);
	}
}
 
Example #5
Source File: ThreadPoolTaskExecutor.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public ListenableFuture<?> submitListenable(Runnable task) {
	ExecutorService executor = getThreadPoolExecutor();
	try {
		ListenableFutureTask<Object> future = new ListenableFutureTask<>(task, null);
		executor.execute(future);
		return future;
	}
	catch (RejectedExecutionException ex) {
		throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
	}
}
 
Example #6
Source File: ThreadPoolTaskExecutor.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public <T> ListenableFuture<T> submitListenable(Callable<T> task) {
	ExecutorService executor = getThreadPoolExecutor();
	try {
		ListenableFutureTask<T> future = new ListenableFutureTask<>(task);
		executor.execute(future);
		return future;
	}
	catch (RejectedExecutionException ex) {
		throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
	}
}
 
Example #7
Source File: ThreadPoolTaskScheduler.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public ListenableFuture<?> submitListenable(Runnable task) {
	ExecutorService executor = getScheduledExecutor();
	try {
		ListenableFutureTask<Object> listenableFuture = new ListenableFutureTask<>(task, null);
		executeAndTrack(executor, listenableFuture);
		return listenableFuture;
	}
	catch (RejectedExecutionException ex) {
		throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
	}
}
 
Example #8
Source File: ThreadPoolTaskScheduler.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public <T> ListenableFuture<T> submitListenable(Callable<T> task) {
	ExecutorService executor = getScheduledExecutor();
	try {
		ListenableFutureTask<T> listenableFuture = new ListenableFutureTask<>(task);
		executeAndTrack(executor, listenableFuture);
		return listenableFuture;
	}
	catch (RejectedExecutionException ex) {
		throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex);
	}
}
 
Example #9
Source File: TaskExecutorAdapter.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public ListenableFuture<?> submitListenable(Runnable task) {
	try {
		ListenableFutureTask<Object> future = new ListenableFutureTask<>(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);
	}
}
 
Example #10
Source File: TaskExecutorAdapter.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public <T> ListenableFuture<T> submitListenable(Callable<T> task) {
	try {
		ListenableFutureTask<T> future = new ListenableFutureTask<>(task);
		doExecute(this.concurrentExecutor, this.taskDecorator, future);
		return future;
	}
	catch (RejectedExecutionException ex) {
		throw new TaskRejectedException(
				"Executor [" + this.concurrentExecutor + "] did not accept task: " + task, ex);
	}
}
 
Example #11
Source File: JettyWebSocketClient.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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));
	}

	headers.forEach(request::setHeader);

	Principal user = getUser();
	final JettyWebSocketSession wsSession = new JettyWebSocketSession(attributes, user);
	final JettyWebSocketHandlerAdapter listener = new JettyWebSocketHandlerAdapter(wsHandler, wsSession);

	Callable<WebSocketSession> connectTask = () -> {
		Future<Session> future = this.client.connect(listener, uri, request);
		future.get();
		return wsSession;
	};

	if (this.taskExecutor != null) {
		return this.taskExecutor.submitListenable(connectTask);
	}
	else {
		ListenableFutureTask<WebSocketSession> task = new ListenableFutureTask<>(connectTask);
		task.run();
		return task;
	}
}
 
Example #12
Source File: WebSocketConnectionManagerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public ListenableFuture<WebSocketSession> doHandshake(WebSocketHandler handler,
		WebSocketHttpHeaders headers, URI uri) {

	this.webSocketHandler = handler;
	this.headers = headers;
	this.uri = uri;
	return new ListenableFutureTask<>(() -> null);
}
 
Example #13
Source File: AsyncTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
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);
	}
}
 
Example #14
Source File: ThreadPoolTaskExecutor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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);
	}
}
 
Example #15
Source File: ThreadPoolTaskExecutor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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);
	}
}
 
Example #16
Source File: StandardWebSocketClient.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 = () -> {
		this.webSocketContainer.connectToServer(endpoint, endpointConfig, uri);
		return session;
	};

	if (this.taskExecutor != null) {
		return this.taskExecutor.submitListenable(connectTask);
	}
	else {
		ListenableFutureTask<WebSocketSession> task = new ListenableFutureTask<>(connectTask);
		task.run();
		return task;
	}
}
 
Example #17
Source File: ThreadPoolTaskScheduler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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);
	}
}
 
Example #18
Source File: TaskExecutorAdapter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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);
	}
}
 
Example #19
Source File: TaskExecutorAdapter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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);
	}
}
 
Example #20
Source File: SimpAnnotationMethodMessageHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@MessageMapping("failure")
public ListenableFutureTask<String> handleListenableFutureException() {
	this.future = new ListenableFutureTask<String>(() -> {
		throw new IllegalStateException();
	});
	return this.future;
}
 
Example #21
Source File: StompBrokerRelayMessageHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #22
Source File: StompBrokerRelayMessageHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #23
Source File: ThreadPoolTaskExecutor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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);
	}
}
 
Example #24
Source File: ThreadPoolTaskExecutor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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);
	}
}
 
Example #25
Source File: ThreadPoolTaskScheduler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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);
	}
}
 
Example #26
Source File: ThreadPoolTaskScheduler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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);
	}
}
 
Example #27
Source File: TaskExecutorAdapter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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);
	}
}
 
Example #28
Source File: TaskExecutorAdapter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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);
	}
}
 
Example #29
Source File: StandardWebSocketClient.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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;
	}
}
 
Example #30
Source File: JettyWebSocketClient.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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;
	}
}