blob: cd3594ade16c5ed5b80176f46945a90a72c6040e [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}
Lifecycle Methods
<p>
The lifecycle methods of the {@link oaj.microservice.Microservice} class consists of the following:
</p>
<ul class='javatree'>
<li class='jc'>{@link oaj.microservice.Microservice}
<ul>
<li class='jm'>{@link oaj.microservice.Microservice#start() start()}
<li class='jm'>{@link oaj.microservice.Microservice#startConsole() startConsole()}
<li class='jm'>{@link oaj.microservice.Microservice#join() join()}
<li class='jm'>{@link oaj.microservice.Microservice#stop() stop()}
<li class='jm'>{@link oaj.microservice.Microservice#stopConsole() stopConsole()}
<li class='jm'>{@link oaj.microservice.Microservice#exit() exit()}
<li class='jm'>{@link oaj.microservice.Microservice#kill() kill()}
</ul>
</ul>
<p>
A typical implementation of an app with lifecycle methods might look like the following:
</p>
<p class='bpcode w800'>
<jk>public class</jk> App {
<jk>private static final</jk> Microservice <jsf>MICROSERVICE</jsf>;
<jk>public static void</jk> main(String[] args) {
<jsf>MICROSERVICE</jsf> = Microservice
.<jsm>create</jsm>() <jc>// Create builder.</jc>
.args(args) <jc>// Pass in args.</jc>
.build() <jc>// Create microservice.</jc>
.start() <jc>// Start microservice.</jc>
.startConsole() <jc>// Start console.</jc>
.join() <jc>// Join thread.</jc>
;
}
<jk>public static void</jk> restart() {
<jsf>MICROSERVICE</jsf>.stop().start();
}
<jk>public static void</jk> exit() {
<jsf>MICROSERVICE</jsf>.exit();
}
}
</p>
<p>
If your application consists of a single microservice, you can use the {@link oaj.microservice.Microservice#getInstance()} method
from anywhere in your code:
</p>
<p class='bpcode w800'>
<jk>public class</jk> App {
<jk>public static void</jk> main(String[] args) {
Microservice
.<jsm>create</jsm>() <jc>// Create builder.</jc>
.args(args) <jc>// Pass in args.</jc>
.build() <jc>// Create microservice.</jc>
.start() <jc>// Start microservice.</jc>
.startConsole() <jc>// Start console.</jc>
.join() <jc>// Join thread.</jc>
;
}
<jk>public static void</jk> restart() {
Microservice.<jsm>getInstance</jsm>().stop().start();
}
<jk>public static void</jk> exit() {
Microservice.<jsm>getInstance</jsm>().exit();
}
}
</p>
<p>
The {@link oaj.microservice.Microservice#startConsole()} and {@link oaj.microservice.Microservice#stopConsole()} control
the lifecycle of the console commands.
Typically you'll want to control these separately from the app so that you can easily restart your application
from the console without affecting the console itself.
</p>
<p>
The lifecycle methods on the {@link oaj.microservice.Microservice} class are purposely left non-final so that
subclasses can override them to provide customized behavior.
</p>