KtorServletService

class KtorServletService(port: Int, servletGroup: HttpServletGroup, initKtor: Application.() -> Unit = {}, host: String? = null, filters: List<Filter> = [], initBlock: KtorServletService.() -> Unit = {}) : GenericIdleService

A Guava GenericIdleService that runs an embedded Ktor CIO server to host servlets from an HttpServletGroup.

Each servlet in the group is registered as a Ktor route. The service manages the Ktor server lifecycle, starting it on startUp and stopping it on shutDown.

The servlets are served without authentication unless filters add it: each servlet is wrapped so that its requests pass through the filters in list order first, as in a servlet container, and a filter can answer a request itself, for example with a 401. A request whose filter throws gets the same 500, with no body, as one whose servlet throws. The filters are initialized once, when the first servlet is, and destroyed once, after the last; a filter that fails to initialize fails the start. They see only requests for a servlet's path: a request for any other path, or for a route that initKtor adds, does not pass through them. Starting a server that binds every interface (a host such as "0.0.0.0" or "::") logs a WARN.

Used by GenericKtorService for serving administrative endpoints (ping, version, health check, thread dump).

Parameters

port

The HTTP port for the Ktor server.

servletGroup

The HttpServletGroup containing servlets to register as routes.

initKtor

An optional Ktor Application configuration block applied before routing.

host

The interface to bind to. The default, null, binds 127.0.0.1, so only local clients can connect; use "0.0.0.0" to accept connections on every interface.

filters

The servlet filters every request for a servlet passes through, in order, before it reaches it.

initBlock

An optional initialization block invoked after the service listener is registered.

Constructors

Link copied to clipboard
constructor(port: Int, servletGroup: HttpServletGroup, initKtor: Application.() -> Unit = {}, host: String? = null, filters: List<Filter> = [], initBlock: KtorServletService.() -> Unit = {})

Properties

Link copied to clipboard

Functions

Link copied to clipboard
fun addListener(listener: Service.Listener, executor: Executor)
Link copied to clipboard
fun awaitRunning(timeout: Duration)
fun awaitRunning(timeout: Long, unit: TimeUnit)
Link copied to clipboard
fun awaitTerminated(timeout: Long, unit: TimeUnit)
Link copied to clipboard
Link copied to clipboard
fun startAsync(): Service
Link copied to clipboard
fun startSync(timeout: Duration)
fun startSync(timeout: Duration)
Link copied to clipboard
fun state(): Service.State
Link copied to clipboard
fun stopAsync(): Service
Link copied to clipboard
fun stopSync(timeout: Duration)
fun stopSync(timeout: Duration)
Link copied to clipboard
open override fun toString(): String