Scala AKKA HTTP 简单示例


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)
  }
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值