How to read fixedDelayMinutes from yml file and shedule the job in spring-boot

Viewed 198

I want to read value from yml in minutes and then in scheduler give the value in milliseconds

e.g. for 5 min 5 * 60 * 1000

@Scheduled(fixedDelayString = "${jobintervalmins} * 60 * 1000") I have tried like this, but it is not working

I am getting error : Invalid fixedDelayString value "5 * 60 * 1000" - cannot parse into long

properties file will contain jobintervalmins: 5

1 Answers

You can't specifically have it interpret the value as minutes, but you can have it parsed as a Duration. The string value is parsed to a long using this code:

private static long parseDelayAsLong(String value) throws RuntimeException {
    if (value.length() > 1 && (isP(value.charAt(0)) || isP(value.charAt(1)))) {
        return Duration.parse(value).toMillis();
    }
    return Long.parseLong(value);
}

If the value has a P as its first or second character, it'll be turned into a long using Duration.parse(value). For example, a value of 5 minutes can be expressed as PT5M.

You could configure your annotation to use ${jobinterval}:

@Scheduled(fixedDelayString = "${jobinterval}")

You can then specify a numeric value in your yaml file that will be used as milliseconds:

jobinterval: 30000

Or you can specify a textural representation of a duration using whatever unit you want:

jobinterval: PT5M
jobinternal: PT300S
Related