@Override public ScheduledFuture<?> scheduleWithFixedDelay( Runnable runnable, long l, long l1, TimeUnit timeUnit ) { return asScheduled( ( d ) -> d.scheduleWithFixedDelay( wrapRunnable( runnable ), l, l1, timeUnit ) ); }
@Override public <T> T invokeAny( Collection<? extends Callable<T>> collection ) throws InterruptedException, ExecutionException { return delegate.invokeAny( wrapAll( collection ) ); }
@Override public <V> ScheduledFuture<V> schedule( Callable<V> callable, long l, TimeUnit timeUnit ) { return asScheduled( (d) -> d.schedule( wrapCallable( callable ), l, timeUnit ) ); }
@Override public Future<?> submit( Runnable runnable ) { return delegate.submit( wrapRunnable( runnable ) ); }
private <T> Collection<Callable<T>> wrapAll( Collection<? extends Callable<T>> collection ) { ThreadContext ctx = ThreadContext.getContext( false ); return collection.parallelStream().map( ( callable ) -> { ThreadContext old = ThreadContext.setContext( ctx ); Logger logger = LoggerFactory.getLogger( getClass() ); logger.debug( "Using ThreadContext: {} (saving: {}) in {}", ctx, old, Thread.currentThread().getName() ); return timeCallable((Callable<T>) () -> { try { return callable.call(); } finally { logger.debug( "Restoring ThreadContext: {} in: {}", old, Thread.currentThread().getName() ); ThreadContext.setContext( old ); } }); } ).collect( Collectors.toList() ); }
@Override public <T> Future<T> submit( Callable<T> callable ) { return delegate.submit( wrapCallable( callable ) ); }
private Runnable wrapRunnable( Runnable runnable ) { ThreadContext ctx = ThreadContext.getContext( false ); return timeRunnable(()->{ ThreadContext old = ThreadContext.setContext( ctx ); Logger logger = LoggerFactory.getLogger( getClass() ); logger.debug( "Using ThreadContext: {} (saving: {}) in {}", ctx, old, Thread.currentThread().getName() ); try { runnable.run(); } finally { logger.debug( "Restoring ThreadContext: {} in: {}", old, Thread.currentThread().getName() ); ThreadContext.setContext( old ); } }); }
service = new ContextSensitiveExecutorService( service, metricRegistry, metricPrefix );
@Override public <T> Future<T> submit( Runnable runnable, T t ) { return delegate.submit( wrapRunnable( runnable ), t ); }
private <T> Callable<T> wrapCallable( Callable<T> callable ) { ThreadContext ctx = ThreadContext.getContext( false ); return timeCallable((Callable<T>) ()->{ ThreadContext old = ThreadContext.setContext( ctx ); Logger logger = LoggerFactory.getLogger( getClass() ); logger.debug( "Using ThreadContext: {} (saving: {}) in {}", ctx, old, Thread.currentThread().getName() ); try { return callable.call(); } finally { logger.debug( "Restoring ThreadContext: {} in: {}", old, Thread.currentThread().getName() ); ThreadContext.setContext( old ); } }); } }
@Override public ScheduledFuture<?> schedule( Runnable runnable, long l, TimeUnit timeUnit ) { return asScheduled( ( d ) -> d.schedule( wrapRunnable( runnable ), l, timeUnit ) ); }
@Override public <T> T invokeAny( Collection<? extends Callable<T>> collection, long l, TimeUnit timeUnit ) throws InterruptedException, ExecutionException, TimeoutException { return delegate.invokeAny( wrapAll( collection ), l, timeUnit ); }
@Override public void execute( Runnable runnable ) { delegate.execute( wrapRunnable( runnable ) ); }
@Override public ScheduledFuture<?> scheduleAtFixedRate( Runnable runnable, long l, long l1, TimeUnit timeUnit ) { return asScheduled( ( d ) -> d.scheduleAtFixedRate( wrapRunnable( runnable ), l, l1, timeUnit ) ); }
@Override public <T> List<Future<T>> invokeAll( Collection<? extends Callable<T>> collection ) throws InterruptedException { return delegate.invokeAll( wrapAll(collection) ); }
@Override public <T> List<Future<T>> invokeAll( Collection<? extends Callable<T>> collection, long l, TimeUnit timeUnit ) throws InterruptedException { return delegate.invokeAll( wrapAll( collection ), l, timeUnit ); }