Listeners
As mentioned previously, the lifecycle methods for the {@link oaj.microservice.Microservice} class are explicitly
defined as non-final so that they can be overridden by subclasses.
In addition to this support, an interface for defining event listeners for your microservice:
- {@link oaj.microservice.MicroserviceBuilder}
- {@link oaj.microservice.MicroserviceBuilder#listener(MicroserviceListener) listener(MicroserviceListener)}
- {@link oaj.microservice.MicroserviceListener}
- {@link oaj.microservice.MicroserviceListener#onStart(Microservice) onStart(Microservice)}
- {@link oaj.microservice.MicroserviceListener#onStop(Microservice) onStop(Microservice)}
- {@link oaj.microservice.MicroserviceListener#onConfigChange(Microservice,ConfigEvents) onConfigChange(Microservice,ConfigEvents)}
- {@link oaj.microservice.BasicMicroserviceListener}
This listener API can be used for listening for and reacting to configuration changes on the file system.
public class MyMicroserviceListener extends BasicMicroserviceListener {
@Override /* MicroserviceListener */
public void onConfigChange(Microservice microservice, ConfigEvents events) {
// Restart the microservice if anything was modified in one of our sections
if (events.isSectionChanged("MySection"))
microservice.stop().start();
}
}
Note that the {@link oaj.microservice.Microservice#onConfigChange(ConfigEvents)} method can also be overridden
to react to configuration changes as well:
public class MyMicroservice extends Microservice {
@Override /* MicroserviceListener */
public void onConfigChange(ConfigEvents events) {
// Restart the microservice if anything was modified in one of our sections
if (events.isSectionChanged("MySection"))
this.stop().start();
}
}