1

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

S.R
  • 113
  • 11

1 Answers1

2

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
Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
  • I want to give the jobinterval in minutes like , jobinterval: 5, Then I want to calculate the minute into milliseconds in the annotation. Is it possible – S.R Jun 15 '21 at 17:50
  • No, that's not possible. The value of the annotation either has to be a number of milliseconds or a string that can be parsed into a duration. `PT5M`, which is how `Duration` parsing expects 5 minutes to be represented, is the closest you can get. – Andy Wilkinson Jun 15 '21 at 19:05