In this method I would like to see the actual response (result.toJson.toString or StatusCodes.InternalServerError.toString) returned instead of empty string. How can I do that?
def process(msgIn : WebSocketMessageIn, service : ActorRef) : String = {
import model.Registration
import model.RegistrationJsonProtocol._
implicit val timeout = Timeout(10 seconds)
msgIn.method.toUpperCase match {
case "POST" =>
log.debug(s"Handing POST message with body ${msgIn.body}")
val registration = msgIn.body.convertTo[Registration]
val future = (service ? PostRegistrationMessage(registration)).mapTo[Registration]
var response = ""
future onComplete {
case Success(result) =>
response = result.toJson.toString
case Failure(e) =>
log.error(s"Error: ${e.toString}")
response = StatusCodes.InternalServerError.toString
}
response
case "PUT" =>
s"Handing PUT message ${msgIn.body}"
}
}
Here is the code snippet that calls the method and sends the response to websocket
case Message(ws, msg, service) =>
log.debug("url {} received msg '{}'", ws.getResourceDescriptor, msg)
val wsMessageIn = msg.parseJson.convertTo[WebSocketMessageIn]
val response = process(wsMessageIn, service)
ws.send(response);
UPDATE 1: Updated to use Await.result(future, 5000 millis) instead of 'future onComplete { ... }'. Here is code snippet that changed. Works now, but just wondering how we will handle failures.
msgIn.method.toUpperCase match {
case "POST" =>
log.debug(s"Handing POST message with body ${msgIn.body}")
val registration = msgIn.body.convertTo[ADSRegistration]
val future = (service ? PostADSRegistrationMessage(registration)).mapTo[ADSRegistration]
val response = Await.result(future, 5000 millis)
response.toJson.toString
You can use Await.result which is blocking. Something like this:
import scala.concurrent.duration._
val result = Await.result(future, atMost = 10.second)
val response = //result processing
Just the same, you could pass the future back and perform the send in the onComplete which would be much more reactive
Related
i am using akka http one of my routes is interacting with an external service via akka http client side api and the httpRequest is continuously running i am unable to make it work
here is my use case -> i am interacting with a janus server and doing a long poll get request as soon as the server responded back with an 'keepAlive' or an "event" i am requesting again and so on the server keeps on responding
all of this is happening inside an actor and i have an akka htttp route which is intiailising the first request
here is my code
final case class CreateLongPollRequest(sessionId:BigInt)
class LongPollRequestActor (config: Config) extends Actor {
def receive = {
case CreateLongPollRequest(sessionId) =>
senderRef = Some(sender())
val uri: String = "localhost:8080/" + sessionId
val request = HttpRequest(HttpMethods.GET, uri)
val responseFuture = Http(context.system).singleRequest(request)
responseFuture
.onComplete {
case Success(res)
Unmarshal(res.entity.toStrict(40 seconds)).value.map { result =>
val responseStr = result.data.utf8String
log.info("Actor LongPollRequestActor: long poll responseStr {}",responseStr)
senderRef match {
case Some(ref) =>
ref ! responseStr
case None => log.info("Actor LongPollRequestActor: sender ref is null")
}
}
case Failure(e) =>log.error(e)
}
}
}
final case class JanusLongPollRequest(sessionId: BigInt)
class JanusManagerActor(childMaker: List[ActorRefFactory => ActorRef]) extends Actor {
var senderRef: Option[akka.actor.ActorRef] = None
val longPollRequestActor = childMaker(1)(context)
def receive: PartialFunction[Any, Unit] = {
case JanusLongPollRequest(sessionId)=>
senderRef = Some(sender)
keepAlive(sessionId,senderRef)
}
def keepAlive(sessionId:BigInt,sender__Ref: Option[ActorRef]):Unit= {
val senderRef = sender__Ref
val future = ask(longPollRequestActor, CreateLongPollRequest(sessionId)).mapTo[String] //.pipeTo(sender)
if (janus.equals("keepalive")) {
val janusRequestResponse = Future {
JanusSessionRequestResponse(janus = janus)
}
senderRef match {
case Some(sender_ref) =>
janusRequestResponse.pipeTo(sender_ref)
}
keepAlive(sessionId,senderRef)
}
else if (janus.equals("event")) {
//some fetching of values from server
val janusLongPollRequestResponse = Future {
JanusLongPollRequestResponse(janus = janus,sender=sender, transaction=transaction,pluginData=Some(pluginData))
}
senderRef match {
case Some(sender_ref) =>
janusLongPollRequestResponse.pipeTo(sender_ref)
}
keepAlive(sessionId,senderRef)
}
def createLongPollRequest: server.Route =
path("create-long-poll-request") {
post {
entity(as[JsValue]) {
json =>
val sessionID = json.asJsObject.fields("sessionID").convertTo[String]
val future = ask(janusManagerActor, JanusLongPollRequest(sessionID)).mapTo[JanusSessionRequestResponse]
onComplete(future) {
case Success(sessionDetails) =>
log.info("janus long poll request created")
val jsonResponse = JsObject("longpollDetails" -> sessionDetails.toJson)
complete(OK, routeResponseMessage.getResponse(StatusCodes.OK.intValue, ServerMessages.JANUS_SESSION_CREATED, jsonResponse))
case Failure(ex) =>
failWith(ex)
}
}
}
now the above route createLongPollRequest worked fine for the first time I can see the response and for the next attempts i am getting a dead letter as follows
[INFO] [akkaDeadLetter][07/30/2021 12:13:53.587] [demo-Janus-ActorSystem-akka.actor.default-dispatcher-6] [akka://demo-Janus-ActorSystem/deadLetters] Message [com.ifkaar.lufz.janus.models.janus.JanusSessionRequestResponse] from Actor[akka://demo-Janus-ActorSystem/user/ActorManager/ManagerActor#-721316187] to Actor[akka://demo-Janus-ActorSystem/deadLetters] was not delivered. [4] dead letters encountered. If this is not an expected behavior then Actor[akka://demo-Janus-ActorSystem/deadLetters] may have terminated unexpectedly. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.
probably this is causing the issue after the first iteration
responseFuture.pipeTo(sender()
IS there a way where i can get a response in my akkahttp route when ever my backend server responds?
The Actor should only reply once to the CreateLongPollRequest and it should only do this when it has valid data. If the poll fails the Actor should just issue another poll request.
It is difficult to give more help without the details of the Actor.
I'm trying to execute the following Scala code inside an Akka Actor.
class FilteringService(implicit timeout: Timeout) extends Actor {
def receive: PartialFunction[Any, Unit] = {
case GetProfiles ⇒
val requester = sender
def getProfiles = {
var result = new Array[Profile](0)
println("[GET-PROFILES] Entered, making request")
val req = Get("http://localhost:9090/profiles")
implicit val profileFormat = jsonFormat16(Profile)
val responseFuture: Future[HttpResponse] = Http().singleRequest(req)
println("[GET-PROFILES] Entered, request sent")
responseFuture.onComplete {
case Success(response) =>
println("[RES - SUCCESS] Request returned with " + response.status)
val responseAsProfiles = Unmarshal(response.entity).to[Array[Profile]]
responseAsProfiles.onComplete {
println("[UNMARSH - SUCCESS] Unmarshaling Done!")
_.get match {
case profiles: Array[Profile] =>
println("[UNMARSH - SUCCESS] Sending Profiles message to " + sender())
requester ! profiles
println("[UNMARSH - SUCCESS] Message sent to " + sender())
case _ => println("error")
}
}
case Failure(_) =>
sys.error("something wrong")
//return Future[Array[Profile]]
}
}
println("[RECEIVE] Message GetProfiles received from " + sender().toString())
getProfiles
println("[RECEIVE] Message GetProfiles invoked")
}
When the Actor receives the message "GetProfiles":
1- it sends a request to a remote server, so the result of operation is a Future[HttpResponse]
2- in case of success it retrieves the response (a JSON array) and asks for unmarshalling the object to Array[Profile]. (It's not important the Profile model). The result of Unmarshall method is a Future[Array[Profile]]
3- In case of success, I want to send the result back to the original sender!
I managed to do this, but it's a trick because I'm saving the sender in a variable, that is visible in the scope (requester).
I know that there is the pipe pattern, so I could send the responseAsProfiles object back to the sender in theory, but the object is created inside the onComplete method of the responseFuture object (we have to wait it, of course!)
So that's all!
How could I send the result back to the sender using the pipe pattern in this case?
Thanks in advance!!!
General idea is that you compose futures using map and flatMap and try to avoid using onComplete as much as possible.
See if you can convert your code to following smaller pieces and then compose:
def getRawProfileData(): Future[HttpResponse] = {
// ... here you make http request
}
def unmarshalProfiles(response: HttpResponse): Future[List[Profile]] = {
// ... unmarshalling logic
}
def getProfiles(): Future[List[Profile]] = getRawProfileData().flatMape(unmarshalProfiles)
// now from receive block
case GetProfiles ⇒ getProfiles().pipeTo(sender())
i trying get response from my actor using ask pattern
in ask response i have List(scala.concurrent.impl.CallbackRunnable#33d40b3)
but i expect get string "1,2"
How i can get expected results ?
this is my code:
class Storage extends Actor {
var map:ListBuffer[List[String]] = new ListBuffer
val logger = Logging(context.system,this)
override def receive = {
case setRequest(url, urlType)=>
map+=List( url, urlType)
logger.info(s"Putting ${url} to storage")
sender() ! Status.Success
case getRequest()=>
if (map.length >=1){
var response= map(0).mkString(",")
logger.info(s"Send ${response}")
map = map.filter(x => x != response)
sender ! response
}
else{
sender()! Status.Failure(new emptyStorage)
}
case getLength()=>
sender()! map.length
}
}
object Main extends App{
implicit val timeout = Timeout(5 seconds)
val system = ActorSystem.create("default-dispatcher", ConfigFactory.load().getConfig("MyDispatcherExample"))
val storage = system.actorOf(Props(new Storage))
storage ! setRequest("1", "2")
val result = Future {storage ? getRequest }
result onComplete{
case Success(result)=> println(result)
case Failure(result)=> println("some error")
}
}
The ask pattern by itself returns a Future which you're wrapping in an additional Future giving us Future[Future[Any]]. There's not need for that:
val result = storage ? getRequest
result onComplete {
case Success(res) => println(res)
case Failure(e) => println(e)
}
Additionally, when you pass data around in Akka, especially generic data types which are subject to type erasure, it is recommended to wrap them in a case class. For example:
case class Response(result: String)
It is also that a Future returned by ask is untyped. It is recommended to use mapTo in order to cast to a typed response:
val result: Future[Response] = (storage ? getRequest).mapTo[Response]
In Scala I have a call to service in controller which is returning me Future[WSResponse]. I want to make sure service is returning valid result so send Ok(..) otherwise send BadRequest(...). I don't think I can use map. Any other suggestion?
def someWork = Action.async(parse.xml) { request =>
val result:Future[WSResponse] = someService.processData(request.body.toString())
//Need to send back Ok or BadRequest Message
}
EDIT
Solution from #alextsc is working fine. Now moving to test my existing test is failing. It is getting 400 instead of 200.
test("should post something") {
val requestBody = <value>{UUID.randomUUID}</value>
val mockResponse = mock[WSResponse]
val expectedResponse: Future[WSResponse] = Future.successful(mockResponse)
val request = FakeRequest(Helpers.POST, "/posthere").withXmlBody(requestBody)
when(mockResponse.body).thenReturn("SOME_RESPONSE")
when(someService.processData(any[String])).thenReturn(expectedResponse)
val response: Future[Result] = call(controller.someWork , request)
whenReady(response) { response =>
assert(response.header.status == 200)
}
}
You're on the right track and yes, you can use map.
Since you're using Action.async already and your service returns a future as it stands all you need to do is map that future to a Future[Result] so Play can handle it:
def someWork = Action.async(parse.xml) { request =>
someService.processData(request.body.toString()).map {
// Assuming status 200 (OK) is a valid result for you.
case resp : WSResponse if resp.getStatus == 200 => Ok(...)
case _ => BadRequest(...)
}
}
(I note that your service returns WSResponse (from the play ws java library) and not play.api.libs.ws.Response (the scala version of it), hence getStatus and not just status)
Looking to achieve this:
HTTP request to REST API -> parse -> make async call to another API -> respond to http req with the result of the async call.
Currently, the code looks like:
def getItems(param: String): LiftResponse = {
#volatile var resp: LiftResponse = new BadResponse
param.toLowerCase match {
case "something" =>
val req = Async call returning a Future
req onSuccess {
case items =>
resp = new JsonResponse(items map (decompose(_)), S.getResponseHeaders(Nil), S.responseCookies, 200)
}
req onFailure {
case fail => resp = new BadResponse
}
resp
case _ => new OkResponse
}
}
But it looks like poor implementation.
What is the idiomatic Scala way to write the above?
Your code will probably not do what you think it should since it depends on scheduling whether it returns null or something else. Is LiftResponse a strict value or can it be deferred? If strict then you will have to return a Future[LiftResponse] obtained by mapping your req: Future.
Look into using Lift's RestHelper in conjunction with RestContinuation.async. It supports using continuations to suspend a request until data is available for it. There's some sample code at http://demo.liftweb.net/async_rest . After the async continuation is invoked and before the reply function is called with the result, the request thread will be released to the thread pool. Once the reply function is called, the request will be put back on a thread and the response sent to the client.
I think you can try to inline #volatile var resp by:
def getItems(param: String): LiftResponse = {
param.toLowerCase match {
case "something" =>
val req = Async call returning a Future
req.onComplete {
case Success(items) => new JsonResponse(items map (decompose(_)), S.getResponseHeaders(Nil), S.responseCookies, 200)
case Failure(t) => new BadResponse
}
case _ => new OkResponse
}
}
--edit--
sorry, onComplete returns Unit, how about using Await to get the result of future:
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent._
import scala.concurrent.duration._
def getItems(param: String): LiftResponse = {
param.toLowerCase match {
case "something" =>
val req = Async call returning a Future
val response = req map { items =>
new JsonResponse
} recover {
case t:Throwable => new BadResponse
}
Await.result(response, Duration(100, MILLISECONDS))
case _ => new OkResponse
}
}
Note (3 years later): with the recent (Nov. 2016) release of Lift3, you can use net.liftweb.http._, as described in "Request and Session Access in Lift Futures", from Piotr Dyraga.
As an example, say that you want to lazily render a list of users.
First, you execute database query asynchronously and get Future[Seq[User]] as a result of this operation.
If you use Lift 2, incorporate FutureBinds that I described in one my previous posts or if you use Lift3, import net.liftweb.http._ and do:
val allUsers: Future[Seq[User]] = ... // retrieve users from DB
".user-list-async-container" #> allUsers.map { users =>
".user-list" #> users.map { user =>
".user" #> userBindings(user)
}
}