withMaterializer
Description
Allows running an inner route using an alternative Materializer in place of the default one.
The materializer can be extracted in an inner route using extractMaterializer directly, or used by directives which internally extract the materializer without surfacing this fact in the API (e.g. responding with a Chunked entity).
Example
- Scala
-
val special = ActorMaterializer(namePrefix = Some("special")) def sample() = path("sample") { extractMaterializer { mat => complete { // explicitly use the materializer: Source.single(s"Materialized by ${mat.##}!") .runWith(Sink.head)(mat) } } } val route = pathPrefix("special") { withMaterializer(special) { sample() // `special` materializer will be used } } ~ sample() // default materializer will be used // tests: Get("/sample") ~> route ~> check { responseAs[String] shouldEqual s"Materialized by ${materializer.##}!" } Get("/special/sample") ~> route ~> check { responseAs[String] shouldEqual s"Materialized by ${special.##}!" }
- Java