org.springframework.scheduling.SchedulingTaskExecutor Java Examples

The following examples show how to use org.springframework.scheduling.SchedulingTaskExecutor. 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: DefaultMessageListenerContainer.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public void initialize() {
	// Adapt default cache level.
	if (this.cacheLevel == CACHE_AUTO) {
		this.cacheLevel = (getTransactionManager() != null ? CACHE_NONE : CACHE_CONSUMER);
	}

	// Prepare taskExecutor and maxMessagesPerTask.
	synchronized (this.lifecycleMonitor) {
		if (this.taskExecutor == null) {
			this.taskExecutor = createDefaultTaskExecutor();
		}
		else if (this.taskExecutor instanceof SchedulingTaskExecutor &&
				((SchedulingTaskExecutor) this.taskExecutor).prefersShortLivedTasks() &&
				this.maxMessagesPerTask == Integer.MIN_VALUE) {
			// TaskExecutor indicated a preference for short-lived tasks. According to
			// setMaxMessagesPerTask javadoc, we'll use 10 message per task in this case
			// unless the user specified a custom value.
			this.maxMessagesPerTask = 10;
		}
	}

	// Proceed with actual listener initialization.
	super.initialize();
}
 
Example #2
Source File: DefaultMessageListenerContainer.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public void initialize() {
	// Adapt default cache level.
	if (this.cacheLevel == CACHE_AUTO) {
		this.cacheLevel = (getTransactionManager() != null ? CACHE_NONE : CACHE_CONSUMER);
	}

	// Prepare taskExecutor and maxMessagesPerTask.
	synchronized (this.lifecycleMonitor) {
		if (this.taskExecutor == null) {
			this.taskExecutor = createDefaultTaskExecutor();
		}
		else if (this.taskExecutor instanceof SchedulingTaskExecutor &&
				((SchedulingTaskExecutor) this.taskExecutor).prefersShortLivedTasks() &&
				this.maxMessagesPerTask == Integer.MIN_VALUE) {
			// TaskExecutor indicated a preference for short-lived tasks. According to
			// setMaxMessagesPerTask javadoc, we'll use 10 message per task in this case
			// unless the user specified a custom value.
			this.maxMessagesPerTask = 10;
		}
	}

	// Proceed with actual listener initialization.
	super.initialize();
}
 
Example #3
Source File: PreservesExecutionContextExecutorStrategyTest.java    From spring-cloud-ribbon-extensions with Apache License 2.0 6 votes vote down vote up
@Test
public void postProcessAfterInitialization() throws Exception {
    assertThat(processor.postProcessAfterInitialization(mock(Executor.class), toBeExcluded).getClass(),
            not(equalTo(ContextAwareExecutor.class)));
    //concurrent
    assertThat(processor.postProcessAfterInitialization(mock(Executor.class), beanName).getClass(),
            equalTo(ContextAwareExecutor.class));
    assertThat(processor.postProcessAfterInitialization(mock(ExecutorService.class), beanName).getClass(),
            equalTo(ContextAwareExecutorService.class));
    assertThat(processor.postProcessAfterInitialization(mock(ScheduledExecutorService.class), beanName).getClass(),
            equalTo(ContextAwareScheduledExecutorService.class));

    //spring
    assertThat(processor.postProcessAfterInitialization(mock(TaskScheduler.class), beanName).getClass(),
            equalTo(ContextAwareTaskScheduler.class));
    assertThat(processor.postProcessAfterInitialization(new ThreadPoolTaskExecutor(), beanName).getClass(),
            equalTo(ContextAwareThreadPoolTaskExecutor.class));
    assertThat(processor.postProcessAfterInitialization(new ThreadPoolTaskScheduler(), beanName).getClass(),
            equalTo(ContextAwareThreadPoolTaskScheduler.class));
    assertThat(processor.postProcessAfterInitialization(mock(AsyncListenableTaskExecutor.class), beanName).getClass(),
            equalTo(ContextAwareAsyncListenableTaskExecutor.class));
    assertThat(processor.postProcessAfterInitialization(mock(AsyncTaskExecutor.class), beanName).getClass(),
            equalTo(ContextAwareAsyncTaskExecutor.class));
    assertThat(processor.postProcessAfterInitialization(mock(SchedulingTaskExecutor.class), beanName).getClass(),
            equalTo(ContextAwareSchedulingTaskExecutor.class));
}
 
Example #4
Source File: DefaultMessageListenerContainer.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public void initialize() {
	// Adapt default cache level.
	if (this.cacheLevel == CACHE_AUTO) {
		this.cacheLevel = (getTransactionManager() != null ? CACHE_NONE : CACHE_CONSUMER);
	}

	// Prepare taskExecutor and maxMessagesPerTask.
	synchronized (this.lifecycleMonitor) {
		if (this.taskExecutor == null) {
			this.taskExecutor = createDefaultTaskExecutor();
		}
		else if (this.taskExecutor instanceof SchedulingTaskExecutor &&
				((SchedulingTaskExecutor) this.taskExecutor).prefersShortLivedTasks() &&
				this.maxMessagesPerTask == Integer.MIN_VALUE) {
			// TaskExecutor indicated a preference for short-lived tasks. According to
			// setMaxMessagesPerTask javadoc, we'll use 10 message per task in this case
			// unless the user specified a custom value.
			this.maxMessagesPerTask = 10;
		}
	}

	// Proceed with actual listener initialization.
	super.initialize();
}
 
Example #5
Source File: PreservesExecutionContextExecutorStrategy.java    From spring-cloud-ribbon-extensions with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) {
    if (bean instanceof Executor || bean instanceof TaskScheduler) {
        if (properties.getExecutor().accept(beanName)) {
            if (bean instanceof AsyncListenableTaskExecutor && bean instanceof SchedulingTaskExecutor && bean instanceof TaskScheduler) {
                log.info("Context propagation enabled for ~ThreadPoolTaskScheduler [{}]:[{}].", beanName, bean.getClass().getName());
                return new ContextAwareThreadPoolTaskScheduler((AsyncListenableTaskExecutor) bean, (SchedulingTaskExecutor) bean, (TaskScheduler) bean);
            } else if (bean instanceof AsyncListenableTaskExecutor && bean instanceof SchedulingTaskExecutor) {
                log.info("Context propagation enabled for ~ThreadPoolTaskExecutor [{}]:[{}].", beanName, bean.getClass().getName());
                return new ContextAwareThreadPoolTaskExecutor((AsyncListenableTaskExecutor) bean, (SchedulingTaskExecutor) bean);
            } else if (bean instanceof TaskScheduler) {
                log.info("Context propagation enabled for TaskScheduler [{}]:[{}].", beanName, bean.getClass().getName());
                return new ContextAwareTaskScheduler((TaskScheduler) bean);
            } else if (bean instanceof SchedulingTaskExecutor) {
                log.info("Context propagation enabled for SchedulingTaskExecutor [{}]:[{}].", beanName, bean.getClass().getName());
                return new ContextAwareSchedulingTaskExecutor((SchedulingTaskExecutor) bean);
            } else if (bean instanceof AsyncListenableTaskExecutor) {
                log.info("Context propagation enabled for AsyncListenableTaskExecutor [{}]:[{}].", beanName, bean.getClass().getName());
                return new ContextAwareAsyncListenableTaskExecutor((AsyncListenableTaskExecutor) bean);
            } else if (bean instanceof AsyncTaskExecutor) {
                log.info("Context propagation enabled for AsyncTaskExecutor [{}]:[{}].", beanName, bean.getClass().getName());
                return new ContextAwareAsyncTaskExecutor((AsyncTaskExecutor) bean);
            } else if (bean instanceof ScheduledExecutorService) {
                log.info("Context propagation enabled for ScheduledExecutorService [{}]:[{}].", beanName, bean.getClass().getName());
                return new ContextAwareScheduledExecutorService((ScheduledExecutorService) bean);
            } else if (bean instanceof ExecutorService) {
                log.info("Context propagation enabled for ExecutorService [{}]:[{}].", beanName, bean.getClass().getName());
                return new ContextAwareExecutorService((ExecutorService) bean);
            } else {
                log.info("Context propagation enabled for Executor [{}]:[{}].", beanName, bean.getClass().getName());
                return new ContextAwareExecutor((Executor) bean);
            }
        } else {
            log.debug("Context propagation disabled for Executor [{}]", beanName);
        }
    }
    return bean;
}
 
Example #6
Source File: ContextAwareThreadPoolTaskScheduler.java    From spring-cloud-ribbon-extensions with Apache License 2.0 3 votes vote down vote up
/**
 * Sole Constructor.
 *
 * @param asyncListenableTaskExecutor The delegate async listenable task executor.
 * @param schedulingTaskExecutor      The delegate scheduling task executor.
 * @param taskScheduler               the delegate task scheduler.
 */
public ContextAwareThreadPoolTaskScheduler(@NotNull AsyncListenableTaskExecutor asyncListenableTaskExecutor,
                                           @NotNull SchedulingTaskExecutor schedulingTaskExecutor,
                                           @NotNull TaskScheduler taskScheduler) {
    super(asyncListenableTaskExecutor, schedulingTaskExecutor);
    executionContextAwareTaskScheduler = new ContextAwareTaskScheduler(taskScheduler);
}
 
Example #7
Source File: ContextAwareSchedulingTaskExecutor.java    From spring-cloud-ribbon-extensions with Apache License 2.0 2 votes vote down vote up
/**
 * Sole Constructor
 *
 * @param delegate the delegate scheduling task executor.
 */
public ContextAwareSchedulingTaskExecutor(@NotNull SchedulingTaskExecutor delegate) {
    super(delegate);
    this.delegate = delegate;
}
 
Example #8
Source File: ContextAwareThreadPoolTaskExecutor.java    From spring-cloud-ribbon-extensions with Apache License 2.0 2 votes vote down vote up
/**
 * Sole Constructor.
 *
 * @param asyncListenableTaskExecutor The delegate async listenable task executor.
 * @param schedulingTaskExecutor      The delegate scheduling task executor.
 */
public ContextAwareThreadPoolTaskExecutor(@NotNull AsyncListenableTaskExecutor asyncListenableTaskExecutor,
                                          @NotNull SchedulingTaskExecutor schedulingTaskExecutor) {
    asyncListenableTaskExecutorPropagator = new ContextAwareAsyncListenableTaskExecutor(asyncListenableTaskExecutor);
    schedulingTaskExecutorPropagator = new ContextAwareSchedulingTaskExecutor(schedulingTaskExecutor);
}