| 1 | --- |
| 2 | layout: docs |
| 3 | title: Reload and Hot Reload |
| 4 | prev_section: variables-expansion |
| 5 | next_section: accessible-mutable |
| 6 | permalink: /docs/reload/ |
| 7 | --- |
| 8 | |
| 9 | Owner does support programmatic reload, as well as the automatic "hot reload" for configuration files. |
| 10 | |
| 11 | There are two ways on how the automatic HotReload can be implemented |
| 12 | with OWNER: synchronous or asynchronous. |
| 13 | |
| 14 | |
| 15 | Programmatic reload |
| 16 | ------------------- |
| 17 | |
| 18 | You can manually ask a configuration object to reload. This is done via |
| 19 | the [Reloadable] interface. |
| 20 | |
| 21 | Example: |
| 22 | |
| 23 | ```java |
| 24 | @Sources{...} |
| 25 | interface MyConfig extends Reloadable { |
| 26 | String someProperties(); |
| 27 | } |
| 28 | |
| 29 | MyConfig cfg = ConfigFactory.create(MyConfig.class); |
| 30 | cfg.reload(); |
| 31 | ``` |
| 32 | |
| 33 | The `cfg.reload()` will perform the reload of all properties in the same way as |
| 34 | when the object was initially created. If the configuration files have been |
| 35 | altered, after the reload invocation, those changes will be reflected in the |
| 36 | config object. |
| 37 | |
| 38 | The `Reloadable` interface extends from `Config`: |
| 39 | |
| 40 |  |
| 41 | |
| 42 | Automatic "hot reload" |
| 43 | ---------------------- |
| 44 | |
| 45 | You can instruct OWNER to automatically reload the properties files if they are |
| 46 | modified on the filesystem. |
| 47 | |
| 48 | For instance: |
| 49 | |
| 50 | ```java |
| 51 | @HotReload |
| 52 | @Sources("file:foo/bar/baz.properties") |
| 53 | interface MyConfig extends Config { |
| 54 | @DefaultValue("localhost") |
| 55 | String serverName(); |
| 56 | } |
| 57 | ``` |
| 58 | |
| 59 | You see in the above example we have specified the annotation `@HotReload` on |
| 60 | the interface level. |
| 61 | |
| 62 | The hot reload works only on filesystem URLs. This means that |
| 63 | you can make it work with those two types of URLs: |
| 64 | |
| 65 | - `file:path/to/your.properties` a filesystem backed URL. |
| 66 | - `jar:file:path/to/some.jar!/path/to/your.properties` a jar file in your |
| 67 | local filesystem that contains a properties files. |
| 68 | - `classpath:path/to/your.properties` a resource loaded from the classpath, |
| 69 | *if* the classpath resource is stored on filesystem (from inside a jar or |
| 70 | from inside a classpath folder). If the ClassLoader is loading the resource |
| 71 | from a remote url (for instance from a jar accessed via http protocol), |
| 72 | then it won't work. Almost always, the application loads classes and |
| 73 | resources from a filesystem backed classpath. So this should work *almost* |
| 74 | always. |
| 75 | |
| 76 | If you don't specify the `@Sources` annotation, then OWNER will try to load |
| 77 | the properties file from the classpath from a resource matching the same package and |
| 78 | class name of the *mapping interface*. |
| 79 | |
| 80 | The hot reload annotation instructs OWNER to monitor those resources for |
| 81 | changes and reload them when they change. |
| 82 | |
| 83 | Why only 'file:', 'jar:file' and 'classpath:' URLs? |
| 84 | --------------------------------------------------- |
| 85 | |
| 86 | Monitoring remote URLs, such as "http" or "ftp", will involve network |
| 87 | communication to download those files from remote servers frequently just for |
| 88 | checking if they are changed, and it is not convenient to implement the hot |
| 89 | reload doing frequent heavy operations like these. |
| 90 | You can still perform the reload programmatically, using the [Reloadable] |
| 91 | interface, for these cases. |
| 92 | |
| 93 | [Reloadable]: http://owner.aeonbits.org/apidocs/latest/org/aeonbits/owner/Reloadable.html |
| 94 | |
| 95 | While instead, monitoring the filesystem is not a big deal, also because |
| 96 | filesystems implement 'last modification date' that can be checked to detect |
| 97 | modifications without actually check the content of the file for changes. |
| 98 | |
| 99 | This is the reason why OWNER implements "hot reload" only on filesystem based |
| 100 | URLs. |
| 101 | |
| 102 | |
| 103 | The @HotReload annotation |
| 104 | ------------------------- |
| 105 | |
| 106 | The `@HotReload` annotation accepts 3 optional parameters. |
| 107 | |
| 108 | And is defined as: |
| 109 | |
| 110 | ```java |
| 111 | @interface HotReload { |
| 112 | long value() default 5; |
| 113 | TimeUnit unit() default SECONDS; |
| 114 | HotReloadType type() default SYNC; |
| 115 | } |
| 116 | |
| 117 | enum HotReloadType { |
| 118 | SYNC, ASYNC |
| 119 | } |
| 120 | ``` |
| 121 | |
| 122 | You can check the [latest javadocs] for further details. |
| 123 | |
| 124 | [latest javadocs]: http://owner.aeonbits.org/apidocs/latest/org/aeonbits/owner/Config.HotReload.html |
| 125 | |
| 126 | So you can specify also the interval for the hot reload, expressed by `value` |
| 127 | and `unit`, and you can also specify the type of hot reload that you need. |
| 128 | |
| 129 | Some examples: |
| 130 | |
| 131 | ```java |
| 132 | // Using the default values: |
| 133 | // will check for MyConfig.properties file changes in classpath |
| 134 | // with interval of 5 seconds. |
| 135 | // It will use SYNC hot reload. |
| 136 | @HotReload |
| 137 | interface MyConfig extends Config { ... } |
| 138 | |
| 139 | // Will check for file changes every 2 seconds. |
| 140 | // It will use SYNC hot reload. |
| 141 | @HotReload(2) |
| 142 | @Sources("file:foo/bar/baz.properties") |
| 143 | interface MyConfig extends Config { ... } |
| 144 | |
| 145 | // Will check for file changes every 500 millis. |
| 146 | // It will use SYNC hot reload. |
| 147 | @HotReload(value=500, unit = TimeUnit.MILLISECONDS) |
| 148 | @Sources("file:foo/bar/baz.properties") |
| 149 | interface MyConfig extends Config { ... } |
| 150 | |
| 151 | // Will use ASYNC reload type: will span a |
| 152 | // separate thread that will check for file |
| 153 | // changes every 5 seconds (default) |
| 154 | @HotReload(type=HotReloadType.ASYNC) |
| 155 | @Sources("file:foo/bar/baz.properties") |
| 156 | interface MyConfig extends Config { ... } |
| 157 | |
| 158 | // Will use ASYNC reload type and will check every 2 seconds. |
| 159 | @HotReload(value=2, type=HotReloadType.ASYNC) |
| 160 | @Sources("file:foo/bar/baz.properties") |
| 161 | interface MyConfig extends Config { ... } |
| 162 | ``` |
| 163 | |
| 164 | The difference between SYNC and ASYNC hot reload will be explained below. |
| 165 | |
| 166 | As explained before the [last modified] date of the file will be used to detect |
| 167 | changes on the files. |
| 168 | |
| 169 | [last modified]: http://docs.oracle.com/javase/7/docs/api/java/io/File.html#lastModified() |
| 170 | |
| 171 | <div class="note warning"> |
| 172 | <h5>Filesystems quirks</h5> |
| 173 | <p> |
| 174 | The date resolution vary from filesystem to filesystem. <br/> |
| 175 | For instance, for Ext3, ReiserFS and HSF+ the date resolution is of 1 second. <br/> |
| 176 | For FAT32 the date resolution for the last modified time is 2 seconds. <br/> |
| 177 | For Ext4 the date resolution is in nanoseconds. |
| 178 | </p> |
| 179 | </div> |
| 180 | |
| 181 | The synchronous hot reload |
| 182 | -------------------------- |
| 183 | |
| 184 | The synchronous hot reload works this way: every time you call a method on the |
| 185 | config object created by `ConfigFactory.create()` the configuration files will |
| 186 | be checked for modifications, then eventually reload the files. |
| 187 | |
| 188 | This means that, if you don't use the config object for long periods there will |
| 189 | be no checks on the filesystem, and consequently no reload will be performed. |
| 190 | |
| 191 | So, we can define this behavior a *lazy* hot reload, since it does that only |
| 192 | when needed, at the very last time. |
| 193 | |
| 194 | This is the default for the `@HotReload` annotation, but you can also specify |
| 195 | this type of hot reload explicitly with `type=SYNC`: |
| 196 | |
| 197 | ```java |
| 198 | @HotReload(type=HotReloadType.SYNC) |
| 199 | ``` |
| 200 | |
| 201 | |
| 202 | The asynchronous hot reload |
| 203 | --------------------------- |
| 204 | |
| 205 | The asynchronous hot reload works this way: it schedule a periodic task to be |
| 206 | executed on a separate thread on the specified interval, to check the files for |
| 207 | modification and eventually reload them. |
| 208 | |
| 209 | This means that, if you don't use the config object for long periods, the check |
| 210 | on the filesystem will be done in background anyway and eventually the reload |
| 211 | will be performed. |
| 212 | |
| 213 | To enable this behavior you need to specify `type=ASYNC` to the hot reload |
| 214 | annotation: |
| 215 | |
| 216 | ```java |
| 217 | @HotReload(type=HotReloadType.ASYNC) |
| 218 | ``` |
| 219 | |
| 220 | Intercepting reload events |
| 221 | -------------------------- |
| 222 | |
| 223 | Since reload can happen programmatically, and automatically synchronously and |
| 224 | asynchronously, it may be helpful for to have some notification mechanism to |
| 225 | intercept reload events. |
| 226 | |
| 227 | For this, please look at the [Reloadable] interface, that allows the user to |
| 228 | attach ReloadListeners to the config object. |
| 229 | |
| 230 | Hot reload example |
| 231 | ------------------ |
| 232 | |
| 233 | In the project's sources it is included a working example: |
| 234 | |
| 235 | ```java |
| 236 | |
| 237 | public class AutoReloadExample { |
| 238 | private static final String spec = |
| 239 | "file:target/test-resources/AutoReloadExample.properties"; |
| 240 | |
| 241 | private static File target; |
| 242 | |
| 243 | @Sources(spec) |
| 244 | @HotReload(1) |
| 245 | interface AutoReloadConfig extends Config, Reloadable { |
| 246 | @DefaultValue("5") |
| 247 | Integer someValue(); |
| 248 | } |
| 249 | |
| 250 | static { |
| 251 | try { |
| 252 | target = new File(new URL(spec).getFile()); |
| 253 | } catch (MalformedURLException e) { |
| 254 | e.printStackTrace(); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | public static void main(String[] args) |
| 259 | throws IOException, InterruptedException { |
| 260 | |
| 261 | save(target, new Properties() { { |
| 262 | setProperty("someValue", "10"); |
| 263 | }}); |
| 264 | |
| 265 | AutoReloadConfig cfg = |
| 266 | ConfigFactory.create(AutoReloadConfig.class); |
| 267 | |
| 268 | cfg.addReloadListener(new ReloadListener() { |
| 269 | public void reloadPerformed(ReloadEvent event) { |
| 270 | System.out.print( |
| 271 | "\rReload intercepted at " |
| 272 | + new Date() + " \n"); |
| 273 | } |
| 274 | }); |
| 275 | |
| 276 | System.out.println("You can change the file " |
| 277 | + target.getAbsolutePath() + |
| 278 | " and see the changes reflected below"); |
| 279 | |
| 280 | int someValue = 0; |
| 281 | while (someValue >= 0) { |
| 282 | someValue = cfg.someValue(); |
| 283 | System.out.print( |
| 284 | "\rsomeValue is: " + someValue + "\t\t\t\t"); |
| 285 | Thread.sleep(500); |
| 286 | } |
| 287 | |
| 288 | } |
| 289 | } |
| 290 | ``` |
| 291 | |
| 292 | To run this example, you need to follow these steps: |
| 293 | |
| 294 | ```bash |
| 295 | # after downloading the sources in the directory 'owner' |
| 296 | $ cd owner |
| 297 | $ mvn clean compile test-compile |
| 298 | $ java -classpath \ |
| 299 | target/classes/:target/test-classes/ \ |
| 300 | org.aeonbits.owner.examples.AutoReloadExample |
| 301 | ``` |
| 302 | |
| 303 | Then you can change the file indicated by the program to see the changes being |
| 304 | reflected and the reload event being intercepted. |