org.wildfly.clustering.context.ContextualExecutorService Maven / Gradle / Ivy
/*
* Copyright The WildFly Authors
* SPDX-License-Identifier: Apache-2.0
*/
package org.wildfly.clustering.context;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* {@link ExecutorService} decorator that contextualizes tasks to be executed.
* @author Paul Ferraro
*/
public class ContextualExecutorService implements ExecutorService {
private final ExecutorService executor;
private final Contextualizer contextualizer;
public ContextualExecutorService(ExecutorService executor, Contextualizer contextualizer) {
this.executor = executor;
this.contextualizer = contextualizer;
}
@Override
public void execute(Runnable command) {
this.executor.execute(this.contextualizer.contextualize(command));
}
@Override
public void shutdown() {
this.executor.shutdown();
}
@Override
public List shutdownNow() {
return this.executor.shutdownNow();
}
@Override
public boolean isShutdown() {
return this.executor.isShutdown();
}
@Override
public boolean isTerminated() {
return this.executor.isTerminated();
}
@Override
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
return this.executor.awaitTermination(timeout, unit);
}
@Override
public Future submit(Callable task) {
return this.executor.submit(this.contextualizer.contextualize(task));
}
@Override
public Future submit(Runnable task, T result) {
return this.executor.submit(this.contextualizer.contextualize(task), result);
}
@Override
public Future> submit(Runnable task) {
return this.executor.submit(this.contextualizer.contextualize(task));
}
@Override
public List> invokeAll(Collection extends Callable> tasks) throws InterruptedException {
return this.executor.invokeAll(this.contextualize(tasks));
}
@Override
public List> invokeAll(Collection extends Callable> tasks, long timeout, TimeUnit unit) throws InterruptedException {
return this.executor.invokeAll(this.contextualize(tasks), timeout, unit);
}
@Override
public T invokeAny(Collection extends Callable> tasks) throws InterruptedException, ExecutionException {
return this.executor.invokeAny(this.contextualize(tasks));
}
@Override
public T invokeAny(Collection extends Callable> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
return this.executor.invokeAny(this.contextualize(tasks), timeout, unit);
}
private Collection> contextualize(Collection extends Callable> tasks) {
List> result = new ArrayList<>(tasks.size());
for (Callable task : tasks) {
result.add(this.contextualizer.contextualize(task));
}
return result;
}
}