2016-01-09 29 views
0

想象一下,我在我的application.conf文件中定義了持續時間值。 documentation表示它可以解析持續時間,但我看不到如何。Scala + Play,如何從HOCON配置獲取持續時間?

timeout = 60 milliseconds 

我可以直接解析它作爲持續時間值嗎?理想情況下,我想要做這樣的事情

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

但超時是Option[Long]。感謝您的任何提示。

+0

Typesafe的'Config'類讀取HOCON文件並提供'getDuration(path:String):Duration'方法。我想你必須做所有的工作,找出合適的地方來閱讀配置文件(如Play的文檔中所述)(https://www.playframework.com/documentation/2.5.x/ProductionConfiguration) –

回答

0

嘗試getDuration(字符串,TIMEUNIT)方法。

val timeout = config.getDuration("timeout", TimeUnit.MILLISECONDS) 
+1

我不認爲Play'Configuration'類存在這種方法。它存在於Typesafe的'Config'類中,但它返回一個長整型。我認爲問題是得到一個'持續時間'返回類型。所以一個答案就是在Typesafe的'Config'類中使用'.getDuration(path:String):Duration'方法。 –

0

嘗試這樣:

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

這會給你一個Option[Duration];你可以通過get/getOrElse來獲得具體的價值。

1

Play的配置確實支持Duration,FiniteDuration和其他Scala類型。使用: configuration.get[FiniteDuration]("path.to.duration") Configuration.get功能需要隱含ConfigLoader[A]。玩帶有lot of implementations和容易添加額外的。

相關問題