Debian Codegraph @

owner-site/site/docs/event-support.md

raw symbols SCIP
1 ---
2 layout: docs
3 title: Event support
4 prev_section: xml-support
5 next_section: singleton
6 permalink: /docs/event-support/
7 ---
8
9 The HotReload is a cool feature, however it would be nice to know which properties has changed so that you can
10 re-configure only the affected services.
11
12 This is why OWNER implements a feature rich event system that allows to know when a reload event occurs, when a
13 property is changed, and lets you to check if the new value is compliant to your application requirements and,
14 if not, you can rollback the property change or the entire change set.
15
16 Listening for Reloads
17 ---------------------
18
19 As explained in [Reload and Hot Reload]({{site.url}}/docs/reload/) section, OWNER supports programmatic reload and
20 automatic reload (when a configuration file is changed). In both cases a reload event is generated.
21
22 To get notified when a reload event occurs, your interface must extend `Reloadable` that exposes some methods to
23 register a `ReloadListener`.
24
25 There is also the possibility to implement a `TransactionalReloadListener` which does notify you before the reload
26 becomes effective and allows you to check what is changed and eventually to rollback the reload operation if the
27 changes you see are not compliant to your application's requirements.
28
29 ![reload-event]({{site.url}}/img/reload-event.png)
30
31 The `Reloadable` interfaces allows to attach and remove `ReloadListener` objects.
32
33 The `ReloadListener` interface defines a method called `reloadPerformed()` that receives a `ReloadEvent` which contains
34 the `oldProperties` (before the reload) and the `newProperties` (after the reload) plus a list of `PropertyChangeEvent`
35 for every property which is changed.
36
37 The `PropertyChangeEvent` contains the `propertyName`, the `oldValue` and the `newValue`.
38
39 When calling the method `Reloadable.addReloadListener()` it is possible to pass an instance of
40 `TransactionalReloadListener` which extends the `ReloadListener` adding the method `beforeReload()`.
41 This listener receives the notification just after the reload is triggered and before the changes are effective. In this
42 way it is possible for this listener to check what is changed and, if the changes are not welcome, it can trigger the
43 exception `RollbackBatchException` which means that all the changes in the reload will be discarded, and the current
44 values will be kept.
45 If the `RollbackBatchException` is thrown, the `ReloadListener.reloadPerformed()` will not be invoked, since the reload
46 has been aborted by the listener.
47
48 Example:
49
50 ```java
51
52 @Sources("Example.properties")
53 interface MyConfig extends Reloadable {
54 @DefaultValue("5")
55 Integer someInteger();
56
57 @DefaultValue("foobar")
58 String someString();
59
60 @DefaultValue("3.14")
61 Double someDouble();
62
63 String nullsByDefault();
64 }
65
66 MyConfig cfg = ConfigFactory.create(MyConfig.class);
67
68 final boolean[] reloadPerformed = new boolean[] {false};
69
70 cfg.addReloadListener(new TransactionalReloadListener() {
71
72 public void beforeReload(ReloadEvent event)
73 throws RollbackBatchException {
74
75 String notAllowedValue = "42";
76
77 String newSomeInteger = event.getNewProperties()
78 .getProperty("someInteger");
79
80 // 42 makes the reload to rollback completely!
81 if (notAllowedValue.equals(newSomeInteger))
82 throw new RollbackBatchException(
83 "42 is not allowed for property 'someInteger'");
84
85 }
86
87 public void reloadPerformed(ReloadEvent event) {
88 reloadPerformed[0] = true;
89 }
90
91 });
92
93 // we update the properties file in the filesystem
94 File target = new File("Example.properties");
95 save(target, new Properties() { {
96 setProperty("someInteger", "41");
97 setProperty("someString", "bazbar");
98 setProperty("someDouble", "2.718");
99 setProperty("nullsByDefault", "NotNullNow");
100 }});
101
102 cfg.reload();
103
104 // reload happened
105 assertTrue(reloadPerformed[0]);
106 assertEquals(new Integer(41), cfg.someInteger());
107 assertEquals("bazbar", cfg.someString());
108 assertEquals(new Double("2.718"), cfg.someDouble());
109 assertNotNull(cfg.nullsByDefault());
110
111 reloadPerformed[0] = false; // reset the flag
112
113 save(target, new Properties() { {
114 setProperty("someInteger", "42"); // not allowed!
115 setProperty("someString", "blahblah");
116 setProperty("someDouble", "1.234");
117 }});
118
119 cfg.reload();
120
121 // reload was rolled back since 42 is not allowed
122 assertFalse(reloadPerformed[0]);
123 assertEquals(new Integer(41), cfg.someInteger());
124 assertEquals("bazbar", cfg.someString());
125 assertEquals(new Double("2.718"), cfg.someDouble());
126 assertNotNull(cfg.nullsByDefault());
127
128 ```
129
130 Listening for PropertyChanges
131 -----------------------------
132
133 A `PropertyChangeEvent` happens every time the user changes a property using a method declared in the `Mutable`
134 interface, or when a [reload]({{site.url}}/docs/reload/) happens.
135
136 To get notified when a property change event occurs, your interface must extend `Mutable` that exposes some methods to
137 register a `PropertyChangeListener`.
138
139 There is also the possibility to implement a `TransactionalPropertyChangeListener` which does notify you before the
140 property change becomes effective and allows you to check what is changed and eventually to rollback the single
141 property change operation or the whole set of changes triggered by the source event.
142
143 ![propertychange-event]({{site.url}}/img/propertychange-event.png)
144
145
146 The `Mutable` interfaces allows to attach and remove `PropertyChangeListener` objects. It is possible to attach
147 a listener to a specific property name, so that the listener will be associated to a single property name:
148
149 ```java
150 public interface Mutable extends Config {
151 void addPropertyChangeListener(String propertyName, PropertyChangeListener listener);
152 void addPropertyChangeListener(PropertyChangeListener listener);
153 void removePropertyChangeListener(PropertyChangeListener listener);
154 // ...the rest of the interface is cut...
155 }
156 ```
157
158 The `Mutable` interface defines methods to change the internal values of the properties as well, all of them will
159 trigger a `PropertyChangeEvent` and notify the registered listeners:
160
161 ```java
162 public interface Mutable extends Config {
163 String setProperty(String key, String value);
164 String removeProperty(String key);
165 void clear();
166 void load(InputStream inStream) throws IOException;
167 void load(Reader reader) throws IOException;
168 // ...the rest of the interface is cut...
169 }
170 ```
171
172
173 The `PropertyChangeListener` interface comes from the `java.beans` package and defines a method called
174 `propertyChange()` that receives a `PropertyChangeEvent` which contains the `propertyName`, the `oldValue` and
175 the `newValue`.
176
177 When calling the method `Mutable.addPropertyChangeListener()` it is possible to pass an instance of
178 `TransactionalPropertyChangeListener` which extends the `PropertyChangeListener` adding the method
179 `beforePropertyChange()`.
180 This listener receives the notification just after the change is triggered but before the changes become effective.
181 In this way it is possible for this listener to check what is changed and, if the changes are not welcome, the listener
182 can trigger the exception `RollbackOperationException` or `RollbackBatchException`.
183
184 Throwing `RollbackOperationException` means that the listener wants to rollback the single property change, even though
185 the property change may have been triggered due to an event who involves many property changes, and only the single
186 property change needs to be rolled back.
187 Instead the `RollbackBatchException` means that all the changes triggered by the source event must be rolled back, and
188 the previous values must be kept.
189
190 If the any of the two `RollbackException` explained above is thrown, the `PropertyChangeListener.propertyChange()`
191 will not be invoked, since the change has been aborted by the listener.
192
193 Obviously also the `reload()` operation happenning during a hot reload, or when invoked via the `Reloadable` interface
194 triggers the PropertyChangeEvent.
195
196 Example:
197
198 ```java
199
200 @Sources("Example.properties")
201 interface MyConfig extends Reloadable {
202 @DefaultValue("5")
203 Integer someInteger();
204
205 @DefaultValue("foobar")
206 String someString();
207
208 @DefaultValue("3.14")
209 Double someDouble();
210
211 String nullsByDefault();
212 }
213
214 MyConfig cfg = ConfigFactory.create(MyConfig.class);
215
216 final boolean[] reloadPerformed = new boolean[] {false};
217
218 cfg.addPropertyChangeListener("someInteger",
219 new TransactionalPropertyChangeListener() {
220 public void beforePropertyChange(PropertyChangeEvent event)
221 throws RollbackOperationException, RollbackBatchException {
222 String notAllowedValue = "88";
223 String makesEverythingToRollback = "42";
224
225 String newSomeInteger = (String)event.getNewValue();
226 if (notAllowedValue.equals(newSomeInteger))
227 throw new RollbackOperationException(
228 "88 is not allowed for property 'someInteger', " +
229 "the single property someInteger is rolled back");
230
231 if (makesEverythingToRollback.equals(newSomeInteger))
232 throw new RollbackBatchException(
233 "42 is not allowed for property 'someInteger', " +
234 "the whole event is rolled back");
235
236 }
237
238 public void propertyChange(PropertyChangeEvent evt) {
239 reloadPerformed[0] = true;
240 }
241 });
242
243 save(target, new Properties() { {
244 setProperty("someInteger", "41");
245 setProperty("someString", "bazbar");
246 setProperty("someDouble", "2.718");
247 setProperty("nullsByDefault", "NotNullNow");
248 }});
249
250 cfg.reload();
251
252 assertTrue(reloadPerformed[0]);
253 assertEquals(new Integer(41), cfg.someInteger());
254 assertEquals("bazbar", cfg.someString());
255 assertEquals(new Double("2.718"), cfg.someDouble());
256 assertNotNull(cfg.nullsByDefault());
257
258 reloadPerformed[0] = false;
259
260 cfg.setProperty("someInteger", "55");
261 assertTrue(reloadPerformed[0]);
262 assertEquals(new Integer(55), cfg.someInteger());
263
264 reloadPerformed[0] = false;
265
266
267 cfg.setProperty("someInteger", "88");
268 // 88 is rolled back.
269 assertFalse(reloadPerformed[0]);
270 assertEquals(new Integer(55), cfg.someInteger());
271
272 reloadPerformed[0] = false;
273
274 save(target, new Properties() { {
275 setProperty("someInteger", "42");
276 setProperty("someString", "blahblah");
277 setProperty("someDouble", "1.234");
278 }});
279
280 cfg.reload();
281
282 assertFalse(reloadPerformed[0]);
283 assertEquals(new Integer(55), cfg.someInteger());
284 assertEquals("bazbar", cfg.someString());
285 assertEquals(new Double("2.718"), cfg.someDouble());
286 assertNotNull(cfg.nullsByDefault());
287
288
289 reloadPerformed[0] = false;
290
291 save(target, new Properties() { {
292 setProperty("someInteger", "88");
293 setProperty("someString", "this is not rolled back");
294 setProperty("someDouble", "1.2345");
295 }});
296
297 cfg.reload();
298
299 assertFalse(reloadPerformed[0]);
300 // only someInteger=88 is rolled back
301 assertEquals(new Integer(55), cfg.someInteger());
302 assertEquals("this is not rolled back", cfg.someString());
303 assertEquals(new Double("1.2345"), cfg.someDouble());
304 assertNull(cfg.nullsByDefault());
305
306 ```
307
308 Conclusions
309 -----------
310
311 With `ReloadListener` and `PropertyChangeListener` it is possible to get notified when your configuration changes.
312 This allows your application to take actions subsequently.
313
314 With `TransactionalReloadListener` and `TransactionalPropertyChangeListener` you can also check if the changes being
315 applied are coherent to your requirements and eventually rollback the single property change or the
316 complete set of changes, keeping things as they where before a reload.
317
318 This is a mechanism which allows for some basic validation.
319 A more simple and powerful validation mechanism is planned for the future releases, and the event mechanism will be
320 probably the backbone of this.