Spring< task:scheduled>对象在运行时表示? [英] How are Spring <task:scheduled> objects represented at runtime?

查看:46
本文介绍了Spring< task:scheduled>对象在运行时表示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用"task:scheduler"和"task:scheduled-tasks"元素的应用程序(后者包含"task:scheduled"元素).一切都很好.

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.

我已经有一个包含一堆"@Autowired"实例变量的类,因此我可以遍历所有这些实例变量.添加一个列表"以获取所有TaskScheduler对象非常容易.我只有两个,而且每个任务都有一组不同的计划任务.

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.

我在那些TaskScheduler对象(实际上是ThreadPoolTask​​Scheduler对象)中看不到的东西看起来像是计划任务列表,所以我猜想计划任务列表记录在其他地方.

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?

推荐答案

此功能将在Spring 4.2中提供

This functionality will be available in Spring 4.2

https://jira.spring.io/browse/SPR-12748 (免责声明:我报告了此问题发行并贡献了解决方案的代码).

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;
}

如果您使用的是XML中定义的ScheduledMethodRunnable:

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();

这篇关于Spring&lt; task:scheduled&gt;对象在运行时表示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆