blob: 9878b6fd2111c2152854c2488086e4d999e590cb [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}
Extending JettyMicroservice
<p>
This example shows how the {@link oaj.microservice.jetty.JettyMicroservice} class
can be extended to implement lifecycle listener methods or override existing methods.
We'll create a new class <l>com.foo.MyJettyMicroservice</l>.
</p>
<p class='bpcode w800'>
<jd>/**
* Sample subclass of a JettyMicroservice that provides customized behavior.
*/</jd>
<jk>public class</jk> MyJettyMicroservice <jk>extends</jk> JettyMicroservice {
<jk>public static void</jk> main(String[] args) <jk>throws</jk> Exception {
JettyMicroserviceBuilder builder = JettyMicroservice.<jsm>create</jsm>();
<jk>new</jk> MyJettyMicroservice(builder).start().join();
}
<jk>public</jk> MyJettyMicroservice(JettyMicroserviceBuilder builder) <jk>throws</jk> Exception {
<jk>super</jk>(builder);
}
<jc>// Customized code here.</jc>
</p>
<p>
Optionally, you can extend the {@link oaj.microservice.jetty.JettyMicroserviceBuilder} class as well:
</p>
<p class='bpcode w800'>
<jk>public class</jk> MyJettyMicroserviceBuilder <jk>extends</jk> JettyMicroserviceBuilder {
<jk>int</jk> <jf>extraField</jf>;
<jd>/**
* Constructor (required).
*/</jd>
<jk>protected</jk> MyJettyMicroserviceBuilder() {}
<jd>/**
* Copy constructor (required).
*
* <ja>@param</ja> copyFrom The builder to copy settings from.
*/</jd>
<jk>protected</jk> MyJettyMicroserviceBuilder(MyJettyMicroserviceBuilder copyFrom) {
<jk>super</jk>(copyFrom);
}
<jc>// Additional setters</jc>
<jk>public</jk> MyJettyMicroserviceBuilder extraField(<jk>int</jk> extraField) {
<jk>this</jk>.<jf>extraField</jf> = extraField;
<jk>return this</jk>;
}
<jc>// Overridden methods</jc>
<ja>@Override</ja> <jc>/* JettyMicroserviceBuilder */</jc>
<jk>public</jk> MyJettyMicroserviceBuilder copy() {
<jk>return new</jk> MyJettyMicroserviceBuilder(<jk>this</jk>);
}
<ja>@Override</ja> <jc>/* JettyMicroserviceBuilder */</jc>
<jk>public</jk> MyJettyMicroserviceBuilder ports(<jk>int</jk>...ports) {
<jk>super</jk>.ports(ports);
<jk>return this</jk>;
}
<ja>@Override</ja> <jc>/* JettyMicroserviceBuilder */</jc>
<jk>public</jk> MyJettyMicroserviceBuilder servlet(Class&lt;? <jk>extends</jk> RestServlet&gt; c) <jk>throws</jk> InstantiationException, IllegalAccessException {
<jk>super</jk>.servlet(c);
<jk>return this</jk>;
}
...
}
</p>