scala akka 修炼之路1(使用actor实现一个job的并发计算和task失败重启)

package cn.yangg.scala.akka.init

 

import akka.actor.Actor

import akka.actor.Props

import akka.actor.ActorRef

import akka.actor.Terminated

import akka.event.Logging

import akka.actor.ActorSystem

import akka.pattern.ask

import scala.util.Success

import scala.util.Failure

import scala.concurrent.ExecutionContext.Implicits.global

import scala.concurrent.ExecutionContext

import akka.actor.Terminated

 

 

 

case class HeartBeat(taskid:Int,parent:String)

case class TaskFinished(taskid:Int)

case object JobFinished

case object JobStart

case class TaskFailure(taskid:Int)

case class TaskMessage

case class TaskRestartMessage

case class TaskFinishedMessage

 

class Parent(jobid:String,tasknum:Int) extends Actor{

  val log=Logging(this.context.system,this)

  var tasks=Array[ActorRef]()

  var replySender=this.context.system.deadLetters

  varcount=0;//存在问题 

  def receive={

    case JobStart=>{

       this.replySender=this.context.sender

       tasks=(1 to tasknum).map(id=>this.context.actorOf(Props(new Child(id)))).toArray

      tasks.foreach(actor=>(actor ! TaskMessage))

    }

    case heartBeat:HeartBeat=>{

      println("taskid-0000"+heartBeat.taskid+",finished:"+heartBeat.parent)

    }

    case TaskFinished(taskid)=>{

      println("taskid-0000"+taskid+"finished...")

      this.self ! TaskFinishedMessage

    }

    case Terminated(actor)=>

      println(actor.path.toString()+"stop")

    case TaskFailure(taskid)=>{

      //restart task

      valrestartActor=this.context.actorOf(Props(new Child(taskid*10)))

      restartActor ! TaskRestartMessage

    } 

    case TaskFinishedMessage=>{

      this.count+=1

      if(this.count==tasknum){

        this.replySender ! akka.actor.Status.Success("all task finished")

      }

      println(this.count)

    }

  }

}

class Child(taskid:Int) extends Actor{

  val log=Logging(this.context.system,this)

  def receive={

    case TaskMessage=>{

      Thread.sleep(1000)

      this.context.parent ! HeartBeat(taskid,"10%")

      Thread.sleep(2000)

      this.context.parent ! HeartBeat(taskid,"70%")

      

      //task failed 

      this.context.stop(this.self)

      if(taskid%3==0){

        this.context.parent ! TaskFailure(this.taskid)

        log.info("taskid="+taskid+" task failed")

      }else{

        this.context.parent ! TaskFinished(this.taskid)

      }     

    }

    case TaskRestartMessage=>{

      log.info(taskid+" restart...")

      this.context.parent ! TaskFinished(this.taskid)

    }

  }

}

object StartMoon {

def main(args:Array[String]){

  val system=ActorSystem("actorSystem")

  val jobActor=system.actorOf(Props(new Parent("DataSplit-job",10)),"DataSplitJob")

  val jobListener=ask(jobActor,JobStart)(10000)

  jobListener.onComplete(result => resultmatch{

    case Success(result)=>{

      println("job finished...,message:"+result)

    }

    case Failure(result)=>{

      println("job failed...,message:"+result.getMessage())

    }

  })

}

}

 

http://blog.csdn.net/yangguo_2011/article/details/27399431

你可能感兴趣的:(akka)