Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot 2 Converting Duration java 8 application.properties

I need to define Duration value (spring.redis.timeout) by application.properties.

I was trying to use one point defined in Spring boot documentation:

Spring Boot has dedicated support for expressing durations. If you expose a java.time.Duration property, the following formats in application properties are available:

A regular long representation (using milliseconds as the default unit unless a @DurationUnit has been specified) The standard ISO-8601 format used by java.util.Duration A more readable format where the value and the unit are coupled (e.g. 10s means 10 seconds)

When i use spring.redis.timeout=3s Spring boot application throws this exception:

Cannot convert value of type 'java.lang.String' to required type 'java.time.Duration': no matching editors or conversion strategy found

Which would it be the best way to set a correct value to a Duration property in application.properties withs last Spring boot 2 release?

like image 237
CRISTIAN ROMERO MATESANZ Avatar asked Aug 13 '18 08:08

CRISTIAN ROMERO MATESANZ


2 Answers

Any property which is of type duration can be injected via .properties or .yml files. All you need to do is use a proper formatting.

If you want to inject a duration of 5 seconds it should be defined as PT5S or pt5s or PT5s

  • case of the letters doesn't matter, so you use any combination which is readable for you
  • generally everyone uses all capital letters

Other examples

PT1.5S       = 1.5 Seconds
PT60S        = 60 Seconds
PT3M         = 3 Minutes
PT2H         = 2 Hours
P3DT5H40M30S = 3Days, 5Hours, 40 Minutes and 30 Seconds

You can also use +ve and -ve signs to denote positive vs negative period of time.

  • You can negate only one of the entity for example: PT-3H30M = -3 hours, +30 minutes, basically -2.5Hours
  • Or You can negate the whole entity: -PT3H30M = -3 hours, -30 minutes, basically -3.5Hours
  • Double negative works here too: -PT-3H+30M = +3 Hours, -30 Minutes, basically +2.5Hours

Note:

  • Durations can only be represented in HOURS or lower ChronoUnit (NANOS, MICROS, MILLIS, SECONDS, MINUTES, HOURS) since they represent accurate durations
  • Higher ChronoUnit (DAYS, WEEKS, MONTHS, YEARS, DECADES,CENTURIES, MILLENNIA, ERAS, FOREVER) are not allowed since they don't represent accurate duration. These ChronoUnits have estimated duration due to the possibility of Days varying due to daylight saving, Months have different lengths etc.
  • Exception - Java does automatic conversion of DAYS into HOURS, But it doesn't do it for any other higher ChronoUnit (MONTHS, YEARS etc.).

If we try to do a "P1D", java automatically converts it into "PT24H". So If we want to do duration of 1 MONTH, we will have to use PT720H or P30D. In case of P30D java's automatic conversion will take place and give us PT720H


Upvote, if it works for you or you like the explanation. Thanks,

like image 73
Rituraj Avatar answered Oct 19 '22 23:10

Rituraj


It's possible to use @Value notation with Spring Expression Language

@Value("#{T(java.time.Duration).parse('${spring.redis.timeout}')}")
private Duration timeout;
like image 35
Serge Avatar answered Oct 20 '22 00:10

Serge