scala定时任务调度器

该博客介绍了一个使用Scala编写的Linux环境下定时任务调度器,特别关注任务间的依赖关系。通过GitHub链接提供了源代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一个用scala写的,在linux下执行的定时任务调度器 定时任务时常会有一个定时任务依赖另一个定时任务,这份代码就是用来解决这个问题的

package com.tcl.bigdata.yhb.timer

import java.util.Date
import com.tcl.bigdata.yhb.timer.bean.TimerDependBean
import scala.collection.mutable
import scala.xml.XML
//import sys.process._

object TimerDependence {

  val waitOutputs = new mutable.HashSet[String]
  val finishOutputs = new mutable.HashSet[String]
  val failedOutputs = new mutable.HashSet[String]
  val commands = new mutable.HashSet[TimerDependBean]

  private def getFirstNodeText(nodeSeq:scala.xml.NodeSeq) = {
    nodeSeq.headOption match {
      case Some(node) => node.text.trim
      case None => ""
    }
  }

  private def loadConf(confPath:String){
    val commandsXML = XML.loadFile(confPath)
    for(commandXML <- commandsXML \ "timer-command"){
      val outputs = getFirstNodeText(commandXML \ "outputs")
      val dependence = getFirstNodeText(commandXML \ "dependence")
      val command_content = getFirstNodeText(commandXML \ "command-content")
      //命令放在commands数组
      commands += new TimerDependBean(outputs.split(",").toSet,dependence.split(",").toSet,command_content)
      //这个集合主要用来判断当前的命令可不可以执行
      waitOutputs ++= outputs.split(",").toSet
    }
  }

  private def executeCommands(){
    var executed:mutable.HashSet[TimerDependBean] = null
    do{
      executed = new mutable.HashSet[TimerDependBean]
      for(command <- commands){
        if(command.dependence == Set("") || command.dependence.subsetOf(finishOutputs)){
          val process = Runtime.getRuntime.exec(command.command)
//          val isSuccess = command.command !
          //判断是否执行成功
          if(process.waitFor() == 0){
//          if(isSuccess == 0){
            println(new Date + " execute \"" + command.command + "\" success")
            waitOutputs --= command.outputs
            executed += command
            finishOutputs ++= command.outputs
          }else{
            println(new Date + " execute \"" + command.command + "\" failed")
            waitOutputs --= command.outputs
            executed += command
            failedOutputs ++= command.outputs
          }
        }else if(!command.dependence.subsetOf(waitOutputs union finishOutputs)){
          println(new Date + " \"" + command.command + "\" can't find dependence " + command.dependence)
          waitOutputs --= command.outputs
          executed += command
          failedOutputs ++= command.outputs
        }
      }
      commands --= executed
    }while(commands.nonEmpty && executed.nonEmpty)
    failedOutputs ++= waitOutputs
  }

  def main(args: Array[String]): Unit = {
    val confPath = args(0)
    loadConf(confPath)
    executeCommands()
    println("failedOutputs:"+failedOutputs.mkString(","))
  }
}
配置文件示例:

<timer-commands>
	<timer-command>
		<outputs>1</outputs>
		<dependence></dependence>
		<command-content>java -version</command-content>
	</timer-command>
	<timer-command>
		<outputs>2</outputs>
		<dependence></dependence>
		<command-content>java aa</command-content>
	</timer-command>
	<timer-command>
		<outputs>3</outputs>
		<dependence>1</dependence>
		<command-content>java -version</command-content>
	</timer-command>
	<timer-command>
		<outputs>4</outputs>
		<dependence>2</dependence>
		<command-content>java -version</command-content>
	</timer-command>
	<timer-command>
		<outputs>5</outputs>
		<dependence>7</dependence>
		<command-content>java -version</command-content>
	</timer-command>
	<timer-command>
		<outputs>6</outputs>
		<dependence>5</dependence>
		<command-content>java -version</command-content>
	</timer-command>
</timer-commands>
源码地址:

https://github.com/CNGong/timer-controller


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值