Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ask pattern ignore sender argument #20917

Closed
deripas opened this issue Jul 7, 2016 · 2 comments
Closed

ask pattern ignore sender argument #20917

deripas opened this issue Jul 7, 2016 · 2 comments
Assignees
Labels
Milestone

Comments

@deripas
Copy link

@deripas deripas commented Jul 7, 2016

Hi Team,
I can't ask actor with custom sender, simple code example:

object Main extends App {
  override def main(args: Array[String]): Unit = {
    val system: ActorSystem = ActorSystem.create("test", ConfigFactory.parseFile(new File("application.conf")))
    val sender: ActorRef = system.actorOf(Props[Sender])
    val actor: ActorRef = system.actorOf(Props[Receiver])

    actor.tell("test 1", sender)
    ask (actor, "test 2", sender) (new Timeout(100, TimeUnit.MILLISECONDS))
    ask (actor, "test 3", ActorRef.noSender) (new Timeout(100, TimeUnit.MILLISECONDS))
  }
}

class Sender extends Actor {
  def receive = {
    case msg => println("sender: " + msg)
  }
}

class Receiver extends Actor {
  def receive = {
    case msg => {
      println("receive: " + msg + " from " + sender())
      sender () ! msg.toString.toUpperCase
    }
  }
}

result:

receive: test 1 from Actor[akka://test/user/$a#1690443577]
sender: TEST 1
receive: test 2 from Actor[akka://test/temp/$a]
receive: test 3 from Actor[akka://test/temp/$b]

"test 1" is correct, sender received answer from actor.
"test 3" is correct because sender is ActorRef.noSender
"test 2" NOT correct, because have sender as ActorRef.noSender

source in class AskSupport.scala, object PromiseActorRef, method apply:

  def apply(provider: ActorRefProvider, timeout: Timeout, targetName: Any, messageClassName: String, sender: ActorRef = Actor.noSender): PromiseActorRef = {
    val result = Promise[Any]()
    val scheduler = provider.guardian.underlying.system.scheduler
    val a = new PromiseActorRef(provider, result, messageClassName)
    implicit val ec = a.internalCallingThreadExecutionContext
    val f = scheduler.scheduleOnce(timeout.duration) {
      result tryComplete Failure(
        new AskTimeoutException(s"""Ask timed out on [$targetName] after [${timeout.duration.toMillis} ms]. Sender[$sender] sent message of type "${a.messageClassName}"."""))
    }
    result.future onComplete { _  try a.stop() finally f.cancel() }
    a
  }

Argument sender using only in exception text. May be need add argument in constructor:
val a = new PromiseActorRef(provider, result, messageClassName**, sender**)

version: 2.4.7
Best regards.

PS: You can add a method ask with sender to Patterns.scala for using by Java Api

@ktoso

This comment has been minimized.

Copy link
Member

@ktoso ktoso commented Jul 11, 2016

This is not a bug, this is how the ask pattern works.

The sender MUST be ignored like this, and is included only for logging as the ask pattern creates a small Actor which is the sender, and it completes the Future once it gets the reply.

If you want the response to be send to a normal Actor that's the normal Actor messaging (tell / !).

@ktoso ktoso self-assigned this Jul 11, 2016
@ktoso ktoso added the t:core label Jul 11, 2016
@ktoso ktoso added this to the invalid milestone Jul 11, 2016
@ktoso ktoso closed this Jul 11, 2016
@alex-eyereturn

This comment has been minimized.

Copy link

@alex-eyereturn alex-eyereturn commented Sep 26, 2018

It might be very helpful to include this point in the official documentation of Patterns. I also fount it very confusing to understand how the result gets propagated back to the sender when using Patterns.ask since I was only looking at the mailbox of the actual actors that I create.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.