blob: 962d337c8c583427df39d9b04cd3e2eba451946d [file] [log] [blame]
<!--
/***************************************************************************************************************************
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
***************************************************************************************************************************/
-->
{8.0.0-new, 8.1.2-deprecated}
Listeners
<p>
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.
</p>
<p>
In addition to this support, an interface for defining event listeners for your microservice:
</p>
<ul class='javatree'>
<li class='jc'>{@link oaj.microservice.MicroserviceBuilder}
<ul>
<li class='jm'>{@link oaj.microservice.MicroserviceBuilder#listener(MicroserviceListener) listener(MicroserviceListener)}
</ul>
<li class='jic'>{@link oaj.microservice.MicroserviceListener}
<ul>
<li class='jm'>{@link oaj.microservice.MicroserviceListener#onStart(Microservice) onStart(Microservice)}
<li class='jm'>{@link oaj.microservice.MicroserviceListener#onStop(Microservice) onStop(Microservice)}
<li class='jm'>{@link oaj.microservice.MicroserviceListener#onConfigChange(Microservice,ConfigEvents) onConfigChange(Microservice,ConfigEvents)}
</ul>
<li class='jac'>{@link oaj.microservice.BasicMicroserviceListener}
</ul>
<p>
This listener API can be used for listening for and reacting to configuration changes on the file system.
</p>
<p class='bpcode w800'>
<jk>public class</jk> MyMicroserviceListener <jk>extends</jk> BasicMicroserviceListener {
<ja>@Override</ja> <jc>/* MicroserviceListener */</jc>
<jk>public void</jk> onConfigChange(Microservice microservice, ConfigEvents events) {
<jc>// Restart the microservice if anything was modified in one of our sections</jc>
<jk>if</jk> (events.isSectionChanged(<js>"MySection"</js>))
microservice.stop().start();
}
}
</p>
<p>
Note that the {@link oaj.microservice.Microservice#onConfigChange(ConfigEvents)} method can also be overridden
to react to configuration changes as well:
</p>
<p class='bpcode w800'>
<jk>public class</jk> MyMicroservice <jk>extends</jk> Microservice {
<ja>@Override</ja> <jc>/* MicroserviceListener */</jc>
<jk>public void</jk> onConfigChange(ConfigEvents events) {
<jc>// Restart the microservice if anything was modified in one of our sections</jc>
<jk>if</jk> (events.isSectionChanged(<js>"MySection"</js>))
<jk>this</jk>.stop().start();
}
}
</p>