Java Code Examples for org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor#afterPropertiesSet()

The following examples show how to use org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor#afterPropertiesSet() . 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: SpringApp.java    From rqueue with Apache License 2.0 6 votes vote down vote up
@Bean
public SimpleRqueueListenerContainerFactory simpleRqueueListenerContainerFactory(
    DeleteMessageListener deleteMessageListener) {
  SimpleRqueueListenerContainerFactory factory = new SimpleRqueueListenerContainerFactory();
  factory.setMaxNumWorkers(maxWorkers);
  factory.setManualDeletionMessageProcessor(deleteMessageListener);
  if (priorityMode != null) {
    factory.setPriorityMode(priorityMode);
  }
  if (provideExecutor) {
    ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
    threadPoolTaskExecutor.setCorePoolSize(maxWorkers);
    threadPoolTaskExecutor.afterPropertiesSet();
    factory.setTaskExecutor(threadPoolTaskExecutor);
  }
  return factory;
}
 
Example 2
Source File: TaskExecutorFactoryBean.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public void afterPropertiesSet() {
	ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
	determinePoolSizeRange(executor);
	if (this.queueCapacity != null) {
		executor.setQueueCapacity(this.queueCapacity);
	}
	if (this.keepAliveSeconds != null) {
		executor.setKeepAliveSeconds(this.keepAliveSeconds);
	}
	if (this.rejectedExecutionHandler != null) {
		executor.setRejectedExecutionHandler(this.rejectedExecutionHandler);
	}
	if (this.beanName != null) {
		executor.setThreadNamePrefix(this.beanName + "-");
	}
	executor.afterPropertiesSet();
	this.target = executor;
}
 
Example 3
Source File: AsyncAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void threadNamePrefix() {
	BeanDefinition processorDefinition = new RootBeanDefinition(AsyncAnnotationBeanPostProcessor.class);
	ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
	executor.setThreadNamePrefix("testExecutor");
	executor.afterPropertiesSet();
	processorDefinition.getPropertyValues().add("executor", executor);
	ConfigurableApplicationContext context = initContext(processorDefinition);

	ITestBean testBean = context.getBean("target", ITestBean.class);
	testBean.test();
	testBean.await(3000);
	Thread asyncThread = testBean.getThread();
	assertTrue(asyncThread.getName().startsWith("testExecutor"));
	context.close();
}
 
Example 4
Source File: TaskExecutorFactoryBean.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public void afterPropertiesSet() {
	ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
	determinePoolSizeRange(executor);
	if (this.queueCapacity != null) {
		executor.setQueueCapacity(this.queueCapacity);
	}
	if (this.keepAliveSeconds != null) {
		executor.setKeepAliveSeconds(this.keepAliveSeconds);
	}
	if (this.rejectedExecutionHandler != null) {
		executor.setRejectedExecutionHandler(this.rejectedExecutionHandler);
	}
	if (this.beanName != null) {
		executor.setThreadNamePrefix(this.beanName + "-");
	}
	executor.afterPropertiesSet();
	this.target = executor;
}
 
Example 5
Source File: AsyncAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void threadNamePrefix() {
	BeanDefinition processorDefinition = new RootBeanDefinition(AsyncAnnotationBeanPostProcessor.class);
	ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
	executor.setThreadNamePrefix("testExecutor");
	executor.afterPropertiesSet();
	processorDefinition.getPropertyValues().add("executor", executor);
	ConfigurableApplicationContext context = initContext(processorDefinition);

	ITestBean testBean = context.getBean("target", ITestBean.class);
	testBean.test();
	testBean.await(3000);
	Thread asyncThread = testBean.getThread();
	assertTrue(asyncThread.getName().startsWith("testExecutor"));
	context.close();
}
 
Example 6
Source File: AsyncExecutorConfiguration.java    From booties with Apache License 2.0 6 votes vote down vote up
@ConditionalOnProperty(prefix = "async.executor", name = "enabled", matchIfMissing = true)
@Bean(name = DEFAULT_TASK_EXECUTOR_BEAN_NAME, destroyMethod = "shutdown")
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public ThreadPoolTaskExecutor threadPoolTaskExecutor(AsyncExecutorProperties properties) {
    log.info("Creating ThreadPoolTaskExecutor 'taskExecutor' with core-size={} max-size={} queue-capacity={}",
            properties.getCorePoolSize(), properties.getMaxPoolSize(), properties.getQueueCapacity());

    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(properties.getCorePoolSize());
    executor.setMaxPoolSize(properties.getMaxPoolSize());

    executor.setQueueCapacity(properties.getQueueCapacity());
    executor.setThreadNamePrefix(properties.getThreadNamePrefix());
    executor.setAwaitTerminationSeconds(properties.getAwaitTerminationSeconds());

    executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy());

    executor.afterPropertiesSet();
    return executor;
}
 
Example 7
Source File: SimpleMessageListenerContainer.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
/**
 * Create a default TaskExecutor. Called if no explicit TaskExecutor has been
 * specified.
 * <p>
 * The default implementation builds a
 * {@link org.springframework.core.task.SimpleAsyncTaskExecutor} with the specified
 * bean name (or the class name, if no bean name specified) as thread name prefix.
 * @return a {@link org.springframework.core.task.SimpleAsyncTaskExecutor} configured
 * with the thread name prefix
 * @see org.springframework.core.task.SimpleAsyncTaskExecutor#SimpleAsyncTaskExecutor(String)
 */
protected AsyncTaskExecutor createDefaultTaskExecutor() {
	String beanName = getBeanName();
	ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
	threadPoolTaskExecutor.setThreadNamePrefix(
			beanName != null ? beanName + "-" : DEFAULT_THREAD_NAME_PREFIX);
	int spinningThreads = this.getRegisteredQueues().size();

	if (spinningThreads > 0) {
		threadPoolTaskExecutor
				.setCorePoolSize(spinningThreads * DEFAULT_WORKER_THREADS);

		int maxNumberOfMessagePerBatch = getMaxNumberOfMessages() != null
				? getMaxNumberOfMessages() : DEFAULT_MAX_NUMBER_OF_MESSAGES;
		threadPoolTaskExecutor
				.setMaxPoolSize(spinningThreads * (maxNumberOfMessagePerBatch + 1));
	}

	// No use of a thread pool executor queue to avoid retaining message to long in
	// memory
	threadPoolTaskExecutor.setQueueCapacity(0);
	threadPoolTaskExecutor.afterPropertiesSet();

	return threadPoolTaskExecutor;

}
 
Example 8
Source File: ThreadUtils.java    From rqueue with Apache License 2.0 5 votes vote down vote up
public static ThreadPoolTaskExecutor createTaskExecutor(
    String beanName, String threadPrefix, int corePoolSize, int maxPoolSize) {
  ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
  threadPoolTaskExecutor.setThreadNamePrefix(threadPrefix);
  threadPoolTaskExecutor.setBeanName(beanName);
  if (corePoolSize > 0) {
    threadPoolTaskExecutor.setCorePoolSize(corePoolSize);
    threadPoolTaskExecutor.setMaxPoolSize(Math.max(corePoolSize, maxPoolSize));
    threadPoolTaskExecutor.setQueueCapacity(0);
    threadPoolTaskExecutor.afterPropertiesSet();
  }
  return threadPoolTaskExecutor;
}
 
Example 9
Source File: AsyncAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void threadNamePrefix() {
	BeanDefinition processorDefinition = new RootBeanDefinition(AsyncAnnotationBeanPostProcessor.class);
	ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
	executor.setThreadNamePrefix("testExecutor");
	executor.afterPropertiesSet();
	processorDefinition.getPropertyValues().add("executor", executor);
	ConfigurableApplicationContext context = initContext(processorDefinition);
	ITestBean testBean = context.getBean("target", ITestBean.class);
	testBean.test();
	testBean.await(3000);
	Thread asyncThread = testBean.getThread();
	assertTrue(asyncThread.getName().startsWith("testExecutor"));
	context.close();
}
 
Example 10
Source File: ThreadPoolTaskExecutorUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenUsingDefaults_thenSingleThread() {
    ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
    taskExecutor.afterPropertiesSet();

    CountDownLatch countDownLatch = new CountDownLatch(10);
    this.startThreads(taskExecutor, countDownLatch, 10);

    while (countDownLatch.getCount() > 0) {
        Assert.assertEquals(1, taskExecutor.getPoolSize());
    }
}
 
Example 11
Source File: ThreadPoolTaskExecutorUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenCorePoolSizeFive_thenFiveThreads() {
    ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
    taskExecutor.setCorePoolSize(5);
    taskExecutor.afterPropertiesSet();

    CountDownLatch countDownLatch = new CountDownLatch(10);
    this.startThreads(taskExecutor, countDownLatch, 10);

    while (countDownLatch.getCount() > 0) {
        Assert.assertEquals(5, taskExecutor.getPoolSize());
    }
}
 
Example 12
Source File: ThreadPoolTaskExecutorUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenCorePoolSizeFiveAndMaxPoolSizeTen_thenFiveThreads() {
    ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
    taskExecutor.setCorePoolSize(5);
    taskExecutor.setMaxPoolSize(10);
    taskExecutor.afterPropertiesSet();

    CountDownLatch countDownLatch = new CountDownLatch(10);
    this.startThreads(taskExecutor, countDownLatch, 10);

    while (countDownLatch.getCount() > 0) {
        Assert.assertEquals(5, taskExecutor.getPoolSize());
    }
}
 
Example 13
Source File: ThreadPoolTaskExecutorUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenCorePoolSizeFiveAndMaxPoolSizeTenAndQueueCapacityZero_thenTenThreads() {
    ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
    taskExecutor.setCorePoolSize(5);
    taskExecutor.setMaxPoolSize(10);
    taskExecutor.setQueueCapacity(0);
    taskExecutor.afterPropertiesSet();

    CountDownLatch countDownLatch = new CountDownLatch(10);
    this.startThreads(taskExecutor, countDownLatch, 10);

    while (countDownLatch.getCount() > 0) {
        Assert.assertEquals(10, taskExecutor.getPoolSize());
    }
}
 
Example 14
Source File: ThreadPoolTaskExecutorUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenCorePoolSizeFiveAndMaxPoolSizeTenAndQueueCapacityTen_thenTenThreads() {
    ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
    taskExecutor.setCorePoolSize(5);
    taskExecutor.setMaxPoolSize(10);
    taskExecutor.setQueueCapacity(10);
    taskExecutor.afterPropertiesSet();

    CountDownLatch countDownLatch = new CountDownLatch(20);
    this.startThreads(taskExecutor, countDownLatch, 20);

    while (countDownLatch.getCount() > 0) {
        Assert.assertEquals(10, taskExecutor.getPoolSize());
    }
}
 
Example 15
Source File: SmoothieConfig.java    From spring4ws-demos with Apache License 2.0 5 votes vote down vote up
@Bean
public CpuDataService cpuDataService() {
	ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
	executor.setMaxPoolSize(10);
	executor.setThreadNamePrefix("SmoothieHandler-");
	executor.afterPropertiesSet();

	return new CpuDataService(executor);
}
 
Example 16
Source File: MdcThreadPoolTaskExecutorTest.java    From spring-boot-starter-batch-web with Apache License 2.0 4 votes vote down vote up
@Test
public void run() throws Exception {
	// Given
	ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
	taskExecutor.afterPropertiesSet();

	final ThreadPoolTaskExecutor innerTaskExecutor = new MdcThreadPoolTaskExecutor();
	innerTaskExecutor.afterPropertiesSet();

	// Simple task which returns always the key from the MDC
	final FutureTask<String> innerTask = new FutureTask<String>(new Callable<String>() {

		@Override
		public String call() throws Exception {
			return MDC.get("key");
		}
	});

	taskExecutor.execute(new Runnable() {

		@Override
		public void run() {
			MDC.put("key", "1");
			innerTaskExecutor.execute(innerTask);
		}
	});
	// Wait for Thread and verify that it contains the right value of key
	assertThat(innerTask.get(), is(equalTo("1")));

	// Wait 1sec. for outer thread
	Thread.sleep(1000);

	// Simple task which returns always the key from the MDC
	final FutureTask<String> innerTask2 = new FutureTask<String>(new Callable<String>() {

		@Override
		public String call() throws Exception {
			return MDC.get("key");
		}
	});

	taskExecutor.execute(new Runnable() {

		@Override
		public void run() {
			MDC.put("key", "2");
			innerTaskExecutor.execute(innerTask2);
		}
	});

	// Wait for Thread and verify that it contains the right value of key
	assertThat(innerTask2.get(), is(equalTo("2")));

}