Provide accessors to scheduled tasks

Updated ScheduledTaskRegistrar to offer accessors to the tasks
it is responsible to schedule.

Issue: SPR-12748
master
Tobias Montagna-Hay 10 years ago committed by Stephane Nicoll
parent dff2a3d180
commit 64467b4f59
  1. 36
      spring-context/src/main/java/org/springframework/scheduling/config/ScheduledTaskRegistrar.java
  2. 74
      spring-context/src/test/java/org/springframework/scheduling/config/ScheduledTaskRegistrarTest.java

@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -17,6 +17,7 @@
package org.springframework.scheduling.config;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
@ -47,6 +48,7 @@ import org.springframework.util.CollectionUtils;
*
* @author Juergen Hoeller
* @author Chris Beams
* @author Tobias Montagna-Hay
* @since 3.0
* @see org.springframework.scheduling.annotation.EnableAsync
* @see org.springframework.scheduling.annotation.SchedulingConfigurer
@ -123,6 +125,14 @@ public class ScheduledTaskRegistrar implements InitializingBean, DisposableBean
this.triggerTasks = triggerTasks;
}
/**
* Return the trigger tasks as a list of {@link TriggerTask}
* @since 4.2
*/
public List<TriggerTask> getTriggerTaskList() {
return Collections.unmodifiableList(this.triggerTasks);
}
/**
* Specify triggered tasks as a Map of Runnables (the tasks) and cron expressions.
* @see CronTrigger
@ -144,6 +154,14 @@ public class ScheduledTaskRegistrar implements InitializingBean, DisposableBean
this.cronTasks = cronTasks;
}
/**
* Return the cron tasks as a list of {@link CronTask}
* @since 4.2
*/
public List<CronTask> getCronTaskList() {
return Collections.unmodifiableList(this.cronTasks);
}
/**
* Specify triggered tasks as a Map of Runnables (the tasks) and fixed-rate values.
* @see TaskScheduler#scheduleAtFixedRate(Runnable, long)
@ -165,6 +183,14 @@ public class ScheduledTaskRegistrar implements InitializingBean, DisposableBean
this.fixedRateTasks = fixedRateTasks;
}
/**
* Return the fixed-rate tasks as a list of {@link IntervalTask}.
* @since 4.2
*/
public List<IntervalTask> getFixedRateTaskList() {
return Collections.unmodifiableList(this.fixedRateTasks);
}
/**
* Specify triggered tasks as a Map of Runnables (the tasks) and fixed-delay values.
* @see TaskScheduler#scheduleWithFixedDelay(Runnable, long)
@ -186,6 +212,14 @@ public class ScheduledTaskRegistrar implements InitializingBean, DisposableBean
this.fixedDelayTasks = fixedDelayTasks;
}
/**
* Return the fixed-delay tasks as a list of {@link IntervalTask}
* @since 4.2
*/
public List<IntervalTask> getFixedDelayTaskList() {
return Collections.unmodifiableList(this.fixedDelayTasks);
}
/**
* Add a Runnable task to be triggered per the given {@link Trigger}.
* @see TaskScheduler#scheduleAtFixedRate(Runnable, long)

@ -0,0 +1,74 @@
/*
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.scheduling.config;
import java.util.Collections;
import java.util.List;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
/**
* @author Tobias Montagna-Hay
*/
public class ScheduledTaskRegistrarTest {
private final ScheduledTaskRegistrar taskRegistrar = new ScheduledTaskRegistrar();
@Test
public void getTriggerTasks() {
TriggerTask mockTriggerTask = mock(TriggerTask.class);
List<TriggerTask> triggerTaskList = Collections.singletonList(mockTriggerTask);
this.taskRegistrar.setTriggerTasksList(triggerTaskList);
List<TriggerTask> retrievedList = this.taskRegistrar.getTriggerTaskList();
assertEquals(1, retrievedList.size());
assertEquals(mockTriggerTask, retrievedList.get(0));
}
@Test
public void getCronTasks() {
CronTask mockCronTask = mock(CronTask.class);
List<CronTask> cronTaskList = Collections.singletonList(mockCronTask);
this.taskRegistrar.setCronTasksList(cronTaskList);
List<CronTask> retrievedList = this.taskRegistrar.getCronTaskList();
assertEquals(1, retrievedList.size());
assertEquals(mockCronTask, retrievedList.get(0));
}
@Test
public void testGetFixedRateTasks() {
IntervalTask mockFixedRateTask = mock(IntervalTask.class);
List<IntervalTask> fixedRateTaskList = Collections.singletonList(mockFixedRateTask);
this.taskRegistrar.setFixedRateTasksList(fixedRateTaskList);
List<IntervalTask> retrievedList = this.taskRegistrar.getFixedRateTaskList();
assertEquals(1, retrievedList.size());
assertEquals(mockFixedRateTask, retrievedList.get(0));
}
@Test
public void testGetFixedDelayTasks() {
IntervalTask mockFixedDelayTask = mock(IntervalTask.class);
List<IntervalTask> fixedDelayTaskList = Collections.singletonList(mockFixedDelayTask);
this.taskRegistrar.setFixedDelayTasksList(fixedDelayTaskList);
List<IntervalTask> retrievedList = this.taskRegistrar.getFixedDelayTaskList();
assertEquals(1, retrievedList.size());
assertEquals(mockFixedDelayTask, retrievedList.get(0));
}
}
Loading…
Cancel
Save