The Ask Pattern (in Scala with Akka)

Поділитися
Вставка
  • Опубліковано 26 лис 2024

КОМЕНТАРІ • 19

  • @pr0master
    @pr0master 8 років тому +8

    I want to thank you for the time and effort that you take to create these videos. You have helped me a lot.

    • @MarkLewis
      @MarkLewis  8 років тому +3

      You are welcome. I am glad that you have found them useful.

  • @srikrishnabhat6793
    @srikrishnabhat6793 7 років тому

    Interesting although, why wasn't a Timeout exception raised as your thread sleep was longer than the timeout period that you provided?

    • @MarkLewis
      @MarkLewis  7 років тому

      Good question. My assumption would be that the terminate shut down the part of the system that would have raised the timeout exception well before it got there. Putting a sleep right before the terminate of two seconds would cause the timeout exception if my guess is correct.

  • @MrNiceseb
    @MrNiceseb 7 років тому +1

    Hi Mark, how to view this series in logical sequence as it is hard to follow otherwise..

    • @MarkLewis
      @MarkLewis  7 років тому

      Here is the playlist: ua-cam.com/play/PLLMXbkbDbVt98z_6KWt3fU3W5jTOja9zY.html. I wish that UA-cam would automatically show the playlists that the creator of a video put that video in.

    • @daqi-media
      @daqi-media 6 років тому

      All the knowledge about Scala principle you are able to explore here: www.programmingusingscala.net/home/object-orientation-abstract-and-data-structures-using-scala---2nd-edition
      I think Mark who is a really low-key scholar :)

  • @herbalhairoil9472
    @herbalhairoil9472 5 років тому

    what is actor of()

    • @MarkLewis
      @MarkLewis  5 років тому

      The actorOf method is used to create actors. You pass it the information on how to create the actor, which is contained in the Props object.

  • @tanson86
    @tanson86 7 років тому

    implicit val timeout = Timeout(1.seconds)
    val answer = actor ? AskName
    This call is very confusing
    def ?(message: Any)(implicit timeout: Timeout, implicit sender: ActorRef): Future[Any]
    Im finding difficulty in identifying if this a curried function or partial function.
    Can you clarify how or what parameters are passed to the method at runtime.
    Is AskName passed to Any and timeout to Timeout and actor to ActorRef?
    Appreciate your hardwork in simplifying such complex concepts and creating videos to help online learners.

    • @MarkLewis
      @MarkLewis  7 років тому

      The ? method is curried with the second argument list being implicit. That means that in most cases, you won't provide the second argument list, and values will be passed implicitly for you.
      For this particular call in my code, I'm not doing much with the result. The line that has "val answer = actor ? AskName" is actually expanded out to the following:
      val answer = actor.?(AskName)(timeout, self)
      This is using three aspects of the Scala syntax/semantics for translation. First, the ? is being used with infix (operator) notation because it is a method that has one argument. It is also curried. The other arguments, after the message, are provided in a second argument list. In addition, that second argument list is implicit, so Scala looks for appropriately typed implicit values to use in it.
      Note that the answer is a Future[Any]. As such, you will have to use methods like map, filter, foreach, ... in order to do things with the results when it comes back. If dealing with the response involved the value "sender", take special care to make a local copy of it. You might want to val val s = sender before you do the ask and then use "s" instead of "sender" in code that handles the Future.

    • @tanson86
      @tanson86 7 років тому +2

      Mark Lewis , your precise tutorials and quick replies are really helping me.

    • @MarkLewis
      @MarkLewis  7 років тому +1

      Glad to help. I was a bit late in that last reply, but things have been busy. I'm trying to catch up on some stuff this weekend.

  • @herbalhairoil9472
    @herbalhairoil9472 5 років тому

    what is props

  • @ummiikku6994
    @ummiikku6994 7 років тому

    object ActorCountDown extends App{
    case class StartCount(n : Int, otherActor: ActorRef)
    case class CountDown(n : Int)
    class ContDownActor extends Actor{
    override def receive: Receive = {
    case StartCount(n, actorRef)=> {
    println( context.toString +"is counting : " + n)
    actorRef ! CountDown(n-1)
    }
    case CountDown(n) => {
    println( context.toString +"is counting : " + n)
    if(n > 0 )
    sender ! CountDown(n-1)
    else
    context.system.terminate()
    }
    }
    }
    val systemActor = ActorSystem("FirstSystemActor")
    val actor1 = systemActor.actorOf(Props[ContDownActor],"actor1")
    val actor2 = systemActor.actorOf(Props[ContDownActor],"actor2")
    actor1 ! StartCount(10,actor2)
    }
    Any reasons why this code throws deadletters

    • @MarkLewis
      @MarkLewis  7 років тому

      That is odd. I can't reproduce that behavior. I just put that exact code into a project with Akka and it is perfectly happy. Even when I put in a sleep to slow down the counting, everything was happy.

    • @ummiikku6994
      @ummiikku6994 7 років тому

      All my fault under the case StartCount I had a sender ! CountDown which was throwing it found out as we were sending the start count msg from out side the actor class and that's why it tried to send it to the deadletter actor which was throwing that exception

    • @MarkLewis
      @MarkLewis  7 років тому +1

      That would do it. As you have figured out, the Start message is being sent from main, which isn't in an actor, so there isn't a valid sender to reply to.