All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.wildfly.clustering.context.ContextualExecutorService Maven / Gradle / Ivy

There is a newer version: 33.0.2.Final
Show newest version
/*
 * 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> tasks) throws InterruptedException {
        return this.executor.invokeAll(this.contextualize(tasks));
    }

    @Override
    public  List> invokeAll(Collection> tasks, long timeout, TimeUnit unit) throws InterruptedException {
        return this.executor.invokeAll(this.contextualize(tasks), timeout, unit);
    }

    @Override
    public  T invokeAny(Collection> tasks) throws InterruptedException, ExecutionException {
        return this.executor.invokeAny(this.contextualize(tasks));
    }

    @Override
    public  T invokeAny(Collection> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
        return this.executor.invokeAny(this.contextualize(tasks), timeout, unit);
    }

    private  Collection> contextualize(Collection> 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