quartz job依赖的实现

原理

通过实现Listener实现job之间的依赖关系,
当JobB依赖JobA是,也就是监听JobA执行的时候,再执行JobB
每当DemoJobChainingJobListener初始化的时候,从数据库里面恢复job依赖关系
添加依赖关系的时候,也会保存到数据中

object DemoJobChainingJobListener{  implicit val conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/quartz","quartz","quartz")}

class DemoJobChainingJobListener(name:String) extends JobChainingJobListener(name) {

  import MyJobChainingJobListener._

  getChain().foreach(c=>{
    val jobKey = new JobKey(c.jobName,c.jobGroup)
    val nextKey = new JobKey(c.nextJobName,c.nextJobGroup)
    super.addJobChainLink(jobKey,nextKey)
  })



  def getChain(): List[Chain] ={

    val chains = SQL("select * from job_chain").executeQuery().as(chainParse *)
    chains
  }

  private def saveChain(firstJob: JobKey, secondJob: JobKey): Unit ={

    val firstName = firstJob.getName
    val firstGroupName = firstJob.getGroup
    val secondName = secondJob.getName
    val secondGroup = secondJob.getGroup

    SQL("insert into job_chain values({firstName},{firstGroupName},{secondName},{secondGroup})")
      .on("firstName"->firstName,"firstGroupName"->firstGroupName,
        "secondName"->secondName,"secondGroup"->secondGroup).executeInsert()

  }

  override def addJobChainLink(firstJob: JobKey, secondJob: JobKey): Unit = {
    super.addJobChainLink(firstJob, secondJob)
    saveChain(firstJob,secondJob)
  }

  override def jobToBeExecuted(context: JobExecutionContext): Unit =
    super.jobToBeExecuted(context)
}

case class Chain(jobName:String,jobGroup:String,nextJobName:String,nextJobGroup:String)

你可能感兴趣的:(quartz job依赖的实现)