Create a custom ApplicationEngine

    Ktor includes several artifacts and engines:

    • For the server: , Jetty, Tomcat, CIO, TestEngine
    • For the client: ApacheEngine, JettyHttp2Engine, CIOEngine, TestHttpClientEngine

    A simplified version of the looks like this:

    Each implementation of the ApplicationEngineFactory along with a subtyped ApplicationEngine.Configuration define the publicly exposed APIs for each engine.

    The ApplicationEngineFactory.create instantiates the correct subtyped ApplicationEngine.Configuration and calls the provided configure: TConfiguration.() -> Unit lambda that should mutate the configuration object. It also constructs an implementation of the ApplicationEngine, most likely a subtype of BaseApplicationEngine.

    The interface ApplicationEngine with an abstract implementation of starts and stops the application.It holds the ApplicationEngineEnvironment as well as the constructed configuration of the application.

    This class has two methods:

    • The start method: connects to the ApplicationEngineEnvironment.connectors (from the environment), starts the environment,and starts and configures the engine to trigger execution of the application pipeline for each HTTP request with an ApplicationCall.
    • The stop method: stops the engine and the environment, and unregisters all items registered by the start method.

    For example: