org.wildfly.clustering.context.ContextualExecutorService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of wildfly-clustering-context Show documentation
Show all versions of wildfly-clustering-context Show documentation
This module contains mechanisms for performing execution using an extensible context.
/*
* JBoss, Home of Professional Open Source.
* Copyright 2019, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
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;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy