Workers

Workers are the reactive event-loops that handle the service business logic and consist of an Initializer and Request Handler. Initializers are created for each worker and provide a place to share resources among all connections handled by the worker. It also provides access for updating data for the request handler and clients. Actor messages can then be broadcast to all workers using the initializerBroadcast method on ServerRef.

case class NameChange(name: String)

val serverRef = HttpServer.start("example-server", 9000) { initContext =>
  new Initializer(initContext) {

    var currentName = "Jones"

    override def receive: Receive = {
      case NameChange(name) => currentName = name
    }

    override def onConnect: RequestHandlerFactory =
      serverContext =>
        new RequestHandler(serverContext) {
          override def handle: PartialHandler[Http] = {
            case request @ Get on Root => Callback.successful(request.ok(s"My name is $currentName"))
          }
      }
  }
}

serverRef.initializerBroadcast(NameChange("Smith"))
The source code for this page can be found here.