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

com.transferwise.common.baseutils.concurrency.ThreadNamingExecutorServiceWrapper Maven / Gradle / Ivy

package com.transferwise.common.baseutils.concurrency;

import lombok.extern.slf4j.Slf4j;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.*;
import java.util.stream.Collectors;

@Slf4j
public class ThreadNamingExecutorServiceWrapper implements ExecutorService {
	private ExecutorService delegate;
	private String threadName;

	public ThreadNamingExecutorServiceWrapper(String threadName, ExecutorService delegate){
		this.threadName = threadName;
		this.delegate = delegate;
	}

	@Override
	public void shutdown() {
		delegate.shutdown();
	}

	@Override
	public List shutdownNow() {
		return delegate.shutdownNow();
	}

	@Override
	public boolean isShutdown() {
		return delegate.isShutdown();
	}

	@Override
	public boolean isTerminated() {
		return delegate.isTerminated();
	}

	@Override
	public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
		return delegate.awaitTermination(timeout, unit);
	}

	@Override
	public  Future submit(Callable task) {
		return delegate.submit(wrap(task));
	}

	@Override
	public  Future submit(Runnable task, T result) {
		return delegate.submit(wrap(task), result);
	}

	@Override
	public Future submit(Runnable task) {
		return delegate.submit(wrap(task));
	}

	@Override
	public  List> invokeAll(Collection> tasks) throws InterruptedException {
		return delegate.invokeAll(wrap(tasks));
	}

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

	@Override
	public  T invokeAny(Collection> tasks) throws InterruptedException, ExecutionException {
		return delegate.invokeAny(wrap(tasks));
	}

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

	@Override
	public void execute(Runnable command) {
		delegate.execute(command);
	}

	private  Collection> wrap(Collection> tasks){
		return tasks.stream().map(this::wrap).collect(Collectors.toList());
	}

	protected  Callable wrap(Callable delegate){
		return () -> {
			String currentName = Thread.currentThread().getName();
			try{
				Thread.currentThread().setName(currentName + "-" + threadName);
				return delegate.call();
			}
			finally{
				Thread.currentThread().setName(currentName);
			}
		};
	}

	protected Runnable wrap(Runnable delegate){
		return ()-> {
			String currentName = Thread.currentThread().getName();
			try{
				Thread.currentThread().setName(currentName + "-" + threadName);
				delegate.run();
			}
			catch (Throwable t){
				log.error(t.getMessage(), t);
			}
			finally{
				Thread.currentThread().setName(currentName);
			}
		};
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy