org.springframework.core.task.TaskDecorator Java Examples

The following examples show how to use org.springframework.core.task.TaskDecorator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SpringAsynchConfig.java    From Spring-5.0-Cookbook with MIT License 6 votes vote down vote up
@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;
}
 
Example #2
Source File: SpringAsynchConfig.java    From Spring-5.0-Cookbook with MIT License 6 votes vote down vote up
@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;
}
 
Example #3
Source File: SpringAsynchConfig.java    From Spring-5.0-Cookbook with MIT License 6 votes vote down vote up
@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;
}
 
Example #4
Source File: SpringAsynchConfig.java    From Spring-5.0-Cookbook with MIT License 6 votes vote down vote up
@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;
}
 
Example #5
Source File: SpringAsynchConfig.java    From Spring-5.0-Cookbook with MIT License 6 votes vote down vote up
@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;
}
 
Example #6
Source File: SpringAsynchConfig.java    From Spring-5.0-Cookbook with MIT License 6 votes vote down vote up
@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;
}
 
Example #7
Source File: SpringAsynchConfig.java    From Spring-5.0-Cookbook with MIT License 6 votes vote down vote up
@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;
}
 
Example #8
Source File: SpringAsynchConfig.java    From Spring-5.0-Cookbook with MIT License 6 votes vote down vote up
@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;
}
 
Example #9
Source File: SpringAsynchConfig.java    From Spring-5.0-Cookbook with MIT License 6 votes vote down vote up
@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;
}
 
Example #10
Source File: SpringAsynchConfig.java    From Spring-5.0-Cookbook with MIT License 6 votes vote down vote up
@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;
}
 
Example #11
Source File: SpringAsynchConfig.java    From Spring-5.0-Cookbook with MIT License 6 votes vote down vote up
@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;
}
 
Example #12
Source File: SpringAsynchConfig.java    From Spring-5.0-Cookbook with MIT License 6 votes vote down vote up
@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;
}
 
Example #13
Source File: SpringAsynchConfig.java    From Spring-5.0-Cookbook with MIT License 6 votes vote down vote up
@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;
}
 
Example #14
Source File: BatchConfig.java    From Spring-5.0-Cookbook with MIT License 6 votes vote down vote up
@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;
}
 
Example #15
Source File: SpringAsynchConfig.java    From Spring-5.0-Cookbook with MIT License 6 votes vote down vote up
@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;
}
 
Example #16
Source File: ChainedTaskDecoratorTest.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example #17
Source File: ChainedTaskDecorator.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@Override
public Runnable decorate(Runnable runnable) {
    Runnable decoratedRunnable = runnable;
    for (TaskDecorator taskDecorator : taskDecorators) {
        decoratedRunnable = taskDecorator.decorate(decoratedRunnable);
    }
    return decoratedRunnable;
}
 
Example #18
Source File: CompositeTaskDecorator.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Override
public Runnable decorate(Runnable runnable) {
    Runnable decoratedRunnable = requireNonNull(runnable, "runnable is null");
    for (TaskDecorator decorator : decorators) {
        decoratedRunnable = decorator.decorate(decoratedRunnable);
    }
    return decoratedRunnable;
}
 
Example #19
Source File: LazyTraceThreadPoolTaskExecutor.java    From elasticactors with Apache License 2.0 4 votes vote down vote up
@Override
public void setTaskDecorator(TaskDecorator taskDecorator) {
    this.delegate.setTaskDecorator(taskDecorator);
}
 
Example #20
Source File: TracedThreadPoolTaskExecutor.java    From java-spring-cloud with Apache License 2.0 4 votes vote down vote up
@Override
public void setTaskDecorator(TaskDecorator taskDecorator) {
  this.delegate.setTaskDecorator(taskDecorator);
}
 
Example #21
Source File: LazyTraceThreadPoolTaskExecutor.java    From spring-cloud-sleuth with Apache License 2.0 4 votes vote down vote up
@Override
public void setTaskDecorator(TaskDecorator taskDecorator) {
	this.delegate.setTaskDecorator(taskDecorator);
}
 
Example #22
Source File: ChainedTaskDecorator.java    From pinpoint with Apache License 2.0 4 votes vote down vote up
public ChainedTaskDecorator(List<TaskDecorator> taskDecorators) {
    this.taskDecorators = Objects.requireNonNull(taskDecorators, "taskDecorators");
}
 
Example #23
Source File: SofaThreadPoolTaskExecutor.java    From sofa-common-tools with Apache License 2.0 4 votes vote down vote up
@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;
}
 
Example #24
Source File: TaskExecutorAdapter.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * 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;
}
 
Example #25
Source File: ThreadPoolTaskExecutor.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * 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;
}
 
Example #26
Source File: ConcurrentTaskExecutor.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * 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);
}
 
Example #27
Source File: CompositeTaskDecorator.java    From cloudbreak with Apache License 2.0 2 votes vote down vote up
/**
 * Create a CompositeTaskDecorator from the list of task decorators.
 *
 * @param decorators a list of task decorators to apply, from innermost to outermost.
 */
public CompositeTaskDecorator(List<TaskDecorator> decorators) {
    requireNonNull(decorators, "decorators is null");
    this.decorators = ImmutableList.copyOf(decorators);
}
 
Example #28
Source File: TaskExecutorAdapter.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * 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;
}
 
Example #29
Source File: TaskExecutorAdapter.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * 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);
}
 
Example #30
Source File: WorkManagerTaskExecutor.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * 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;
}