Scheduling tasks to run once, using the Spring task namespace

前端 未结 4 1834
暗喜
暗喜 2020-12-25 15:26

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

I want to schedule most tasks to fire according to a cron expression, and some to fire

4条回答
  •  情话喂你
    2020-12-25 15:38

    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.

    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.

提交回复
热议问题