Engines

    • Jetty

    • Tomcat

    • CIO (Coroutine-based I/O)

    In addition to the engines mentioned above, Ktor provides a special engine type for testing application logic. You can learn more about it from Testing.

    Before using the desired engine, you need to add a corresponding dependency to your or pom.xml file:

    • ktor-server-netty
    • ktor-server-jetty
    • ktor-server-cio

    Below are examples of adding a dependency for Netty:

    Gradle (Kotlin)

    Maven

    Engines - 图2

    Configure Engines

    A Ktor server application can be run in two ways:

    • embeddedServer is a simple way to configure server parameters in code and quickly run an application.

    • provides more flexibility to configure a server. You can specify server parameters in an application.conf file and change a configuration without recompiling your application. Moreover, you can run your application from a command line and override the required server parameters by passing corresponding command-line arguments.

    The embeddedServer function accepts an engine factory used to create an engine of a specific type. In the example below, we pass the factory to run a server with the Netty engine and listen on the 8000 port:

    EngineMain represents an engine for running a server. You can use the following engines:

    • io.ktor.server.jetty.EngineMain
    • io.ktor.server.cio.EngineMain

    The EngineMain.main function is used to start a server with the selected engine and can accept command-line server parameters. In the example below, we start a server from the application’s function:

    If you need to start a server using a build system task, you need to configure the required EngineMain as the main class:

    Gradle (Groovy)

    Gradle (Kotlin)

    Maven