Skip to main content

Listeners

As mentioned previously, the lifecycle methods for the 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:

Microservice.Builderlistener(MicroserviceListener)MicroserviceListeneronStart(Microservice) onStop(Microservice) onConfigChange(Microservice,ConfigEvents)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 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();
}
}