Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are Spring <task:scheduled> objects represented at runtime?

Tags:

spring

I have an app that uses the "task:scheduler" and "task:scheduled-tasks" elements (the latter containing "task:scheduled" elements). This is all working fine.

I'm trying to write some code that introspects the "application configuration" to get a short summary of some important information, like what tasks are scheduled and what their schedule is.

I already have a class that has a bunch of "@Autowired" instance variables so I can iterate through all of this. It was easy enough to add a "List" to get all of the TaskScheduler objects. I only have two of these, and I have a different set of scheduled tasks in each of them.

What I can't see in those TaskScheduler objects (they are actually ThreadPoolTaskScheduler objects) is anything that looks like a list of scheduled tasks, so I'm guessing the list of scheduled tasks is recorded somewhere else.

What objects can I use to introspect the set of scheduled tasks, and which thread pool they are in?

like image 364
David M. Karr Avatar asked Feb 15 '14 00:02

David M. Karr


People also ask

Is spring scheduled async?

The Spring Framework provides abstractions for asynchronous execution and scheduling of tasks with the TaskExecutor and TaskScheduler interfaces, respectively. Spring also features implementations of those interfaces that support thread pools or delegation to CommonJ within an application server environment.

Is Spring scheduler single threaded?

By default, Spring uses a local single-threaded scheduler to run the tasks. As a result, even if we have multiple @Scheduled methods, they each need to wait for the thread to complete executing a previous task.


1 Answers

This functionality will be available in Spring 4.2

https://jira.spring.io/browse/SPR-12748 (Disclaimer: I reported this issue and contributed code towards its solution).

// Warning there may be more than one ScheduledTaskRegistrar in your
// application context. If this is the case you can autowire a list of 
// ScheduledTaskRegistrar instead.
@Autowired   
private ScheduledTaskRegistrar scheduledTaskRegistrar; 

public List<Task> getScheduledTasks() {
    List<Task> result = new ArrayList<Task>();
    result.addAll(this.scheduledTaskRegistrar.getTriggerTaskList());
    result.addAll(this.scheduledTaskRegistrar.getCronTaskList());
    result.addAll(this.scheduledTaskRegistrar.getFixedRateTaskList());
    result.addAll(this.scheduledTaskRegistrar.getFixedDelayTaskList());
    return result;
}

// You can this inspect the tasks, 
// so for example a cron task can be inspected like this:

public List<CronTask> getScheduledCronTasks() {
    List<CronTask> cronTaskList = this.scheduledTaskRegistrar.getCronTaskList();
    for (CronTask cronTask : cronTaskList) {
        System.out.println(cronTask.getExpression);
    }
    return cronTaskList;
}

If you are using a ScheduledMethodRunnable defined in XML:

<task:scheduled method="run" cron="0 0 12 * * ?" ref="MyObject" />

You can access the underlying target object:

 ScheduledMethodRunnable scheduledMethodRunnable = (ScheduledMethodRunnable) task.getRunnable();
 TargetClass target = (TargetClass) scheduledMethodRunnable.getTarget();
like image 68
Tobias M Avatar answered Oct 21 '22 10:10

Tobias M