To work with Deepsy health service it is necessary to use DeepsyHealthManager located in com.gmv.its.deepsy.health for getting the initial HealthManager object.
HealthManager healthManager = DeepsyHealthManager.getInstance();
DeepsyHealthManager.getInstance has an optional parameter indicating the ip address of remote service. For instance:
HealthManager healthManager = DeepsyHealthManager.getInstance("192.168.1.10");
HealthManager is an AutoCloseable interface. An object that may hold resources (such as file or socket handles) until it is closed. The close() method of an AutoCloseable object is called automatically when exiting a try-with-resources block for which the object has been declared in the resource specification header. This construction ensures prompt release, avoiding resource exhaustion exceptions and errors that may otherwise occur. To obtain a Health object:
try (HealthManager healthManager = DeepsyHealthManager.getInstance("192.168.1.10")){
Health health = healthManager.getHealth();
} catch (HealthNotAvailableException e) {
e.printStackTrace();
} catch (HealthLowLevelException e) {
e.printStackTrace();
}
If this object is not inside a try with resources block, it is recommended to call to healthManager.close() in order to finalize properly;
To detect service availability the DeepsyHealthManager implements Manager interface which provides methods for getting availability and subscribing to availability events.
class MyManagerObserver implements ManagerObserver{
public void notify(ManagerConnectionEvent event) {
System.out.println(event.getManager().getName() +
" in "+
event.getManager().getIp() +
" "+
event.getAction().toString());
}
}
public class App {
public static void main(String[] args) {
ManagerObserver observer = new MyManagerObserver();
try (HealthManager healthManager = DeepsyHealthManager.getInstance()){
healthManager.subscribe(observer);
} catch (CannotSubscribeException e1) {
e1.printStackTrace();
}
}
}
Report new status to health service and retrieve list of pending status
package com.gmv.its.dpyjavaexamples.health;
import java.util.List;
public class HealthStatusListApp {
public static void main(String[] args) {
try (DeepsyHealthManager healthManager = DeepsyHealthManager.getInstance("172.22.198.103")) {
healthManager.reportNewStatus("PROJECT_ROUTE_NUMBER", "146",HealthStatusPriority.NORMAL);
healthManager.reportNewStatus("PROJECT_PASSENGER_QTY", "35",HealthStatusPriority.NORMAL);
System.out.println(
"Project status reported correctly to Health system");
Thread.sleep(2000);
List<HealthStatus> currentStatusList = healthManager.getCurrentStatusList();
System.out.println(
"Current status list is:");
for(HealthStatus currentStatus : currentStatusList) {
System.out.println(
"\tStatus " + currentStatus.toString());
}
List<HealthStatus> pendingStatusList = healthManager.getPendingStatusList();
System.out.println(
"Pending status to report are:");
for(HealthStatus pendingStatus : pendingStatusList) {
System.out.println(
"\tStatus " + pendingStatus.toString());
}
} catch (HealthNotAvailableException e) {
e.printStackTrace();
} catch (HealthLowLevelException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}