extractMaterializer
Description
Extracts the Materializer
Materializer
from the RequestContext
RequestContext
, which can be useful when you want to run an Apache Pekko Stream directly in your route.
See also withMaterializer to see how to customise the used materializer for specific inner routes.
Example
- Scala
-
source
val route = path("sample") { extractMaterializer { materializer => complete { // explicitly use the `materializer`: Source.single(s"Materialized by ${SystemMaterializer(system).materializer.##}!") .runWith(Sink.head)(materializer) } } } // default materializer will be used // tests: Get("/sample") ~> route ~> check { responseAs[String] shouldEqual s"Materialized by ${SystemMaterializer(system).materializer.##}!" }
- Java
-
source
import static org.apache.pekko.http.javadsl.server.Directives.extractMaterializer; final Route route = path( "sample", () -> extractMaterializer( mat -> onSuccess( () -> // explicitly use the materializer: Source.single("Materialized by " + mat.hashCode() + "!") .runWith(Sink.head(), mat), Directives::complete))); // default materializer will be used testRoute(route) .run(HttpRequest.GET("/sample")) .assertEntity("Materialized by " + materializer().hashCode() + "!");
1.1.0