tags:

views:

47

answers:

1

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 just once, a fixed delay after startup, and then never again (i.e. what setting repeatCount to 0 on a SimpleTriggerBean would achieve).

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

+1  A: 

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.

seanizer