concept akka - http in category akka

appears as: kka-http
Akka in Action

This is an excerpt from Manning's book Akka in Action.

The RequestTimeout trait is shown for completeness sake, which makes it possible for the RestApi to use the configured request timeout in akka-http.

In this chapter we’ll look at some examples of actors being used to integrate with other external systems. Today applications are increasingly complex, requiring connections to different information services and applications. It’s almost impossible to create a system that doesn’t either rely on information from or supply information to other systems. To be able to communicate with other systems, the two sides have to go through an agreed-upon interface. We’ll start with some enterprise integration patterns (EIPs). Next, we’ll describe how akka-camel (an Akka extension for Apache Camel, a project that simplifies integration over many transports) can help a system integrate with other external systems, especially in a request/response style. We’ll finish with an HTTP example using akka-http and detail the different approaches to integration with Akka.

12.3.2. Implementing a REST endpoint with akka-http

To give you a feeling for how akka-http can help you in implementing a REST interface, we’ll implement the same example endpoint from earlier, this time using akka-http. But keep in mind that this is only a small part of akka-http; there’s much more. For instance, we’ll look at examples of streaming data over HTTP in chapter 13.

We’ll start by creating the HTTP routes for the REST endpoint in an OrderService trait. The OrderService trait defines an abstract method that will return an ActorRef to the ProcessOrders. It’s a good practice to separate the route definitions from the usage of an actor, because this enables you to test the routes without starting the actor or by injecting a TestProbe, for instance. Figure 12.18 shows both the OrderService-Api class that will provide the necessary ExecutionContext and Timeout for using ask to request from the ProcessOrder, and the OrderService trait containing the routes. akka-http has its own test kit that enables you to test routes. Listing 12.12 shows how the OrderService will be tested.

Figure 12.18. Endpoint implementation using akka-http
Listing 13.18. Akka-http dependencies
"com.typesafe.akka" %% "akka-http-core"                    % version, "com.typesafe.akka" %% "akka-http-experimental"            % version, #1 "com.typesafe.akka" %% "akka-http-spray-json-experimental" % version, #2 #1 - Akka-http dependency #2 - Integration between akka-http and spray-json !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[]"} !@%STYLE%@!