Skip to content

Commit b2cb6d1

Browse files
authored
Document steps to ensure application terminates properly when batch mode is enabled (#763)
1 parent 54901a1 commit b2cb6d1

File tree

2 files changed

+43
-16
lines changed

2 files changed

+43
-16
lines changed

MANUAL.md

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,33 @@ influxDB.query(new Query("CREATE RETENTION POLICY " + retentionPolicyName
1717
+ " ON " + databaseName + " DURATION 1d REPLICATION 1 DEFAULT"));
1818
influxDB.setRetentionPolicy(retentionPolicyName); // (3)
1919

20-
influxDB.enableBatch(BatchOptions.DEFAULTS); // (4)
21-
22-
influxDB.write(Point.measurement("h2o_feet") // (5)
20+
influxDB.enableBatch(
21+
BatchOptions.DEFAULTS
22+
.threadFactory(runnable -> {
23+
Thread thread = new Thread(runnable);
24+
thread.setDaemon(true);
25+
return thread;
26+
})
27+
); // (4)
28+
29+
Runtime.getRuntime().addShutdownHook(new Thread(influxDB::close)); // (5)
30+
31+
influxDB.write(Point.measurement("h2o_feet") // (6)
2332
.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS)
2433
.tag("location", "santa_monica")
2534
.addField("level description", "below 3 feet")
2635
.addField("water_level", 2.064d)
2736
.build());
2837

29-
influxDB.write(Point.measurement("h2o_feet") // (5)
38+
influxDB.write(Point.measurement("h2o_feet") // (6)
3039
.tag("location", "coyote_creek")
3140
.addField("level description", "between 6 and 9 feet")
3241
.addField("water_level", 8.12d)
33-
.build()); // (6)
42+
.build());
3443

35-
Thread.sleep(5_000L); // (7)
44+
Thread.sleep(5_000L);
3645

37-
QueryResult queryResult = influxDB.query(new Query("SELECT * FROM h2o_feet"));
46+
QueryResult queryResult = influxDB.query(new Query("SELECT * FROM h2o_feet")); // (7)
3847

3948
System.out.println(queryResult);
4049
// It will print something like:
@@ -44,8 +53,6 @@ System.out.println(queryResult);
4453
// [2020-03-22T20:50:12.929Z, below 3 feet, santa_monica, 2.064],
4554
// [2020-03-22T20:50:12.929Z, between 6 and 9 feet, coyote_creek, 8.12]
4655
// ]]], error=null]], error=null]
47-
48-
influxDB.close(); // (8)
4956
```
5057

5158
### Connecting to InfluxDB
@@ -137,13 +144,26 @@ With batching enabled the client provides two strategies how to deal with errors
137144
When new data points are written before the previous (failed) points are successfully written, those are queued inside the client and wait until older data points are successfully written.
138145
Size of this queue is limited and configured by `BatchOptions.bufferLimit` property. When the limit is reached, the oldest points in the queue are dropped. 'Retry on error' strategy is used when individual write batch size defined by `BatchOptions.actions` is lower than `BatchOptions.bufferLimit`.
139146

140-
Note:
147+
#### Ensure application exit when batching is enabled
148+
`BatchOptions.DEFAULTS` creates a non-daemon thread pool which prevents the JVM from initiating shutdown in the case of
149+
exceptions or successful completion of the main thread. This will prevent shutdown hooks (many frameworks and plain JVM
150+
applications use these to close/ cleanup resources) from running, preventing graceful termination of the application.
151+
152+
Thus, configuring batch options with a daemon thread pool will solve this issue and will for example ensure that the registered
153+
(5) shutdown hook is run to close the `InfluxDB` client properly (flushing and closing of resources will happen).
154+
155+
### Close InfluxDB Client on JVM Termination
156+
(5) In order to ensure that in-flight points are flushed and resources are released properly, it is essential to call
157+
`influxDB.close()` the client when it is no longer required.
158+
159+
Registering a shutdown hook is a good way to ensure that this is done on application termination regardless of exceptions
160+
that are thrown in the main thread of the code. Note that if you are using a framework, do check the documentation for its
161+
way of configuring shutdown lifecycle hooks or if it might already be calling `close` automatically.
141162

142-
* Batching functionality creates an internal thread pool that needs to be shutdown explicitly as part of a graceful application shutdown or the application will terminate properly. To do so, call `influxDB.close()`.
143163

144164
### Writing to InfluxDB
145165

146-
(5) ...
166+
(6) ...
147167

148168
`----8<----BEGIN DRAFT----8<----`
149169

README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,17 @@ influxDB.query(new Query("CREATE RETENTION POLICY " + retentionPolicyName
6666
influxDB.setRetentionPolicy(retentionPolicyName);
6767

6868
// Enable batch writes to get better performance.
69-
influxDB.enableBatch(BatchOptions.DEFAULTS);
69+
influxDB.enableBatch(
70+
BatchOptions.DEFAULTS
71+
.threadFactory(runnable -> {
72+
Thread thread = new Thread(runnable);
73+
thread.setDaemon(true);
74+
return thread;
75+
})
76+
);
77+
78+
// Close it if your application is terminating or you are not using it anymore.
79+
Runtime.getRuntime().addShutdownHook(new Thread(influxDB::close));
7080

7181
// Write points to InfluxDB.
7282
influxDB.write(Point.measurement("h2o_feet")
@@ -100,9 +110,6 @@ System.out.println(queryResult);
100110
// [2020-03-22T20:50:12.929Z, below 3 feet, santa_monica, 2.064],
101111
// [2020-03-22T20:50:12.929Z, between 6 and 9 feet, coyote_creek, 8.12]
102112
// ]]], error=null]], error=null]
103-
104-
// Close it if your application is terminating or you are not using it anymore.
105-
influxDB.close();
106113
```
107114

108115
## Contribute

0 commit comments

Comments
 (0)