{title:'Extending JettyMicroservice', created:'8.0.0'}

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 com.foo.MyJettyMicroservice.

| /** | * Sample subclass of a JettyMicroservice that provides customized behavior. | */ | public class MyJettyMicroservice extends JettyMicroservice { | | public static void main(String[] args) throws Exception { | JettyMicroservice.Builder builder = JettyMicroservice. create(); | new MyJettyMicroservice(builder).start().join(); | } | | public MyJettyMicroservice(JettyMicroservice.Builder builder) throws Exception { | super(builder); | } | | // Customized code here.

Optionally, you can extend the {@link oaj.microservice.jetty.JettyMicroservice.Builder} class as well:

| public class MyJettyMicroservice.Builder extends JettyMicroservice.Builder { | | int extraField; | | /** | * Constructor (required). | */ | protected MyJettyMicroservice.Builder() {} | | /** | * Copy constructor (required). | * | * @param copyFrom The builder to copy settings from. | */ | protected MyJettyMicroservice.Builder(MyJettyMicroservice.Builder copyFrom) { | super(copyFrom); | } | | // Additional setters | | public MyJettyMicroservice.Builder extraField(int extraField) { | this. extraField = extraField; | return this; | } | | // Overridden methods | | @Override /* JettyMicroservice.Builder */ | public MyJettyMicroservice.Builder copy() { | return new MyJettyMicroservice.Builder(this); | } | | @Override /* JettyMicroservice.Builder */ | public MyJettyMicroservice.Builder ports(int...ports) { | super.ports(ports); | return this; | } | | @Override /* JettyMicroservice.Builder */ | public MyJettyMicroservice.Builder servlet(Class<? extends RestServlet> cls) throws InstantiationException, IllegalAccessException { | super.servlet(cls); | return this; | } | | ... | }