使用Spring任务命名空间调度任务运行一次 [英] Scheduling tasks to run once, using the Spring task namespace

查看:111
本文介绍了使用Spring任务命名空间调度任务运行一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用任务命名空间在spring中设置一个计划任务方案。

I'm setting up a scheduled tasks scheme in spring, using the task namespace.

我想安排大多数任务根据cron表达式进行计划,有些只需启动一次,启动后固定延迟,然后再也不会(即设置什么) SimpleTriggerBean上的 repeatCount 0 将实现)。

I want to schedule most tasks to fire according to a cron expression, and some to fire just once, a fixed delay after startup, and then never again (i.e. what setting repeatCount to 0 on a SimpleTriggerBean would achieve).

是否有可能在任务命名空间中实现这一点,或者我是否需要恢复为我的触发器定义bean?

Is it possible to achieve this within the task namespace, or do I need to revert to defining beans for my triggers?

推荐答案

如果您查看任务名称空间XSD ,那么'将看到只有三种不同的配置类型: fixed-delay fixed-rate cron

If you have a look at the Task namespace XSD, you'll see that there are only three different configuration types: fixed-delay, fixed-rate and cron.

如果你看一下 ScheduledTasksBeanDefinitionParser ,您将看到不会评估其中一个值。以下是相关部分:

And if you look at the source of ScheduledTasksBeanDefinitionParser, you'll see that no more than one of these values are evaluated. Here is the relevant part:

String cronAttribute = taskElement.getAttribute("cron");
if (StringUtils.hasText(cronAttribute)) {
    cronTaskMap.put(runnableBeanRef, cronAttribute);
}
else {
    String fixedDelayAttribute = taskElement.getAttribute("fixed-delay");
    if (StringUtils.hasText(fixedDelayAttribute)) {
        fixedDelayTaskMap.put(runnableBeanRef, fixedDelayAttribute);
    }
    else {
        String fixedRateAttribute = taskElement.getAttribute("fixed-rate");
        if (!StringUtils.hasText(fixedRateAttribute)) {
            parserContext.getReaderContext().error(
                    "One of 'cron', 'fixed-delay', or 'fixed-rate' is required",
                    taskElement);
            // Continue with the possible next task element
            continue;
        }
        fixedRateTaskMap.put(runnableBeanRef, fixedRateAttribute);
    }
}

因此无法合并这些属性。简而言之:命名空间不会让你到那里。

So there is no way to combine these attributes. In short: the namespace won't get you there.

这篇关于使用Spring任务命名空间调度任务运行一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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