org.springframework.scheduling.config.IntervalTask Java Examples

The following examples show how to use org.springframework.scheduling.config.IntervalTask. 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: ScheduledAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void metaAnnotationWithFixedRate() {
	BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
	BeanDefinition targetDefinition = new RootBeanDefinition(MetaAnnotationFixedRateTestBean.class);
	context.registerBeanDefinition("postProcessor", processorDefinition);
	context.registerBeanDefinition("target", targetDefinition);
	context.refresh();

	ScheduledTaskHolder postProcessor = context.getBean("postProcessor", ScheduledTaskHolder.class);
	assertEquals(1, postProcessor.getScheduledTasks().size());

	Object target = context.getBean("target");
	ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar)
			new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
	@SuppressWarnings("unchecked")
	List<IntervalTask> fixedRateTasks = (List<IntervalTask>)
			new DirectFieldAccessor(registrar).getPropertyValue("fixedRateTasks");
	assertEquals(1, fixedRateTasks.size());
	IntervalTask task = fixedRateTasks.get(0);
	ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task.getRunnable();
	Object targetObject = runnable.getTarget();
	Method targetMethod = runnable.getMethod();
	assertEquals(target, targetObject);
	assertEquals("checkForUpdates", targetMethod.getName());
	assertEquals(5000L, task.getInterval());
}
 
Example #2
Source File: ScheduledAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void metaAnnotationWithFixedRate() {
	BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
	BeanDefinition targetDefinition = new RootBeanDefinition(MetaAnnotationFixedRateTestBean.class);
	context.registerBeanDefinition("postProcessor", processorDefinition);
	context.registerBeanDefinition("target", targetDefinition);
	context.refresh();

	Object postProcessor = context.getBean("postProcessor");
	Object target = context.getBean("target");
	ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar)
			new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
	@SuppressWarnings("unchecked")
	List<IntervalTask> fixedRateTasks = (List<IntervalTask>)
			new DirectFieldAccessor(registrar).getPropertyValue("fixedRateTasks");
	assertEquals(1, fixedRateTasks.size());
	IntervalTask task = fixedRateTasks.get(0);
	ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task.getRunnable();
	Object targetObject = runnable.getTarget();
	Method targetMethod = runnable.getMethod();
	assertEquals(target, targetObject);
	assertEquals("checkForUpdates", targetMethod.getName());
	assertEquals(5000L, task.getInterval());
}
 
Example #3
Source File: ScheduledAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void fixedRateTaskWithInitialDelay() {
	BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
	BeanDefinition targetDefinition = new RootBeanDefinition(FixedRateWithInitialDelayTestBean.class);
	context.registerBeanDefinition("postProcessor", processorDefinition);
	context.registerBeanDefinition("target", targetDefinition);
	context.refresh();

	Object postProcessor = context.getBean("postProcessor");
	Object target = context.getBean("target");
	ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar)
			new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
	@SuppressWarnings("unchecked")
	List<IntervalTask> fixedRateTasks = (List<IntervalTask>)
			new DirectFieldAccessor(registrar).getPropertyValue("fixedRateTasks");
	assertEquals(1, fixedRateTasks.size());
	IntervalTask task = fixedRateTasks.get(0);
	ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task.getRunnable();
	Object targetObject = runnable.getTarget();
	Method targetMethod = runnable.getMethod();
	assertEquals(target, targetObject);
	assertEquals("fixedRate", targetMethod.getName());
	assertEquals(1000L, task.getInitialDelay());
	assertEquals(3000L, task.getInterval());
}
 
Example #4
Source File: ScheduledAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void fixedRateTask() {
	BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
	BeanDefinition targetDefinition = new RootBeanDefinition(FixedRateTestBean.class);
	context.registerBeanDefinition("postProcessor", processorDefinition);
	context.registerBeanDefinition("target", targetDefinition);
	context.refresh();

	Object postProcessor = context.getBean("postProcessor");
	Object target = context.getBean("target");
	ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar)
			new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
	@SuppressWarnings("unchecked")
	List<IntervalTask> fixedRateTasks = (List<IntervalTask>)
			new DirectFieldAccessor(registrar).getPropertyValue("fixedRateTasks");
	assertEquals(1, fixedRateTasks.size());
	IntervalTask task = fixedRateTasks.get(0);
	ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task.getRunnable();
	Object targetObject = runnable.getTarget();
	Method targetMethod = runnable.getMethod();
	assertEquals(target, targetObject);
	assertEquals("fixedRate", targetMethod.getName());
	assertEquals(0L, task.getInitialDelay());
	assertEquals(3000L, task.getInterval());
}
 
Example #5
Source File: ScheduledAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void fixedDelayTask() {
	BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
	BeanDefinition targetDefinition = new RootBeanDefinition(FixedDelayTestBean.class);
	context.registerBeanDefinition("postProcessor", processorDefinition);
	context.registerBeanDefinition("target", targetDefinition);
	context.refresh();

	Object postProcessor = context.getBean("postProcessor");
	Object target = context.getBean("target");
	ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar)
			new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
	@SuppressWarnings("unchecked")
	List<IntervalTask> fixedDelayTasks = (List<IntervalTask>)
			new DirectFieldAccessor(registrar).getPropertyValue("fixedDelayTasks");
	assertEquals(1, fixedDelayTasks.size());
	IntervalTask task = fixedDelayTasks.get(0);
	ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task.getRunnable();
	Object targetObject = runnable.getTarget();
	Method targetMethod = runnable.getMethod();
	assertEquals(target, targetObject);
	assertEquals("fixedDelay", targetMethod.getName());
	assertEquals(0L, task.getInitialDelay());
	assertEquals(5000L, task.getInterval());
}
 
Example #6
Source File: ScheduledAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void metaAnnotationWithFixedRate() {
	BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
	BeanDefinition targetDefinition = new RootBeanDefinition(MetaAnnotationFixedRateTestBean.class);
	context.registerBeanDefinition("postProcessor", processorDefinition);
	context.registerBeanDefinition("target", targetDefinition);
	context.refresh();

	ScheduledTaskHolder postProcessor = context.getBean("postProcessor", ScheduledTaskHolder.class);
	assertEquals(1, postProcessor.getScheduledTasks().size());

	Object target = context.getBean("target");
	ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar)
			new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
	@SuppressWarnings("unchecked")
	List<IntervalTask> fixedRateTasks = (List<IntervalTask>)
			new DirectFieldAccessor(registrar).getPropertyValue("fixedRateTasks");
	assertEquals(1, fixedRateTasks.size());
	IntervalTask task = fixedRateTasks.get(0);
	ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task.getRunnable();
	Object targetObject = runnable.getTarget();
	Method targetMethod = runnable.getMethod();
	assertEquals(target, targetObject);
	assertEquals("checkForUpdates", targetMethod.getName());
	assertEquals(5000L, task.getInterval());
}
 
Example #7
Source File: RemoteResourceRefresher.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
@Override
public void afterPropertiesSet() {

	for (final RemoteResource resource : this.discoveredResources) {
		addFixedDelayTask(new IntervalTask(new Runnable() {

			@Override
			public void run() {
				resource.verifyOrDiscover();
			}
		}, this.fixedDelay, this.initialDelay));
	}

	super.afterPropertiesSet();
}
 
Example #8
Source File: EnableSchedulingTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
	taskRegistrar.setScheduler(taskScheduler());
	taskRegistrar.addFixedRateTask(new IntervalTask(
			new Runnable() {
				@Override
				public void run() {
					worker().executedByThread = Thread.currentThread().getName();
				}
			},
			10, 0));
}
 
Example #9
Source File: ConfigClientRefreshAutoConfiguration.java    From spring-cloud-config-refresh with Apache License 2.0 5 votes vote down vote up
/**
 * Configures the scheduler by registering task responsible for periodically refreshing entire endpoint.
 *
 * @param taskRegistrar the task registrar
 */
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {

    final long interval = getRefreshIntervalInMilliseconds();

    logger.info(String.format("Scheduling config refresh task with %s second delay", refreshInterval));

    taskRegistrar.addFixedDelayTask(new IntervalTask(new Runnable() {
        @Override
        public void run() {
            refreshEndpoint.refresh();
        }
    }, interval, interval));
}
 
Example #10
Source File: ScheduledAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void propertyPlaceholderWithFixedRate() {
	BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
	BeanDefinition placeholderDefinition = new RootBeanDefinition(PropertyPlaceholderConfigurer.class);
	Properties properties = new Properties();
	properties.setProperty("fixedRate", "3000");
	properties.setProperty("initialDelay", "1000");
	placeholderDefinition.getPropertyValues().addPropertyValue("properties", properties);
	BeanDefinition targetDefinition = new RootBeanDefinition(PropertyPlaceholderWithFixedRateTestBean.class);
	context.registerBeanDefinition("placeholder", placeholderDefinition);
	context.registerBeanDefinition("postProcessor", processorDefinition);
	context.registerBeanDefinition("target", targetDefinition);
	context.refresh();

	Object postProcessor = context.getBean("postProcessor");
	Object target = context.getBean("target");
	ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar)
			new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
	@SuppressWarnings("unchecked")
	List<IntervalTask> fixedRateTasks = (List<IntervalTask>)
			new DirectFieldAccessor(registrar).getPropertyValue("fixedRateTasks");
	assertEquals(1, fixedRateTasks.size());
	IntervalTask task = fixedRateTasks.get(0);
	ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task.getRunnable();
	Object targetObject = runnable.getTarget();
	Method targetMethod = runnable.getMethod();
	assertEquals(target, targetObject);
	assertEquals("fixedRate", targetMethod.getName());
	assertEquals(1000L, task.getInitialDelay());
	assertEquals(3000L, task.getInterval());
}
 
Example #11
Source File: ScheduledAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void propertyPlaceholderWithFixedDelay() {
	BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
	BeanDefinition placeholderDefinition = new RootBeanDefinition(PropertyPlaceholderConfigurer.class);
	Properties properties = new Properties();
	properties.setProperty("fixedDelay", "5000");
	properties.setProperty("initialDelay", "1000");
	placeholderDefinition.getPropertyValues().addPropertyValue("properties", properties);
	BeanDefinition targetDefinition = new RootBeanDefinition(PropertyPlaceholderWithFixedDelayTestBean.class);
	context.registerBeanDefinition("placeholder", placeholderDefinition);
	context.registerBeanDefinition("postProcessor", processorDefinition);
	context.registerBeanDefinition("target", targetDefinition);
	context.refresh();

	Object postProcessor = context.getBean("postProcessor");
	Object target = context.getBean("target");
	ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar)
			new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
	@SuppressWarnings("unchecked")
	List<IntervalTask> fixedDelayTasks = (List<IntervalTask>)
			new DirectFieldAccessor(registrar).getPropertyValue("fixedDelayTasks");
	assertEquals(1, fixedDelayTasks.size());
	IntervalTask task = fixedDelayTasks.get(0);
	ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task.getRunnable();
	Object targetObject = runnable.getTarget();
	Method targetMethod = runnable.getMethod();
	assertEquals(target, targetObject);
	assertEquals("fixedDelay", targetMethod.getName());
	assertEquals(1000L, task.getInitialDelay());
	assertEquals(5000L, task.getInterval());
}
 
Example #12
Source File: ScheduledAnnotationBeanPostProcessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private void severalFixedRates(StaticApplicationContext context,
		BeanDefinition processorDefinition, BeanDefinition targetDefinition) {

	context.registerBeanDefinition("postProcessor", processorDefinition);
	context.registerBeanDefinition("target", targetDefinition);
	context.refresh();

	Object postProcessor = context.getBean("postProcessor");
	Object target = context.getBean("target");
	ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar)
			new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
	@SuppressWarnings("unchecked")
	List<IntervalTask> fixedRateTasks = (List<IntervalTask>)
			new DirectFieldAccessor(registrar).getPropertyValue("fixedRateTasks");
	assertEquals(2, fixedRateTasks.size());
	IntervalTask task1 = fixedRateTasks.get(0);
	ScheduledMethodRunnable runnable1 = (ScheduledMethodRunnable) task1.getRunnable();
	Object targetObject = runnable1.getTarget();
	Method targetMethod = runnable1.getMethod();
	assertEquals(target, targetObject);
	assertEquals("fixedRate", targetMethod.getName());
	assertEquals(0, task1.getInitialDelay());
	assertEquals(4000L, task1.getInterval());
	IntervalTask task2 = fixedRateTasks.get(1);
	ScheduledMethodRunnable runnable2 = (ScheduledMethodRunnable) task2.getRunnable();
	targetObject = runnable2.getTarget();
	targetMethod = runnable2.getMethod();
	assertEquals(target, targetObject);
	assertEquals("fixedRate", targetMethod.getName());
	assertEquals(2000L, task2.getInitialDelay());
	assertEquals(4000L, task2.getInterval());
}
 
Example #13
Source File: EnableSchedulingTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
	taskRegistrar.setScheduler(taskScheduler());
	taskRegistrar.addFixedRateTask(new IntervalTask(
			new Runnable() {
				@Override
				public void run() {
					worker().executedByThread = Thread.currentThread().getName();
				}
			},
			10, 0));
}
 
Example #14
Source File: ScheduledAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void propertyPlaceholderWithFixedRate(boolean durationFormat) {
	BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
	BeanDefinition placeholderDefinition = new RootBeanDefinition(PropertyPlaceholderConfigurer.class);
	Properties properties = new Properties();
	properties.setProperty("fixedRate", (durationFormat ? "PT3S" : "3000"));
	properties.setProperty("initialDelay", (durationFormat ? "PT1S" : "1000"));
	placeholderDefinition.getPropertyValues().addPropertyValue("properties", properties);
	BeanDefinition targetDefinition = new RootBeanDefinition(PropertyPlaceholderWithFixedRateTestBean.class);
	context.registerBeanDefinition("postProcessor", processorDefinition);
	context.registerBeanDefinition("placeholder", placeholderDefinition);
	context.registerBeanDefinition("target", targetDefinition);
	context.refresh();

	ScheduledTaskHolder postProcessor = context.getBean("postProcessor", ScheduledTaskHolder.class);
	assertEquals(1, postProcessor.getScheduledTasks().size());

	Object target = context.getBean("target");
	ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar)
			new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
	@SuppressWarnings("unchecked")
	List<IntervalTask> fixedRateTasks = (List<IntervalTask>)
			new DirectFieldAccessor(registrar).getPropertyValue("fixedRateTasks");
	assertEquals(1, fixedRateTasks.size());
	IntervalTask task = fixedRateTasks.get(0);
	ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task.getRunnable();
	Object targetObject = runnable.getTarget();
	Method targetMethod = runnable.getMethod();
	assertEquals(target, targetObject);
	assertEquals("fixedRate", targetMethod.getName());
	assertEquals(1000L, task.getInitialDelay());
	assertEquals(3000L, task.getInterval());
}
 
Example #15
Source File: ScheduledAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void propertyPlaceholderWithFixedDelay(boolean durationFormat) {
	BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
	BeanDefinition placeholderDefinition = new RootBeanDefinition(PropertyPlaceholderConfigurer.class);
	Properties properties = new Properties();
	properties.setProperty("fixedDelay", (durationFormat ? "PT5S" : "5000"));
	properties.setProperty("initialDelay", (durationFormat ? "PT1S" : "1000"));
	placeholderDefinition.getPropertyValues().addPropertyValue("properties", properties);
	BeanDefinition targetDefinition = new RootBeanDefinition(PropertyPlaceholderWithFixedDelayTestBean.class);
	context.registerBeanDefinition("postProcessor", processorDefinition);
	context.registerBeanDefinition("placeholder", placeholderDefinition);
	context.registerBeanDefinition("target", targetDefinition);
	context.refresh();

	ScheduledTaskHolder postProcessor = context.getBean("postProcessor", ScheduledTaskHolder.class);
	assertEquals(1, postProcessor.getScheduledTasks().size());

	Object target = context.getBean("target");
	ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar)
			new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
	@SuppressWarnings("unchecked")
	List<IntervalTask> fixedDelayTasks = (List<IntervalTask>)
			new DirectFieldAccessor(registrar).getPropertyValue("fixedDelayTasks");
	assertEquals(1, fixedDelayTasks.size());
	IntervalTask task = fixedDelayTasks.get(0);
	ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task.getRunnable();
	Object targetObject = runnable.getTarget();
	Method targetMethod = runnable.getMethod();
	assertEquals(target, targetObject);
	assertEquals("fixedDelay", targetMethod.getName());
	assertEquals(1000L, task.getInitialDelay());
	assertEquals(5000L, task.getInterval());
}
 
Example #16
Source File: ScheduledAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void composedAnnotationWithInitialDelayAndFixedRate() {
	BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
	BeanDefinition targetDefinition = new RootBeanDefinition(ComposedAnnotationFixedRateTestBean.class);
	context.registerBeanDefinition("postProcessor", processorDefinition);
	context.registerBeanDefinition("target", targetDefinition);
	context.refresh();

	ScheduledTaskHolder postProcessor = context.getBean("postProcessor", ScheduledTaskHolder.class);
	assertEquals(1, postProcessor.getScheduledTasks().size());

	Object target = context.getBean("target");
	ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar)
			new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
	@SuppressWarnings("unchecked")
	List<IntervalTask> fixedRateTasks = (List<IntervalTask>)
			new DirectFieldAccessor(registrar).getPropertyValue("fixedRateTasks");
	assertEquals(1, fixedRateTasks.size());
	IntervalTask task = fixedRateTasks.get(0);
	ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task.getRunnable();
	Object targetObject = runnable.getTarget();
	Method targetMethod = runnable.getMethod();
	assertEquals(target, targetObject);
	assertEquals("checkForUpdates", targetMethod.getName());
	assertEquals(5000L, task.getInterval());
	assertEquals(1000L, task.getInitialDelay());
}
 
Example #17
Source File: ScheduledAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void severalFixedRates(StaticApplicationContext context,
		BeanDefinition processorDefinition, BeanDefinition targetDefinition) {

	context.registerBeanDefinition("postProcessor", processorDefinition);
	context.registerBeanDefinition("target", targetDefinition);
	context.refresh();

	ScheduledTaskHolder postProcessor = context.getBean("postProcessor", ScheduledTaskHolder.class);
	assertEquals(2, postProcessor.getScheduledTasks().size());

	Object target = context.getBean("target");
	ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar)
			new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
	@SuppressWarnings("unchecked")
	List<IntervalTask> fixedRateTasks = (List<IntervalTask>)
			new DirectFieldAccessor(registrar).getPropertyValue("fixedRateTasks");
	assertEquals(2, fixedRateTasks.size());
	IntervalTask task1 = fixedRateTasks.get(0);
	ScheduledMethodRunnable runnable1 = (ScheduledMethodRunnable) task1.getRunnable();
	Object targetObject = runnable1.getTarget();
	Method targetMethod = runnable1.getMethod();
	assertEquals(target, targetObject);
	assertEquals("fixedRate", targetMethod.getName());
	assertEquals(0, task1.getInitialDelay());
	assertEquals(4000L, task1.getInterval());
	IntervalTask task2 = fixedRateTasks.get(1);
	ScheduledMethodRunnable runnable2 = (ScheduledMethodRunnable) task2.getRunnable();
	targetObject = runnable2.getTarget();
	targetMethod = runnable2.getMethod();
	assertEquals(target, targetObject);
	assertEquals("fixedRate", targetMethod.getName());
	assertEquals(2000L, task2.getInitialDelay());
	assertEquals(4000L, task2.getInterval());
}
 
Example #18
Source File: ScheduledAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void fixedRateTaskWithInitialDelay() {
	BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
	BeanDefinition targetDefinition = new RootBeanDefinition(FixedRateWithInitialDelayTestBean.class);
	context.registerBeanDefinition("postProcessor", processorDefinition);
	context.registerBeanDefinition("target", targetDefinition);
	context.refresh();

	ScheduledTaskHolder postProcessor = context.getBean("postProcessor", ScheduledTaskHolder.class);
	assertEquals(1, postProcessor.getScheduledTasks().size());

	Object target = context.getBean("target");
	ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar)
			new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
	@SuppressWarnings("unchecked")
	List<IntervalTask> fixedRateTasks = (List<IntervalTask>)
			new DirectFieldAccessor(registrar).getPropertyValue("fixedRateTasks");
	assertEquals(1, fixedRateTasks.size());
	IntervalTask task = fixedRateTasks.get(0);
	ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task.getRunnable();
	Object targetObject = runnable.getTarget();
	Method targetMethod = runnable.getMethod();
	assertEquals(target, targetObject);
	assertEquals("fixedRate", targetMethod.getName());
	assertEquals(1000L, task.getInitialDelay());
	assertEquals(3000L, task.getInterval());
}
 
Example #19
Source File: ScheduledAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void fixedRateTask() {
	BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
	BeanDefinition targetDefinition = new RootBeanDefinition(FixedRateTestBean.class);
	context.registerBeanDefinition("postProcessor", processorDefinition);
	context.registerBeanDefinition("target", targetDefinition);
	context.refresh();

	ScheduledTaskHolder postProcessor = context.getBean("postProcessor", ScheduledTaskHolder.class);
	assertEquals(1, postProcessor.getScheduledTasks().size());

	Object target = context.getBean("target");
	ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar)
			new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
	@SuppressWarnings("unchecked")
	List<IntervalTask> fixedRateTasks = (List<IntervalTask>)
			new DirectFieldAccessor(registrar).getPropertyValue("fixedRateTasks");
	assertEquals(1, fixedRateTasks.size());
	IntervalTask task = fixedRateTasks.get(0);
	ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task.getRunnable();
	Object targetObject = runnable.getTarget();
	Method targetMethod = runnable.getMethod();
	assertEquals(target, targetObject);
	assertEquals("fixedRate", targetMethod.getName());
	assertEquals(0L, task.getInitialDelay());
	assertEquals(3000L, task.getInterval());
}
 
Example #20
Source File: ScheduledAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void fixedDelayTask() {
	BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
	BeanDefinition targetDefinition = new RootBeanDefinition(FixedDelayTestBean.class);
	context.registerBeanDefinition("postProcessor", processorDefinition);
	context.registerBeanDefinition("target", targetDefinition);
	context.refresh();

	ScheduledTaskHolder postProcessor = context.getBean("postProcessor", ScheduledTaskHolder.class);
	assertEquals(1, postProcessor.getScheduledTasks().size());

	Object target = context.getBean("target");
	ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar)
			new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
	@SuppressWarnings("unchecked")
	List<IntervalTask> fixedDelayTasks = (List<IntervalTask>)
			new DirectFieldAccessor(registrar).getPropertyValue("fixedDelayTasks");
	assertEquals(1, fixedDelayTasks.size());
	IntervalTask task = fixedDelayTasks.get(0);
	ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task.getRunnable();
	Object targetObject = runnable.getTarget();
	Method targetMethod = runnable.getMethod();
	assertEquals(target, targetObject);
	assertEquals("fixedDelay", targetMethod.getName());
	assertEquals(0L, task.getInitialDelay());
	assertEquals(5000L, task.getInterval());
}
 
Example #21
Source File: EnableSchedulingTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
	taskRegistrar.setScheduler(taskScheduler());
	taskRegistrar.addFixedRateTask(new IntervalTask(
			new Runnable() {
				@Override
				public void run() {
					worker().executedByThread = Thread.currentThread().getName();
				}
			},
			10, 0));
}
 
Example #22
Source File: ScheduledAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void propertyPlaceholderWithFixedRate(boolean durationFormat) {
	BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
	BeanDefinition placeholderDefinition = new RootBeanDefinition(PropertySourcesPlaceholderConfigurer.class);
	Properties properties = new Properties();
	properties.setProperty("fixedRate", (durationFormat ? "PT3S" : "3000"));
	properties.setProperty("initialDelay", (durationFormat ? "PT1S" : "1000"));
	placeholderDefinition.getPropertyValues().addPropertyValue("properties", properties);
	BeanDefinition targetDefinition = new RootBeanDefinition(PropertyPlaceholderWithFixedRateTestBean.class);
	context.registerBeanDefinition("postProcessor", processorDefinition);
	context.registerBeanDefinition("placeholder", placeholderDefinition);
	context.registerBeanDefinition("target", targetDefinition);
	context.refresh();

	ScheduledTaskHolder postProcessor = context.getBean("postProcessor", ScheduledTaskHolder.class);
	assertEquals(1, postProcessor.getScheduledTasks().size());

	Object target = context.getBean("target");
	ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar)
			new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
	@SuppressWarnings("unchecked")
	List<IntervalTask> fixedRateTasks = (List<IntervalTask>)
			new DirectFieldAccessor(registrar).getPropertyValue("fixedRateTasks");
	assertEquals(1, fixedRateTasks.size());
	IntervalTask task = fixedRateTasks.get(0);
	ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task.getRunnable();
	Object targetObject = runnable.getTarget();
	Method targetMethod = runnable.getMethod();
	assertEquals(target, targetObject);
	assertEquals("fixedRate", targetMethod.getName());
	assertEquals(1000L, task.getInitialDelay());
	assertEquals(3000L, task.getInterval());
}
 
Example #23
Source File: ScheduledAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void propertyPlaceholderWithFixedDelay(boolean durationFormat) {
	BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
	BeanDefinition placeholderDefinition = new RootBeanDefinition(PropertySourcesPlaceholderConfigurer.class);
	Properties properties = new Properties();
	properties.setProperty("fixedDelay", (durationFormat ? "PT5S" : "5000"));
	properties.setProperty("initialDelay", (durationFormat ? "PT1S" : "1000"));
	placeholderDefinition.getPropertyValues().addPropertyValue("properties", properties);
	BeanDefinition targetDefinition = new RootBeanDefinition(PropertyPlaceholderWithFixedDelayTestBean.class);
	context.registerBeanDefinition("postProcessor", processorDefinition);
	context.registerBeanDefinition("placeholder", placeholderDefinition);
	context.registerBeanDefinition("target", targetDefinition);
	context.refresh();

	ScheduledTaskHolder postProcessor = context.getBean("postProcessor", ScheduledTaskHolder.class);
	assertEquals(1, postProcessor.getScheduledTasks().size());

	Object target = context.getBean("target");
	ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar)
			new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
	@SuppressWarnings("unchecked")
	List<IntervalTask> fixedDelayTasks = (List<IntervalTask>)
			new DirectFieldAccessor(registrar).getPropertyValue("fixedDelayTasks");
	assertEquals(1, fixedDelayTasks.size());
	IntervalTask task = fixedDelayTasks.get(0);
	ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task.getRunnable();
	Object targetObject = runnable.getTarget();
	Method targetMethod = runnable.getMethod();
	assertEquals(target, targetObject);
	assertEquals("fixedDelay", targetMethod.getName());
	assertEquals(1000L, task.getInitialDelay());
	assertEquals(5000L, task.getInterval());
}
 
Example #24
Source File: ScheduledAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void composedAnnotationWithInitialDelayAndFixedRate() {
	BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
	BeanDefinition targetDefinition = new RootBeanDefinition(ComposedAnnotationFixedRateTestBean.class);
	context.registerBeanDefinition("postProcessor", processorDefinition);
	context.registerBeanDefinition("target", targetDefinition);
	context.refresh();

	ScheduledTaskHolder postProcessor = context.getBean("postProcessor", ScheduledTaskHolder.class);
	assertEquals(1, postProcessor.getScheduledTasks().size());

	Object target = context.getBean("target");
	ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar)
			new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
	@SuppressWarnings("unchecked")
	List<IntervalTask> fixedRateTasks = (List<IntervalTask>)
			new DirectFieldAccessor(registrar).getPropertyValue("fixedRateTasks");
	assertEquals(1, fixedRateTasks.size());
	IntervalTask task = fixedRateTasks.get(0);
	ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task.getRunnable();
	Object targetObject = runnable.getTarget();
	Method targetMethod = runnable.getMethod();
	assertEquals(target, targetObject);
	assertEquals("checkForUpdates", targetMethod.getName());
	assertEquals(5000L, task.getInterval());
	assertEquals(1000L, task.getInitialDelay());
}
 
Example #25
Source File: ScheduledAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void severalFixedRates(StaticApplicationContext context,
		BeanDefinition processorDefinition, BeanDefinition targetDefinition) {

	context.registerBeanDefinition("postProcessor", processorDefinition);
	context.registerBeanDefinition("target", targetDefinition);
	context.refresh();

	ScheduledTaskHolder postProcessor = context.getBean("postProcessor", ScheduledTaskHolder.class);
	assertEquals(2, postProcessor.getScheduledTasks().size());

	Object target = context.getBean("target");
	ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar)
			new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
	@SuppressWarnings("unchecked")
	List<IntervalTask> fixedRateTasks = (List<IntervalTask>)
			new DirectFieldAccessor(registrar).getPropertyValue("fixedRateTasks");
	assertEquals(2, fixedRateTasks.size());
	IntervalTask task1 = fixedRateTasks.get(0);
	ScheduledMethodRunnable runnable1 = (ScheduledMethodRunnable) task1.getRunnable();
	Object targetObject = runnable1.getTarget();
	Method targetMethod = runnable1.getMethod();
	assertEquals(target, targetObject);
	assertEquals("fixedRate", targetMethod.getName());
	assertEquals(0, task1.getInitialDelay());
	assertEquals(4000L, task1.getInterval());
	IntervalTask task2 = fixedRateTasks.get(1);
	ScheduledMethodRunnable runnable2 = (ScheduledMethodRunnable) task2.getRunnable();
	targetObject = runnable2.getTarget();
	targetMethod = runnable2.getMethod();
	assertEquals(target, targetObject);
	assertEquals("fixedRate", targetMethod.getName());
	assertEquals(2000L, task2.getInitialDelay());
	assertEquals(4000L, task2.getInterval());
}
 
Example #26
Source File: ScheduledAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void fixedRateTaskWithInitialDelay() {
	BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
	BeanDefinition targetDefinition = new RootBeanDefinition(FixedRateWithInitialDelayTestBean.class);
	context.registerBeanDefinition("postProcessor", processorDefinition);
	context.registerBeanDefinition("target", targetDefinition);
	context.refresh();

	ScheduledTaskHolder postProcessor = context.getBean("postProcessor", ScheduledTaskHolder.class);
	assertEquals(1, postProcessor.getScheduledTasks().size());

	Object target = context.getBean("target");
	ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar)
			new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
	@SuppressWarnings("unchecked")
	List<IntervalTask> fixedRateTasks = (List<IntervalTask>)
			new DirectFieldAccessor(registrar).getPropertyValue("fixedRateTasks");
	assertEquals(1, fixedRateTasks.size());
	IntervalTask task = fixedRateTasks.get(0);
	ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task.getRunnable();
	Object targetObject = runnable.getTarget();
	Method targetMethod = runnable.getMethod();
	assertEquals(target, targetObject);
	assertEquals("fixedRate", targetMethod.getName());
	assertEquals(1000L, task.getInitialDelay());
	assertEquals(3000L, task.getInterval());
}
 
Example #27
Source File: ScheduledAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void fixedRateTask() {
	BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
	BeanDefinition targetDefinition = new RootBeanDefinition(FixedRateTestBean.class);
	context.registerBeanDefinition("postProcessor", processorDefinition);
	context.registerBeanDefinition("target", targetDefinition);
	context.refresh();

	ScheduledTaskHolder postProcessor = context.getBean("postProcessor", ScheduledTaskHolder.class);
	assertEquals(1, postProcessor.getScheduledTasks().size());

	Object target = context.getBean("target");
	ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar)
			new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
	@SuppressWarnings("unchecked")
	List<IntervalTask> fixedRateTasks = (List<IntervalTask>)
			new DirectFieldAccessor(registrar).getPropertyValue("fixedRateTasks");
	assertEquals(1, fixedRateTasks.size());
	IntervalTask task = fixedRateTasks.get(0);
	ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task.getRunnable();
	Object targetObject = runnable.getTarget();
	Method targetMethod = runnable.getMethod();
	assertEquals(target, targetObject);
	assertEquals("fixedRate", targetMethod.getName());
	assertEquals(0L, task.getInitialDelay());
	assertEquals(3000L, task.getInterval());
}
 
Example #28
Source File: ScheduledAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void fixedDelayTask() {
	BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
	BeanDefinition targetDefinition = new RootBeanDefinition(FixedDelayTestBean.class);
	context.registerBeanDefinition("postProcessor", processorDefinition);
	context.registerBeanDefinition("target", targetDefinition);
	context.refresh();

	ScheduledTaskHolder postProcessor = context.getBean("postProcessor", ScheduledTaskHolder.class);
	assertEquals(1, postProcessor.getScheduledTasks().size());

	Object target = context.getBean("target");
	ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar)
			new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
	@SuppressWarnings("unchecked")
	List<IntervalTask> fixedDelayTasks = (List<IntervalTask>)
			new DirectFieldAccessor(registrar).getPropertyValue("fixedDelayTasks");
	assertEquals(1, fixedDelayTasks.size());
	IntervalTask task = fixedDelayTasks.get(0);
	ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task.getRunnable();
	Object targetObject = runnable.getTarget();
	Method targetMethod = runnable.getMethod();
	assertEquals(target, targetObject);
	assertEquals("fixedDelay", targetMethod.getName());
	assertEquals(0L, task.getInitialDelay());
	assertEquals(5000L, task.getInterval());
}