Spray Akka Json Unmarshalling - scala

I've a problem about unmarshalling objects to Json via using spray - akka.
When i'd like to use actors that returns Future[List[Person]] , it doesn't work.
If i use dao object directly, it works.
Here are my codes:
PersonDao.scala
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
case class Person(id: Int, name: String, surname: String)
object PersonDao {
def getAll: Future[List[Person]] = Future {
List[Person](Person(1, "Bilal", "Alp"), Person(2, "Ahmet", "Alp"))
}
}
EntityServiceActor.scala
import akka.actor.Actor
import com.bilalalp.akkakafka.model.PersonDao
import com.bilalalp.akkakafka.service.ServiceOperation.FIND_ALL
object ServiceOperation {
case object FIND_ALL
}
class EntityServiceActor extends Actor {
override def receive: Receive = {
case FIND_ALL => PersonDao.getAll
}
}
ServerSupervisor.scala
import akka.actor.{Actor, ActorRefFactory}
import com.bilalalp.akkakafka.webservice.TaskWebService
import spray.routing.RejectionHandler.Default
class ServerSupervisor extends Actor with PersonWebService {
implicit val system = context.system
override def receive: Receive = runRoute(entityServiceRoutes)
override implicit def actorRefFactory: ActorRefFactory = context
}
WebServiceTrait.scala
import akka.util.Timeout
import spray.routing.HttpService
import scala.concurrent.duration._
import scala.language.postfixOps
import org.json4s.NoTypeHints
import org.json4s.native.Serialization._
trait WebServiceTrait extends HttpService {
implicit def executionContext = actorRefFactory.dispatcher
implicit val json4sFormats = formats(NoTypeHints)
implicit val timeout = Timeout(120 seconds)
}
PersonWebService.scala
trait PersonWebService extends WebServiceTrait with Json4sSupport {
val json3sFormats = DefaultFormats
val entityServiceWorker = actorRefFactory.actorOf(Props[EntityServiceActor], "entityServiceActor")
val entityServiceRoutes = {
pathPrefix("person") {
pathEndOrSingleSlash {
get {
ctx => ctx.complete((entityServiceWorker ? FIND_ALL).mapTo[Person])
}
}
}
}
}
Application.scala
import akka.actor.{ActorRef, ActorSystem, Props}
import akka.io.IO
import com.bilalalp.akkakafka.server.ServerSupervisor
import spray.can.Http
object Application extends App {
implicit val system = ActorSystem("actorSystem")
val mainHandler: ActorRef = system.actorOf(Props[ServerSupervisor])
IO(Http)! Http.Bind(mainHandler, interface = Configuration.appInterface, port = Configuration.appPort)
}
When i run this code, It gives nothing and waits for a while.
After waiting browser gives this message:
The server was not able to produce a timely response to your request.
And console output is
[ERROR] [11/22/2015 21:15:24.109]
[actorSystem-akka.actor.default-dispatcher-7]
[akka.actor.ActorSystemImpl(actorSystem)] Error during processing of
request HttpRequest(GET,http://localhost:3001/person/,List(Host:
localhost:3001, Connection: keep-alive, Cache-C ontrol: no-cache,
Pragma: no-cache, User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64)
AppleWebKit/537.36 (KHTML, like Gecko) Maxthon/4.4.6.1000
Chrome/30.0.1599.101 Safari/537.36, DNT: 1, Accept-Encoding: gzip,
deflate, Accept-Language: tr-TR),Empty,HTTP/1.1)
akka.pattern.AskTimeoutException: Ask timed out on
[Actor[akka://actorSystem/user/$a/entityServiceActor#-1810673919]]
after [120000 ms]. Sender[null] sent message of type
"com.bilalalp.akkakafka.service.ServiceOperation$FIND_ALL$".
at akka.pattern.PromiseActorRef$$anonfun$1.apply$mcV$sp(AskSupport.scala:415)
at akka.actor.Scheduler$$anon$7.run(Scheduler.scala:132)
at scala.concurrent.Future$InternalCallbackExecutor$.unbatchedExecute(Future.scala:599)
at scala.concurrent.BatchingExecutor$class.execute(BatchingExecutor.scala:109)
at scala.concurrent.Future$InternalCallbackExecutor$.execute(Future.scala:597)
If i change PersonWebService.scala to this :
trait PersonWebService extends WebServiceTrait with Json4sSupport {
val json3sFormats = DefaultFormats
val entityServiceWorker = actorRefFactory.actorOf(Props[EntityServiceActor], "entityServiceActor")
val entityServiceRoutes = {
pathPrefix("person") {
pathEndOrSingleSlash {
get (
// ctx => ctx.complete((entityServiceWorker ? FIND_ALL).mapTo[Person])
ctx => ctx.complete(PersonDao getAll)
)
}
}
}
}
It works and output is :
[{"id":1,"name":"Bilal","surname":"Alp"},{"id":2,"name":"Ahmet","surname":"Alp"}]
I'd like to use actors in spray routes. I don't know whether it is a bad practice or not because i'm newbie in akka and spray.
How can i solve this? Any ideas?
Thank you.

First of all, You can type (PersonWebService.scala):
pathEndOrSingleSlash {
get {
complete {
(entityServiceWorker ? FindAll).mapTo[List[Person]]
}
}
And as #Timothy Kim said You need to send back results using "sender ! getAll.onComplete
As I an see getAll returns Future, so in my opinion best would be to resolve it in EntityServiceActor.scala:
// import the pipe pattern (see pipeTo below):
import akka.pattern.pipe
import context.dispatcher
override def receive: Receive = {
case FindAll =>
PersonDao.getAll()
.recover({ case err => List() /* could log error here */ })
.pipeTo(sender()) // do this instead of onComplete, it's safer
in this simple case getAll Future is resolved, if everything is ok, service will get list of persons, otherwise List will be empty.
Oh and another thing PersonWebService.scala should have .mapTo[List[Person]]

You need to send a result back to sender:
case FIND_ALL =>
PersonDao.getAll.pipeTo(sender())

Related

Stuck in infinite loop with simple akka-http route test with actors

I have a simple example where I have a route that invokes an actor, however it seems to get stuck in an infinite loop and the http response never comes. I am using akka-actor version 2.6.15 and akka-http version 10.2.4. Here is the sample code, any help is appreciated.
package test
import akka.actor.{Actor, ActorRef, Props}
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.{Route, _}
import akka.http.scaladsl.testkit.{RouteTestTimeout, ScalatestRouteTest}
import akka.pattern.ask
import akka.util.Timeout
import org.scalatest.Matchers
import org.scalatest.wordspec.AnyWordSpec
import scala.concurrent.duration.DurationInt
case class TestMessage()
class TestActor extends Actor {
def receive: Receive = {
case _ => "response"
}
}
class AkkaHttpTest extends AnyWordSpec with Matchers with ScalatestRouteTest {
val testActor: ActorRef = system.actorOf(Props(new TestActor()), name = "TestActor")
implicit val timeout: Timeout = 15.seconds
implicit val defaultTimeout = RouteTestTimeout(15.seconds)
val route: Route = {
get {
pathSingleSlash {
complete((testActor ? TestMessage()).mapTo[String])
}
}
}
"Test" should {
"Return text" in {
Get() ~> route ~> check {
println(responseAs[String])
}
}
}
}
To reply to a message in Akka, you have to explicitly send the reply.
In your example:
def receive: Receive = {
case _ =>
sender ! "response"
}

Get the chosen actor in a RoundRobinPool?

If I had a RoundRobinPool like this
val actorPoolRef = AkkaConfig.actorSystem.actorOf(RoundRobinPool(100).props(Props[MyService]))
and a handler
def requestHandler(request: HttpRequest): Future[HttpResponse] = {
val promise = Promise[HttpResponse]()
promise.completeWith(actorPoolRef ? request)
promise.future
}
Is there any way I can
get the exact actor reference from the scope of def requestHandler, or
send a follow-up message to the same actor that just handled the request
you can do by using the akka.pattern.ask to request the actor reference from a RoundRobinPool of actors, returning the self.path as an Option and wrapping the response as Option[ActorPath]. To clarify what I am saying I build this simple proof of concept:
import akka.actor.{Actor, ActorLogging, ActorPath, ActorSystem, Props}
import akka.http.scaladsl.Http
import akka.http.scaladsl.model._
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route
import akka.pattern.ask
import akka.routing.RoundRobinPool
import akka.util.Timeout
import scala.concurrent.Future
import scala.concurrent.duration._
object BasicRoundRobinHttpServer {
def main(args: Array[String]): Unit = {
run()
}
def run() = {
implicit val system = ActorSystem("BasicRoundRobinHttpServer")
import system.dispatcher
implicit val timeout = Timeout(3 seconds)
val myServiceActor = system.actorOf(RoundRobinPool(5).props(Props[MyService]), "myServiceActor")
val simpleRoute: Route =
(path("reference") & get) {
val validResponseFuture: Option[Future[HttpResponse]] = {
// construct the HTTP response
val actorPathResponse: Future[Option[ActorPath]] = (myServiceActor ? "reference").mapTo[Option[ActorPath]]
Option(actorPathResponse.map(ref => HttpResponse(
StatusCodes.OK,
entity = HttpEntity(
ContentTypes.`text/html(UTF-8)`,
s"""
|<html>
| <body>I got the actor reference: ${ref} </body>
|</html>
|""".stripMargin
))))
}
val entityFuture: Future[HttpResponse] = validResponseFuture.getOrElse(Future(HttpResponse(StatusCodes.BadRequest)))
complete(entityFuture)
}
println("http GET localhost:8080/reference")
Http().newServerAt("localhost", 8080).bind(simpleRoute)
}
}
class MyService extends Actor with ActorLogging {
override def receive: Receive = {
case "reference" =>
log.info(s"request reference at actor: ${self}")
sender() ! Option(self.path)
case message =>
log.info(s"unknown message: ${message.toString}")
}
}
requesting the address $ http GET localhost:8080/reference from the browser or using any HTTP requester several times you get actor reference $a, $b, etc...
// first time
<html>
<body>I got the actor reference: Some(akka://BasicRoundRobinHttpServer/user/myServiceActor/$a) </body>
</html>
// second time
<html>
<body>I got the actor reference: Some(akka://BasicRoundRobinHttpServer/user/myServiceActor/$b) </body>
</html>
...

Scala compiler can't find the unmarshalling implicits in route declaration

I'm trying to build a REST server using this tutorial:
https://spindance.com/reactive-rest-services-akka-http/
However, having reached the "Responding with JSON" section, I've noticed that my code doesn't compile, and I can't make POST requests. This is the error that I'm getting:
Error:(59, 18) could not find implicit value for parameter um: akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller[Health]
entity(as[Health]) { statusReport =>
^
Having looked at other tutorials, I've found out that you need to include an object containing an implicit variable for the class that I'm trying to unmarshall. I did that, and I even imported the httpx library, but I'm still getting this error. My code is given below.
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route
import akka.stream.ActorMaterializer
import akka.pattern.ask
import akka.util.Timeout
import spray.json._
import DefaultJsonProtocol._
import spray.httpx.SprayJsonSupport.sprayJsonUnmarshaller
import scala.concurrent.duration._
import scala.io.StdIn
object JsonImplicits extends DefaultJsonProtocol {
implicit val healthFormat = jsonFormat2(Health)
}
object MyApplication {
val host = "localhost"
val port = 8080
def main(args: Array[String]): Unit = {
implicit val system = ActorSystem("simple-rest-system")
// Something to do with flows
implicit val materializer = ActorMaterializer()
// A reference to a specific thread pool
// You can configure thread pool options through it
// It is the engine that executes the actors
implicit val executionContext = system.dispatcher
val requestHandler = system.actorOf(RequestHandler.props(), "requestHandler")
//Define the route
val route : Route = {
implicit val timeout = Timeout(20 seconds)
import JsonImplicits._
import spray.httpx.SprayJsonSupport._
path("health") {
get {
onSuccess(requestHandler ? GetHealthRequest) {
case response: HealthResponse =>
complete(StatusCodes.OK, s"Everything is ${response.health.status}!")
case _ =>
complete(StatusCodes.InternalServerError)
}
}
} ~ post {
// Entity extracts the body of the POST request and then converts it into a
// Health object
entity(as[Health]) { statusReport =>
onSuccess(requestHandler ? SetStatusRequest(statusReport)) {
case response: HealthResponse =>
complete(StatusCodes.OK,s"Posted health as ${response.health.status}!")
case _ =>
complete(StatusCodes.InternalServerError)
}
}
}
}
//Start up and listen for requests
val bindingFuture = Http().bindAndHandle(route, host, port)
println(s"Waiting for requests at http://$host:$port/...\nHit RETURN to terminate")
StdIn.readLine()
//Shutdown
bindingFuture.flatMap(_.unbind())
system.terminate()
}
}

Mock functional argument of the function

I'm developing web app using Scala, Play Framework. And I need to test controller with the custom action. Please, take a look on the code:
package controllers.helpers
import play.api.mvc.{ActionBuilder, Request, Result}
import scala.concurrent.Future
class CustomAction extends ActionBuilder[Request] {
override def invokeBlock[A](request: Request[A], block: (Request[A]) => Future[Result]): Future[Result] = {
// do some stuff
block(request)
}
}
package controllers
import javax.inject.Singleton
import play.api.mvc.Controller
#Singleton
class SomeController #Inject() (customAction: CustomAction
) extends Controller {
def foo() = customAction(parse.json) { implicit request =>
// do some stuff and create result
}
}
And below you can find a code of the test class. I use Specs2 and I got org.mockito.exceptions.misusing.InvalidUseOfMatchersException on the line with customeActionMock.invokeBlock(any[Request[_]], any[(Request[_]) => Future[Result]]) returns mock[Future[Result]]
package controllers
import controllers.helpers.CustomAction
import org.specs2.mock.Mockito
import play.api.mvc.{Request, Result}
import play.api.test.{FakeHeaders, FakeRequest, PlaySpecification}
import scala.concurrent.Future
class SomeControllerSpec extends PlaySpecification with Mockito {
private val customeActionMock = mock[CustomAction]
customeActionMock.invokeBlock(any[Request[_]], any[(Request[_]) => Future[Result]]) returns mock[Future[Result]] //this doesn't work, exception is thrown there
"SomController" should {
"respond Ok on valid request" in {
val result = new UserController(customeActionMock).foo()(FakeRequest())
status(result) shouldEqual OK
}
}
}
I understand that I mock block parameter of the CustomAction incorrectly. Can someone help me to do it properly?
My project uses Play 2.5.x. I use scalatest. This is how I test controllers.
import org.scalatestplus.play.OneAppPerSuite
import org.scalatest._
import org.scalatest.time.{Millis, Seconds, Span}
import org.scalatest.concurrent.ScalaFutures
import scala.concurrent.Future
class SomeControllerSpec extends FlatSpec with Matchers with ScalaFutures with OneAppPerSuite {
private val customeActionMock = new CustomAction // create an instance of a class
implicit val defaultPatience = PatienceConfig(timeout = Span(5,Seconds), interval = Span(500, Millis))
it should "respond Ok on valid request" in {
val resultF : Future[Result] = new UserController(customeActionMock).foo()(FakeRequest())
whenReady(resultF) { resultR =>
resultR.header.status shouldBe 200
}
}
}
Don't mock the CustomAction :
class SomeControllerSpec extends PlaySpecification with Mockito {
private val customeActionMock = new CustomAction
"SomController" should {
"respond Ok on valid request" in {
val result = new UserController(customeActionMock).foo()(FakeRequest())
status(result) shouldEqual OK
}
}
}

Stubbing SOAP requests in Scala

I use scalaxb to generate models and client part of the SOAP interface. For testing I use Betamax, which can also be used in Scala. However, scalaxb uses Netty as a transport, which ignores proxy settings set up by Betamax. How would you cope with this situation?
scalaxb uses cake pattern, so the service is built from 3 parts like in the following example:
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent._
import scala.concurrent.duration._
val service = (new stockquote.StockQuoteSoap12Bindings with
scalaxb.SoapClientsAsync with
scalaxb.DispatchHttpClientsAsync {}).service
val fresponse = service.getQuote(Some("GOOG"))
val response = Await.result(fresponse, 5 seconds)
println(response)
And tests:
import co.freeside.betamax.{TapeMode, Recorder}
import co.freeside.betamax.proxy.jetty.ProxyServer
import dispatch._
import org.scalatest.{Tag, FunSuite}
import scala.concurrent.duration._
import scala.concurrent.{Await, Future}
class StockquoteSpec extends FunSuite with Betamax {
testWithBetamax("stockquote", Some(TapeMode.READ_WRITE))("stockquote") {
val fresponse = service.getQuote(Some("GOOG"))
val response = Await.result(fresponse, 5 seconds)
println(response)
}
}
trait Betamax {
protected def test(testName: String, testTags: Tag*)(testFun: => Unit)
def testWithBetamax(tape: String, mode: Option[TapeMode] = None)(testName: String, testTags: Tag*)(testFun: => Unit) = {
test(testName, testTags: _*) {
val recorder = new Recorder
val proxyServer = new ProxyServer(recorder)
recorder.insertTape(tape)
recorder.getTape.setMode(mode.getOrElse(recorder.getDefaultMode()))
proxyServer.start()
try {
testFun
} finally {
recorder.ejectTape()
proxyServer.stop()
}
}
}
}
Versions:
net.databinder.dispatch 0.11.2
co.freeside.betamax 1.1.2
com.ning.async-http-client 1.8.10
io.netty.netty 3.9.2.Final
It is indeed possible to use proxy with Netty. Although Netty does not read system properties for proxy settings, the settings can be injected using ProxyServerSelector. It is created in build method of AsyncHttpClientConfig:
if (proxyServerSelector == null && useProxySelector) {
proxyServerSelector = ProxyUtils.getJdkDefaultProxyServerSelector();
}
if (proxyServerSelector == null && useProxyProperties) {
proxyServerSelector = ProxyUtils.createProxyServerSelector(System.getProperties());
}
if (proxyServerSelector == null) {
proxyServerSelector = ProxyServerSelector.NO_PROXY_SELECTOR;
}
The only obstacle is that scalaxb uses default config with useProxyProperties=false. You can override it with custom MyDispatchHttpClientsAsync that you can use when creating the service:
val service = (new stockquote.StockQuoteSoap12Bindings with
scalaxb.SoapClientsAsync with
MyDispatchHttpClientsAsync {}).service
And the source code of MyDispatchHttpClientsAsync (the key point is calling setUseProxyProperties(true)):
import com.ning.http.client.providers.netty.NettyAsyncHttpProvider
import com.ning.http.client.{AsyncHttpClientConfig, AsyncHttpClient}
import scalaxb.HttpClientsAsync
/**
* #author miso
*/
trait MyDispatchHttpClientsAsync extends HttpClientsAsync {
lazy val httpClient = new DispatchHttpClient {}
trait DispatchHttpClient extends HttpClient {
import dispatch._, Defaults._
// Keep it lazy. See https://github.com/eed3si9n/scalaxb/pull/279
lazy val http = new Http(new AsyncHttpClient(new NettyAsyncHttpProvider(new AsyncHttpClientConfig.Builder().setUseProxyProperties(true).build())))
// lazy val http = Http.configure(_.setUseProxyProperties(true)) // Maybe later. See https://github.com/eed3si9n/scalaxb/issues/312
def request(in: String, address: java.net.URI, headers: Map[String, String]): concurrent.Future[String] = {
val req = url(address.toString).setBodyEncoding("UTF-8") <:< headers << in
http(req > as.String)
}
}
}