@Bean("mvcTaskexecutor") @Override public Executor getAsyncExecutor() { ConcurrentTaskExecutor executor = new ConcurrentTaskExecutor( Executors.newFixedThreadPool(100)); executor.setTaskDecorator(new TaskDecorator() { @Override public Runnable decorate (Runnable runnable) { return () -> { long t = System.currentTimeMillis(); runnable.run(); System.out.printf("Thread %s has a processing time: %s%n", Thread.currentThread().getName(), (System.currentTimeMillis() - t)); }; } }); return executor; }
@Bean("mvcTaskexecutor") @Override public Executor getAsyncExecutor() { ConcurrentTaskExecutor executor = new ConcurrentTaskExecutor( Executors.newFixedThreadPool(100)); executor.setTaskDecorator(new TaskDecorator() { @Override public Runnable decorate (Runnable runnable) { return () -> { long t = System.currentTimeMillis(); runnable.run(); logger.info("creating ConcurrentTaskExecutor thread pool...."); System.out.printf("Thread %s has a processing time: %s%n", Thread.currentThread().getName(), (System.currentTimeMillis() - t)); }; } }); return executor; }
@Bean("mvcTaskexecutor") public TaskExecutor getAsyncExecutor() { ConcurrentTaskExecutor executor = new ConcurrentTaskExecutor( Executors.newFixedThreadPool(100)); executor.setTaskDecorator(new TaskDecorator() { @Override public Runnable decorate (Runnable runnable) { return () -> { long t = System.currentTimeMillis(); runnable.run(); System.out.printf("Thread %s has a processing time: %s%n", Thread.currentThread().getName(), (System.currentTimeMillis() - t)); }; } }); return executor; }
@Test public void chainedDecoratorsShouldBeCalled() throws InterruptedException { // Given final int testCount = 100; final CountDownLatch completeLatch = new CountDownLatch(testCount); final CountingTaskDecorator decorator1 = new CountingTaskDecorator(); final CountingTaskDecorator decorator2 = new CountingTaskDecorator(); final CountingTaskDecorator decorator3 = new CountingTaskDecorator(); final List<TaskDecorator> decorators = Arrays.asList(decorator1, decorator2, decorator3); final ChainedTaskDecorator chainedDecorator = new ChainedTaskDecorator(decorators); executor.setTaskDecorator(chainedDecorator); // When for (int i = 0; i < testCount; i++) { executor.execute(new TestWorker(completeLatch)); } completeLatch.await(5L, TimeUnit.SECONDS); // Then Assert.assertEquals(testCount, decorator1.getCount()); Assert.assertEquals(testCount, decorator2.getCount()); Assert.assertEquals(testCount, decorator3.getCount()); }
@Override public Runnable decorate(Runnable runnable) { Runnable decoratedRunnable = runnable; for (TaskDecorator taskDecorator : taskDecorators) { decoratedRunnable = taskDecorator.decorate(decoratedRunnable); } return decoratedRunnable; }
@Override public void setTaskDecorator(TaskDecorator taskDecorator) { this.delegate.setTaskDecorator(taskDecorator); }
public ChainedTaskDecorator(List<TaskDecorator> taskDecorators) { this.taskDecorators = Objects.requireNonNull(taskDecorators, "taskDecorators must not be null"); }
/** * Specify a custom {@link TaskDecorator} to be applied to any {@link Runnable} * about to be executed. * <p>Note that such a decorator is not necessarily being applied to the * user-supplied {@code Runnable}/{@code Callable} but rather to the actual * execution callback (which may be a wrapper around the user-supplied task). * <p>The primary use case is to set some execution context around the task's * invocation, or to provide some monitoring/statistics for task execution. * @since 4.3 */ public void setTaskDecorator(TaskDecorator taskDecorator) { this.taskDecorator = taskDecorator; }
/** * Specify a custom {@link TaskDecorator} to be applied to any {@link Runnable} * about to be executed. * <p>Note that such a decorator is not necessarily being applied to the * user-supplied {@code Runnable}/{@code Callable} but rather to the actual * execution callback (which may be a wrapper around the user-supplied task). * <p>The primary use case is to set some execution context around the task's * invocation, or to provide some monitoring/statistics for task execution. * @since 4.3 */ public final void setTaskDecorator(TaskDecorator taskDecorator) { this.adaptedExecutor.setTaskDecorator(taskDecorator); }
/** * Specify a custom {@link TaskDecorator} to be applied to any {@link Runnable} * about to be executed. * <p>Note that such a decorator is not necessarily being applied to the * user-supplied {@code Runnable}/{@code Callable} but rather to the actual * execution callback (which may be a wrapper around the user-supplied task). * <p>The primary use case is to set some execution context around the task's * invocation, or to provide some monitoring/statistics for task execution. * @since 4.3 */ public final void setTaskDecorator(TaskDecorator taskDecorator) { this.taskDecorator = taskDecorator; }
/** * Actually execute the given {@code Runnable} (which may be a user-supplied task * or a wrapper around a user-supplied task) with the given executor. * @param concurrentExecutor the underlying JDK concurrent executor to delegate to * @param taskDecorator the specified decorator to be applied, if any * @param runnable the runnable to execute * @throws RejectedExecutionException if the given runnable cannot be accepted * @since 4.3 */ protected void doExecute(Executor concurrentExecutor, TaskDecorator taskDecorator, Runnable runnable) throws RejectedExecutionException{ concurrentExecutor.execute(taskDecorator != null ? taskDecorator.decorate(runnable) : runnable); }