Задать вопрос
  • Как применить значение из Future[T] к стейту актора?

    mrRontgen
    @mrRontgen
    Scala lover.
    Ну типа того наверное,

    class BlockingInsideOfActor extends Actor {
      var state = State() // next time I'll use become(newState)...
      def timeout: Timeout = ...
      
      val queue = Queue[(Request, ActorRef)].empty
      
      def pending: Receive = {
         case req: Request =>
          queue.push((req, sender()))
          //log message here 
      }
    
      def normalContext = {
       //popping queue in order of incoming message
      }
      
      def receive = {
        case req: Request =>
          val futRespData: Future[ResponseData] = processRequest(req) // resp is under a Future.
          val sndr = sender()
          context.become(pending)// catch all messagee whil pending request 
          
          futRespData.onComplete{ tryResutl => 
            tryResutl match {
              
              case Success(...) =>  
                  val nextState =if (respData.success) state.incSuccessCount 
                  else state.incFailureCount
                  
               // response is a function of 'resp'
              sndr ! Response(
                        data = respData, 
                        successCount = nextState.successCount
                        failureCount = failureCount.failureCount)    
                        
       //       state = nextState     
             context.become(normalContext)  
            }
          }
          
      }
    }
    Ответ написан
    9 комментариев