gpt4 book ai didi

scala - 使用流程中的最新项目完成请求

转载 作者:行者123 更新时间:2023-12-02 02:53:23 30 4
gpt4 key购买 nike

我想用流程中的最新可用项完成 GET 请求。此流程特别聚合了由参与者生成并已由 WebSocket 单独使用的事件。

假设事件可以表示如下:

final case class Event(id: String, value: Double)

我做的第一件事是创建一个 SourceQueue,参与者将在其中推送事件和一个集线器,以便不同的客户端可以独立接收这些事件:

val (queue, hub) =
Source.queue[Event](256, OverflowStrategy.dropHead).
toMat(BroadcastHub.sink(bufferSize = 256))(Keep.both).run()

然后我可以创建一个 actor,它可以将事件推送到 queue 并将 hub 传递给通过 WebSocket 为事件提供服务的服务:

extractUpgradeToWebSocket { upgrade =>
complete(upgrade.handleMessagesWithSinkSource(
inSink = Sink.ignore,
outSource =
hub.map(in => TextMessage(fmt.write(in).toString()))
))
}

这很好用,同时也适用于多个消费者。

接下来我想做的是拥有一个服务,该服务使用来自 hub 的事件并为每个 id 生成最新事件列表,通过 GET 为它提供服务端点。

我尝试了几种方法来解决这个问题。我尝试的两种方法是:

  • 运行更新私有(private)变量的流程
  • 完成一个返回last元素的sink

运行更新私有(private)变量的流程

实际上,这是我尝试过的最后一种方法。奇怪的(或者是吗?)认为我注意到实际上没有记录任何东西(不应该记录通过 log 组合器的东西吗?)。

使用此方法的结果是 latest 始终为 null,因此响应始终为空。

final class Service(hub: Source[Event, NotUsed])(implicit s: ActorSystem, m: ActorMaterializer, t: Timeout) extends Directives with JsonSupport {

implicit private val executor = system.dispatcher

@volatile private[this] var latest: List[Event] = _

hub.
log("hub", identity).
groupBy(Int.MaxValue, { case Event(id, _) => id }).
map { case event @ Event(id, _) => Map(id -> event) }.
reduce(_ ++ _).
mergeSubstreams.
map(_.values.toList).
toMat(Sink.foreach(latest = _))(Keep.none).run()

val definition = get { complete(Option(latest)) }

}

我还尝试了一种类似的方法,即使用“盒子”actor 并将聚合通过管道传递给它,但效果是一样的。

完成一个返回last元素的sink

这是我尝试采用的第一种方法。结果是响应挂起,直到达到超时并且 Akka HTTP 向浏览器返回 500。

final class Service(hub: Source[Event, NotUsed])(implicit s: ActorSystem, m: ActorMaterializer, t: Timeout) extends Directives with JsonSupport {

implicit private val executor = system.dispatcher
private[this] val currentLocations =
hub.
groupBy(Int.MaxValue, { case Event(id, _) => id }).
map { case event @ Event(id, _) => Map(id -> event) }.
reduce(_ ++ _).
mergeSubstreams.
map(_.values.toList).
runWith(Sink.reduce((_, next) => next))

val definition = get { complete(currentLocations) }

}

最佳答案

ActorRef 作为接收器

您可以创建一个 Actor 来保持 idEvent 的运行 Map:

import scala.collection.immutable

object QueryMap

class MapKeeperActor() extends Actor {

var internalMap = immutable.Map.empty[String, Event]

override def receive = {
case e : Event => internalMap = internalMap + (e.id -> e)
case _ : QueryMap => sender ! internalMap
}
}

此 ref 然后可以在 Sink 中使用,该 Sink 将附加到 BroadcastHub:

object OnCompleteMessage

val system : ActorSystem = ???

val mapKeeperRef = system.actorOf(Props[MapKeeperActor])

val mapKeeperSink : Sink[Event, _] = Sink.actorRef[Event](mapKeeperRef, OnCompleteMessage)

查询路由中的 Actor

我们现在可以创建一个Route,它将使用指令查询 map 管理员。但是,您必须决定如何将 Map 序列化为 HttpResponseResponseEntity:

val serializeMap : Map[String, Event] => ResponseEntity = ???

val route =
get {
onComplete( (mapKeeperRef ? QueryMap).mapTo[Map[String, Event]]) {
case Success(map) => complete(HttpResponse(entity=serializeMap(map))
case Failure(ex) => complete((InternalServerError, s"An error occurred: ${ex.getMessage}"))
}
}

关于scala - 使用流程中的最新项目完成请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50888121/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com