Akka Actor系统终止时如何运行预定功能

时间:2019-05-07 13:46:58

标签: scala akka apache-flink

我有一个actor系统,可用来安排功能的执行,该功能会通过flink的map运算符在Kafka主题中产生事件。在例外情况下,actor系统将终止,并且应在akka文档中声明(请参阅https://doc.akka.io/docs/akka/current/scheduler.html#from-akka-actor-actorsystem),并执行所有计划的任务。在我的情况下,当函数执行时,会抛出与函数内部使用的类有关的java.lang.NoClassDefFoundError。

new RichMapFunction[String, String] {
      implicit lazy val executor: ExecutionContext = ExecutionContext.fromExecutor(Executors.directExecutor())
      var myActorSystem: ActorSystem = _
      var kafkaProducer: KafkaProducer[String, String] = _
      var runtimeContext: RuntimeContext = _

      override def map(value: String): String = {
        value match {
          case "stop" =>
            throw new Exception("Stop command received")
          case _ =>
            myActorSystem.scheduler.scheduleOnce(FiniteDuration(5L, MINUTES)){
              kafkaProducer.send(new ProducerRecord[String, String]("test", value.reverse))
            }
        }

        s"scheduled function on event $value"
      }

      override def open(parameters: Configuration): Unit = {
        myActorSystem = ActorSystem("testSystem")
        kafkaProducer = {
          val props = new Properties()
          props.put("bootstrap.servers", "localhost:9092")
          // props.put("acks", "all")
          props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer")
          props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer")
          new KafkaProducer[String, String](props)
        }
        runtimeContext = getRuntimeContext
      }

      override def close(): Unit = {
        println("Terminate actor system...")
        myActorSystem.terminate()
      }
    }

1 个答案:

答案 0 :(得分:0)

演员系统终止是异步的,因此我在下面使用了代码。

http://jenkinsURL/job/ProjectFolderName/api/xml?tree=jobs[name,color=yellow]