Websocket客户端不从Akka流Source.queue接收数据

时间:2018-07-26 01:34:41

标签: scala akka akka-stream akka-http

我正在使用Akka流Source.queue作为websocket客户端的源。

使用kaka消费者API从具有10k条记录的kafka主题读取,并将其提供给缓冲区100k的Source.queue。

我正在使用BroardcastHub进行扇出。该websocket客户端没有获得任何数据,但是在报价结果上看到了来自kafka的记录。

感谢任何帮助。

def kafkaSourceQueue() = {

val sourceQueue = Source.queue[String](100000, OverflowStrategy.dropHead)
val (theQueue, queueSource)= sourceQueue.toMat(BroadcastHub.sink(bufferSize = 256))(Keep.both).run

val consumer = KafkaEventSource.initKafkaConsumer()
try {
    while (true) {
      val records = consumer.poll(polltimeout.toMillis)
      for (record <- records.records(topic)) {
        //println(record.value())

          theQueue.offer(record.value()).onComplete{
            case Success(QueueOfferResult.Enqueued) =>
              println("enqueued")
            case _ => println("Failed to enqueue")
          }
        }
    }
}
finally {
  if (consumer != null) {
    println("consumer unsubscribed")
    consumer.unsubscribe()
  }
}
queueSource
}

private def logicStreamFlow: Flow[String, String, NotUsed] = {
 Flow.fromSinkAndSourceCoupled(Sink.ignore, kafkaSourceQueue)
}

def websocketFlow: Flow[Message, Message, NotUsed] = {
 Flow[Message]
  .map{
    case TextMessage.Strict(msg) => msg

    case _ => throw new Exception("exception msg")
  }
  .via(logicStreamFlow)
  .map { msg: String => TextMessage.Strict(msg) }
}
lazy private val streamRoute =
 path("stream") {
     handleWebSocketMessages {
          websocketFlow          
       .watchTermination() { (_, done) =>
          done.onComplete {
          case Success(_) =>
            log.info("Stream route completed successfully")
          case Failure(ex) =>
          log.error(s"Stream route completed with failure : $ex")
       }
   }
 }
}

def startServer(): Unit = {
  bindingFuture = Http().bindAndHandle(wsRoutes, HOST, PORT)
  log.info(s"Server online at http://localhost:9000/")
}

def stopServer(): Unit = {
  bindingFuture
  .flatMap(_.unbind())
  .onComplete{
   _ => system.terminate()
  log.info("terminated")
  }
}   

0 个答案:

没有答案