3

Imagine I have a Duration value defined in my application.conf file. The documentation says it can parse Durations, but I can't see how.

timeout = 60 milliseconds

Can I parse it directly as a duration value? Ideally I would like to do something like this

val timeout = current.configuration.getMilliseconds("timeout")
(myActor ? GiveMeSomething)(timeout).mapTo[...]

but timeout is an Option[Long]. Thanks for any tips.

ticofab
  • 7,551
  • 13
  • 49
  • 90
  • Typesafe's `Config` class reads HOCON files and provides a `getDuration(path: String): Duration` method. I guess you'd have to do all the legwork of figuring out the proper places from which to read the configuration files as [described in Play's docs](https://www.playframework.com/documentation/2.5.x/ProductionConfiguration) – Carl G Jun 19 '16 at 23:28

5 Answers5

2

Play's configuration does support Duration, FiniteDuration and other Scala types. Use: configuration.get[FiniteDuration]("path.to.duration") The Configuration.get function takes an implicit ConfigLoader[A]. Play comes with a lot of implementations and easy to add additional ones.

Joost den Boer
  • 4,556
  • 4
  • 25
  • 39
0

Try getDuration(String, TimeUnit) method.

val timeout = config.getDuration("timeout", TimeUnit.MILLISECONDS)
grzesiekw
  • 477
  • 4
  • 8
  • 2
    I don't think that method exists on the Play `Configuration` class. It does exist on the Typesafe `Config` class, but it returns a long. I think the question is to get a `Duration` return type. So one answer would be to use the `.getDuration(path: String): Duration` method on the Typesafe `Config` class. – Carl G Jun 19 '16 at 23:32
0

try something like this:

import scala.concurrent.duration._
config.getMilliseconds("timeout").map(_.milliseconds)

that'll give you an Option[Duration]; you can get/getOrElse to get a concrete value.

mizerlou
  • 156
  • 1
  • 6
0

You can use the Duration apply method as such:

Duration.apply(configuration.get[String]("timeout"))

And then in configuration you would have something like this:

timeout = 15 seconds
-5

I don't believe you can parse it directly as a duration value.

I usually have a helper class/method that does it for me, similar to this: http://pierreandrews.net/posts/config-scala.html

colinjwebb
  • 4,362
  • 7
  • 31
  • 35