Refresh Configuration object at runtime

1,409 views
Skip to first unread message

Chris Rimmer

unread,
Oct 3, 2014, 12:17:11 PM10/3/14
to play-fr...@googlegroups.com
Hi 

I'm new to Play and was looking for a way to load most of the applications settings from a database and having them refresh periodically.

I have worked out how I could load them when the Play server starts, by overriding the onLoadConfig method. But I cannot seem to find any way to replace the Configuration object returned by play.Play.application().configuration() at runtime. Is there a way to do this? Or would this require a plugin? 

Thanks
Chris

Will Sargent

unread,
Oct 3, 2014, 12:51:43 PM10/3/14
to play-fr...@googlegroups.com
Probably the best way to do this would be to load your own configuration, using ConfigFactory.load().


Will.

--
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-framewor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Chris Rimmer

unread,
Oct 6, 2014, 7:04:27 AM10/6/14
to play-fr...@googlegroups.com
So are you saying to not use play.Play.application().configuration() at all and just create my own Configuration object and use that instead?

James Roper

unread,
Oct 7, 2014, 1:25:16 AM10/7/14
to play-framework
Hi Chris,

What configuration do you want to periodically refresh?  The problem with arbitrarily refreshing configuration is that in many places (and as of Play 2.4, in most places) the configuration is read once and stored (ie, in a field) as a property of the component that's using it.  Reading directly out of Configuration every time the configuration is needed, in certain circumstances, would be very slow, and a number of significant performance improvements that we've implemented recently have been removing that exact behaviour.  So you can update the configuration, but it's going to have no impact on the code using it because the code only reads from it at startup.  If you want to reload all configuration, you have to restart the app.

However, if you want to just update certain configuration properties within your own application, then I would suggest not using Configuration at all (or only using it as a bootstrap), and using some other mechanism to share some mutable configuration.

Regards,

James
--
James Roper
Software Engineer

Typesafe – Build reactive apps!
Twitter: @jroper

Steven Marcus

unread,
Oct 7, 2014, 3:03:50 AM10/7/14
to play-fr...@googlegroups.com
Hi!

I've been looking at refreshable configuration for our app too. James makes the point that the Play configuration object really is intended to be read at startup and static during runtime. So it makes sense to use it to "bootstrap" the dynamic configuration objects in your application.

There are lots of ways to do dynamic configuration changes at runtime -- and I'm looking at melding the Spring Framework with Play. The hope is to make Play a little more "enterprisey" so I can continue to use it for what it's good at...

talks about a new @Refreshable annotation in Spring Framework intended for the use case of components that require dynamic configuration changes at runtime.

The video talks about the new Spring Cloud initiative which appears to be mixing in some components developed by Netflix -- including a dynamic configuration "server" that can be used with @Refreshable. But you can use your own sources of dynamic configuration data as needed.

So using Play configuration to bootstrap a Spring context, and using the refreshable mechanism to DI your Play controllers and services might be a way to proceed.

The Spring Scala project provides a scala dsl for defining your Spring beans, amongst other convenience features.
I'm hoping that this approach will be even more useful with Play 2.4 -- as DI becomes better supported in Play...

Anyway, I can't say I have this working, so it's just one option to consider...

Cheers,
Steven

Steven Marcus

unread,
Oct 7, 2014, 6:48:18 PM10/7/14
to play-fr...@googlegroups.com

If using the Spring DI container in Play doesn't appeal, here's an alternative:
https://github.com/Netflix/archaius
Archaius provides support for dynamic typed properties in Java and Scala.

(Also, a correction to my previous post: @Refreshable should be -> @RefreshScope)

Cheers

Chris Rimmer

unread,
Oct 8, 2014, 6:15:24 AM10/8/14
to play-fr...@googlegroups.com
Archaius looks to be what I want. I'm going to give that a try. Thanks :)
Reply all
Reply to author
Forward
0 new messages