我正在使用EasyNetQ来使用f#来管理RabbitMq Messenger总线,这很简单,它发布了消息:
let HandleBusResponse (data:BaseFrame) (bus:IBus) : ISubscriptionResult =
let handler = Action<RequestMessage>(fun request ->
let cabinetSubscribeId = sprintf "Project.%i" request.ProjectId
match request.FrameType with
| FrameType.UNDEFINED ->
let response = ResponseService.BusRequestFactory request data
| _ ->
let response = ResponseService.BusRequestFactory request data
bus.Publish<ResponseMessage>(response, cabinetSubscribeId))
此方法(订户)在应用程序的某处被调用,例如:
HandleBusResponse data bus
//WAIT FOR MESSAGE ARRIVAL
informationPools.AddToConnectionPool(data.ProjectId, client) |> ignore
ClientInfoHandler client data |> Async.RunSynchronously|> ignore
基本上与c#中的工作方式相同。
现在此代码已超出订阅处理程序,我的问题是有可能从此部分进行检测
//等待消息到达
是否需要一些时间(如果有新消息到达)(还记得第二个代码段在订阅处理程序之外)?
答案 0 :(得分:1)
如果您可以发布一个完整的最小示例,该示例在F#Interactive中进行了编译和工作,则可以更轻松地直接为您的问题实施解决方案。但是,通常应该使用的一种解决方案是等待事件。在F#中,您可以创建如下事件:
let requestArrived = Event<RequestMessage>()
let responsePublished = Event<ResponseMessage>()
然后,您可以异步等待这些事件并处理它们产生的数据:
let rec messageLoop<'message> f (event: IEvent<'message>) =
async {
let! message = event |> Async.AwaitEvent
message |> f
return! messageLoop f event
}
因此,在您的// WAIT FOR MESSAGE ARRIVAL
块中,您可以执行以下操作(取决于您是要处理请求还是响应,还是要处理它们:
requestArrived.Publish |> messageLoop (fun request -> printfn "Received Request: %A" request)
// AND/OR
responsePublished.Publish |> messageLoop (fun response -> printfn "Sent Response: %A" response)
然后,您可以更新处理程序以按要求触发事件:
let handler = Action<RequestMessage>(fun request ->
requestArrived.Trigger(request)
let cabinetSubscribeId = sprintf "Project.%i" request.ProjectId
let response =
match request.FrameType with
| FrameType.UNDEFINED ->
ResponseService.BusRequestFactory request data
| _ ->
ResponseService.BusRequestFactory request data
bus.Publish<ResponseMessage>(response, cabinetSubscribeId)
responsePublished.Trigger(response))