I have this code:
static method(){ // also other class called this method
for (Map.Entry<String, DataSource> dataSource :dbhealth.entrySet()) {
Connection connection = dataSource.getValue().getConnection()
if (!connection.isValid(timeout)) {
log("bd connection is not valid for"+, dataSource.getKey());
}
}}
Here, dbhealth
is a Map<String, DataSource>
which stores health information. How can I get the MeterRegistry
to alert the user if the connection has timed out?
I considered creating a new method to put in the constructor like so:
public xxx(xx,xx, MeterRegistry meterRegistry){
for (Map.Entry<String, DataSource> dataSource :dbhealth.entrySet()) {
Connection connection = dataSource.getValue().getConnection()
boolean connected = connection.isValid(timeout)
if(meterRegistry!=null){
Gauge.builder(C.Metrics.DS_HEALTH, () -> (connected) ? 1 : 0)
.register(meterRegistry);
}
}
}
But this looks strange, and seems to duplicate code. Is there a more direct way to solve the problem?