下面列出了怎么用org.springframework.core.task.TaskDecorator的API类实例代码及写法,或者点击链接到github查看源代码。
@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")
@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")
@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")
@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")
@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")
@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")
@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")
@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")
@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")
@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")
@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")
@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;
}
@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;
}
@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 Runnable decorate(Runnable runnable) {
Runnable decoratedRunnable = requireNonNull(runnable, "runnable is null");
for (TaskDecorator decorator : decorators) {
decoratedRunnable = decorator.decorate(decoratedRunnable);
}
return decoratedRunnable;
}
@Override
protected ExecutorService initializeExecutor(ThreadFactory threadFactory,
RejectedExecutionHandler rejectedExecutionHandler) {
Integer queueCapacity = ClassUtil.getField("queueCapacity", this);
final TaskDecorator taskDecorator = ClassUtil.getField("taskDecorator", this);
BlockingQueue<Runnable> queue = createQueue(queueCapacity);
SofaThreadPoolExecutor executor;
// When used as Spring bean, setter method is called before init method
if (threadPoolName == null) {
threadPoolName = createName();
}
if (taskDecorator != null) {
executor = new SofaThreadPoolExecutor(getCorePoolSize(), getMaxPoolSize(),
getKeepAliveSeconds(), TimeUnit.SECONDS, queue, threadFactory,
rejectedExecutionHandler, threadPoolName, DEFAULT_TASK_TIMEOUT, DEFAULT_PERIOD,
DEFAULT_TIME_UNIT) {
@Override
public void execute(Runnable command) {
super.execute(taskDecorator.decorate(command));
}
};
} else {
executor = new SofaThreadPoolExecutor(getCorePoolSize(), getMaxPoolSize(),
getKeepAliveSeconds(), TimeUnit.SECONDS, queue, threadFactory,
rejectedExecutionHandler, threadPoolName, DEFAULT_TASK_TIMEOUT, DEFAULT_PERIOD,
DEFAULT_TIME_UNIT);
}
Boolean allowCoreThreadTimeOut = ClassUtil.getField("allowCoreThreadTimeOut", this);
if (allowCoreThreadTimeOut) {
executor.allowCoreThreadTimeOut(true);
}
ClassUtil.setField("threadPoolExecutor", this, executor);
this.sofaThreadPoolExecutor = executor;
return executor;
}
@Override
public void setTaskDecorator(TaskDecorator taskDecorator) {
this.delegate.setTaskDecorator(taskDecorator);
}
@Override
public void setTaskDecorator(TaskDecorator taskDecorator) {
this.delegate.setTaskDecorator(taskDecorator);
}
public ChainedTaskDecorator(List<TaskDecorator> taskDecorators) {
this.taskDecorators = Objects.requireNonNull(taskDecorators, "taskDecorators");
}
@Override
public void setTaskDecorator(TaskDecorator taskDecorator) {
this.delegate.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 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, @Nullable TaskDecorator taskDecorator, Runnable runnable)
throws RejectedExecutionException{
concurrentExecutor.execute(taskDecorator != null ? taskDecorator.decorate(runnable) : runnable);
}
/**
* 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 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 void setTaskDecorator(TaskDecorator taskDecorator) {
this.taskDecorator = taskDecorator;
}