Manually constructing WSClient instances

323 views
Skip to first unread message

Jeff White

unread,
Dec 13, 2017, 5:25:19 PM12/13/17
to Play Framework
I would like to configure different timeout settings for each of my clients. Given a config snippet like such:

fooClient {
  baseUrl = "http://example.com"
  play.ws.timeout.connection = 2s
  play.ws.timeout.idle = 20s
  play.ws.timeout.request = 20s
}

Would this be the idiomatic way of constructing a WSClient, with fallback defaulting to values in my application.conf, and finally reference.conf in the play lib?
wsConfig points to the fooClient element.

def wsClient(wsConfig: Config, applicationLifecycle: ApplicationLifecycle)(implicit mat: Materializer): WSClient = {
  val client = new AhcWSClient(StandaloneAhcWSClient(config = AhcWSClientConfigFactory.forConfig(wsConfig.withFallback(ConfigFactory.load))))
  lifecycle.addStopHook(() => Future.successful(client.close()))
  client
}

Is the lifecycle hook important, since the WSClient is going to live for the duration of the JVM process? I suppose there could be resource leakage during development mode with hot-reload...

thanks
jeff

Jeff White

unread,
Dec 13, 2017, 7:21:45 PM12/13/17
to Play Framework
I've simplified it to

Enter def wsClient(wsConfig: Config, applicationLifecycle: ApplicationLifecycle)(implicit mat: Materializer): WSClient = {
  val client = AhcWSClient(AhcWSClientConfigFactory.forConfig(wsConfig.withFallback(ConfigFactory.load)))
  lifecycle.addStopHook(() => Future.successful(client.close()))
  client
}

Jeff White

unread,
Dec 13, 2017, 9:42:19 PM12/13/17
to Play Framework
Also, for a play app, is there any value in wrapping the StandaloneWSClient in a WSClient?

Will Sargent

unread,
Dec 13, 2017, 11:18:29 PM12/13/17
to play-fr...@googlegroups.com
The WSClient gives you access to Play specific classes, notably MultipartFormData.

You can see some examples of custom manually constructed WSClients in


Notably:


--
You received this message because you are subscribed to the Google Groups "Play Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/af98efdf-ffdb-40b8-938e-f6d84cdfd585%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Jeff White

unread,
Dec 15, 2017, 2:37:10 PM12/15/17
to Play Framework
Thanks, Will. That example doesn't compile for 2.6, unfortunately.

Also, I'm trying to understand more generally what the lets-encrypt example:

private lazy val client = {
  val configuration = Configuration.reference ++ Configuration(wsConfig)
  val parser = new WSConfigParser(configuration, env)
  val config = new AhcWSClientConfig(wsClientConfig = parser.parse())
  val builder = new AhcConfigBuilder(config)
  val client = AhcWSClient(builder.build())
  lifecycle.addStopHook { () =>
    Future(client.close())
  }
  client
}

provides compared to a simpler:

def get(wsConfig: Config): WSClient = {
  val config = AhcWSClientConfigFactory.forConfig(wsConfig.withFallback(ConfigFactory.load))
  val client = AhcWSClient(config)
  applicationLifecycle.addStopHook(() => Future.successful(client.close()))
  client
}

Basically, I want to ensure I'm getting all the Play defaults with my overrides applied.

thanks
jeff
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.

Will Sargent

unread,
Dec 15, 2017, 3:34:31 PM12/15/17
to play-fr...@googlegroups.com
  val configuration = Configuration.reference ++ Configuration(wsConfig)

is different from ConfigFactory.load and your example, because it doesn't pull in the application.conf at all.

 WSClient is going to live for the duration of the JVM process? I suppose there could be resource leakage during development mode with hot-reload...

Yes, that's what the lifecycle hook is therefore -- otherwise reloading a running app will cause a thread leak.  There are warnings on the WS page to that effect.

 any value in wrapping the StandaloneWSClient in a WSClient?

In addition to the play specific extensions, you may also want to use the HTTP caching feature, which is set up in Play itself.


To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/09817464-c9a9-4945-aad9-8ba8b2388411%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages