Line data Source code
1 : // Copyright (C) 2018 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.logging; 16 : 17 : import static java.util.stream.Collectors.toList; 18 : 19 : import java.util.Collection; 20 : import java.util.List; 21 : import java.util.concurrent.Callable; 22 : import java.util.concurrent.ExecutionException; 23 : import java.util.concurrent.ExecutorService; 24 : import java.util.concurrent.Future; 25 : import java.util.concurrent.TimeUnit; 26 : import java.util.concurrent.TimeoutException; 27 : 28 : /** 29 : * An {@link ExecutorService} that copies the {@link LoggingContext} on executing a {@link Runnable} 30 : * to the executing thread. 31 : */ 32 : public class LoggingContextAwareExecutorService implements ExecutorService { 33 : private final ExecutorService executorService; 34 : 35 144 : public LoggingContextAwareExecutorService(ExecutorService executorService) { 36 144 : this.executorService = executorService; 37 144 : } 38 : 39 : @Override 40 : public void execute(Runnable command) { 41 25 : executorService.execute(LoggingContext.copy(command)); 42 25 : } 43 : 44 : @Override 45 : public void shutdown() { 46 7 : executorService.shutdown(); 47 7 : } 48 : 49 : @Override 50 : public List<Runnable> shutdownNow() { 51 27 : return executorService.shutdownNow(); 52 : } 53 : 54 : @Override 55 : public boolean isShutdown() { 56 0 : return executorService.isShutdown(); 57 : } 58 : 59 : @Override 60 : public boolean isTerminated() { 61 0 : return executorService.isTerminated(); 62 : } 63 : 64 : @Override 65 : public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { 66 21 : return executorService.awaitTermination(timeout, unit); 67 : } 68 : 69 : @Override 70 : public <T> Future<T> submit(Callable<T> task) { 71 87 : return executorService.submit(LoggingContext.copy(task)); 72 : } 73 : 74 : @Override 75 : public <T> Future<T> submit(Runnable task, T result) { 76 0 : return executorService.submit(LoggingContext.copy(task), result); 77 : } 78 : 79 : @Override 80 : public Future<?> submit(Runnable task) { 81 1 : return executorService.submit(LoggingContext.copy(task)); 82 : } 83 : 84 : @Override 85 : public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) 86 : throws InterruptedException { 87 0 : return executorService.invokeAll(tasks.stream().map(LoggingContext::copy).collect(toList())); 88 : } 89 : 90 : @Override 91 : public <T> List<Future<T>> invokeAll( 92 : Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) 93 : throws InterruptedException { 94 0 : return executorService.invokeAll( 95 0 : tasks.stream().map(LoggingContext::copy).collect(toList()), timeout, unit); 96 : } 97 : 98 : @Override 99 : public <T> T invokeAny(Collection<? extends Callable<T>> tasks) 100 : throws InterruptedException, ExecutionException { 101 0 : return executorService.invokeAny(tasks.stream().map(LoggingContext::copy).collect(toList())); 102 : } 103 : 104 : @Override 105 : public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) 106 : throws InterruptedException, ExecutionException, TimeoutException { 107 0 : return executorService.invokeAny( 108 0 : tasks.stream().map(LoggingContext::copy).collect(toList()), timeout, unit); 109 : } 110 : }