Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8452857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:36:43+00:00 2026-06-10T11:36:43+00:00

I am using akka for some time. I started to see some patterns in

  • 0

I am using akka for some time. I started to see some patterns in my code to solve late reply for async io. Is this implementation ok? There is another way to do a late reply without block?

class ApplicationApi(asyncIo : ActorRef) extends Actor {
    // store senders to late reply
    val waiting = Map[request, ActorRef]()

    def receive = {
        // an actore request for a user, store it to late reply and ask for asyncIo actor to do the real job
        case request : GetUser => 
            waiting += (sender -> request)
            asyncIo ! AsyncGet("http://app/user/" + request.userId)
        // asyncio response, parse and reply
        case response : AsyncResponse =>
            val user = parseUser(response.body)
            waiting.remove(response.request) match {
                case Some(actor) => actor ! GetUserResponse(user)
            }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:36:44+00:00Added an answer on June 10, 2026 at 11:36 am

    One way to avoid blocking while waiting for a reply is to send using the ask method—a.k.a. ? operator—which returns a Future (unlike ! which returns ()).

    Using the onSuccess or foreach methods, you can specify actions to be performed if/when the future is completed with a reply. To use this you need to mix in the AskSupport trait:

    class ApplicationApi(asyncIo : ActorRef) extends Actor with AskSupport {
    
      def receive = {
        case request: GetUser =>
          val replyTo = sender
          asyncIo ? AsyncGet("http://app/user/" + request.userId) onSuccess {
            case response: AsyncResponse =>
              val user = parseUser(response.body)
              replyTo ! GetUserResponse(user)
          }
    
    }
    

    Avoid using this technique to perform any side effect that modifies the state of the ApplicationApi actor, because the effect will happen out-of-sync with the receive loop. Forwarding messages to other actors should be safe, though.


    By the way, here is a trick to capture the current sender as part of the pattern match, avoiding the need to assign it to a variable later.

    trait FromSupport { this: Actor =>
      case object from {
        def unapply(msg: Any) = Some(msg, sender)
      }
    }
    
    class MyActor extends Actor with FromSupport {
      def receive = {
        case (request: GetUser) from sender =>
          // sender is now a variable (shadowing the method) that is safe to use in a closure
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Akka framework recommends using typed actor only for interacting with external code. However, standard
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question
Using CI for the first time and i'm smashing my head with this seemingly
When using Akka 2.0, Is there a way to get an ActorRef to a
I'm running into some deployment issues using Akka remoting to implement a small search
I was wondering the best implementation of a DAG (Directed Acyclic Graph) using Scala/Akka
I have a Scala application using Akka that receives REST requests, makes some operations
I reckon there might be a broader question of application design using Akka hidden
I am using Akka FSM for handling state in my Actor. I want some
Integrating redis with my Scala application using Akka but for some reason it does

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.