concept monitor in category java

This is an excerpt from Manning's book The Java Module System.
monitor—Triggers data collection and pipes the data through statistics into persistence; implements the REST API with spark
Upon requests,
MonitorServer
, which exposes the REST API, asks the monitor to provide the statistical data—either from memory or from persistence—and then extracts the requested bits and returns them.Listing 2.5
MonitorServer
classpublic class MonitorServer { private final Supplier<Statistics> statistics; public MonitorServer(Supplier<Statistics> statistics) { this.statistics = statistics; } // [...] private Statistics getStatistics() { return statistics.get(); } // [...] }An interesting detail to note is that although
MonitorServer
callsMonitor
, it doesn’t depend on it. That’s becauseMonitorServer
doesn’t get a reference to a monitor but rather a supplier for the data that forwards calls to the monitor. The reason is pretty simple:Monitor
orchestrates the entire application, which makes it a class with a lot going on inside. I didn’t want to couple the REST API to such a heavyweight object just to call a single getter. Before Java 8, I might have created a dedicated interface to get the statistics and makeMonitor
implement it; but since Java 8, lambda expressions and the existing functional interfaces make ad hoc decoupling much easier.
Figure 10.6 Service binding is part of module resolution: Once a module is resolved (like monitor or java.base), its
uses
directives are analyzed, and all modules that provide matching services (alpha and beta as well as charsets and localedata) are added to the module graph.![]()