package com.shiwusuo.ReadHdfsToClickHouse
import java.io.{FileSystem => _}
import akka.Done
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
import akka.http.scaladsl.server.Directives._
import akka.stream.ActorMaterializer
import com.typesafe.config.ConfigFactory
import spray.json.DefaultJsonProtocol._
import scala.concurrent.Future
//import SparkReaderTestSession.dealSession
/** @创建时间 2020/8/27
* @描述
* 最原始的任务引擎
* 不用了
* */
object HttpService1 {
//加载配置项
val config = ConfigFactory.load("application.conf")
//2个web接口使用的配置项
val webAddress = config.getString("web.address")
val webPort = config.getString("web.port")
val hdfsLog = config.getString("web.hdfsLog")
//全局程序控制变量,同一时间只能运行一个任务
@volatile var flag = false
implicit val system = ActorSystem("my-system")
implicit val materializer = ActorMaterializer()
// needed for the future flatMap/onComplete in the end
implicit val executionContext = system.dispatcher
//接收任务与任务完成返回类
final case class ReadToClickhouse(
taskId: String,
targetId: String,
resourceId: String,
tag: String,
module: String,
conDir: String,
operatorName: String,
env_type: String)
//任务id接收类
final case class ReadToClickhouseTask(taskId: String)
//json结构化
implicit val ReadToClickhouseFormat = jsonFormat8(ReadToClickhouse)
implicit val ReadToClickhouseTaskFormat = jsonFormat1(ReadToClickhouseTask)
//系统控制台的轮询请求时,程序运行的状态,程序空闲为0,执行处理中为1,执行出错为2,执行完成为3。
var ReadToClickhouseState: Int = 0
// 处理方法
def getAll(readToClickhouse: ReadToClickhouse): Future[Done] = {
ReadToClickhouseState = 1
try {
println("开始处理自己的程序========")
ReadToClickhouseState = 3
} catch {
case e: Throwable => e.printStackTrace()
ReadToClickhouseState = 2
}
Future {
Done
}
}
def main(args: Array[String]): Unit = {
val route =
concat(
post {
path("ReadToClickhouse") {
entity(as[ReadToClickhouse]) { ReadToClickhouse1 =>
if (flag) {
complete("已有任务正在执行\n,请稍后重新发送任务")
} else {
flag = true
val fut: Future[Unit] = Future {
// 异步执行 ,创建一个线程
new Thread(new Runnable {
override def run(): Unit = {
getAll(ReadToClickhouse1)
flag = false
}
}).start
}
onComplete(fut) { done =>
complete("ReadToClickhouse TaskId receive\n")
}
}
}
}
})
val bindingFuture = Http().bindAndHandle(route, webAddress, webPort.toInt)
println(s"Server online at http://" + webAddress + ":" + webPort)
}
}
Scala AKKA HTTP 简单示例
最新推荐文章于 2024-06-28 15:10:50 发布