Why is my mailBoxProcessor stuck at the receive method?

268 Views Asked by At

I am using F# mailBoxProcessor to asynchronously process messages received from multiple network ends.

The code works as expected until I added function call getTreasuryYield after inbox.receive().

It gets stuck every time at inbox.receive() after running for a few seconds.

GetTreasuryYield is a quite slow method since it involves database and IO operations, but I

still do not understand how it gets stuck.

Any HELP will be appreciated.

let start rvSetting (agent:Agent<Message>) messageSelector=
    try
        TIBCO.Rendezvous.Environment.Open()
        let _transport = new NetTransport(rvSetting.rvService, rvSetting.rvNetwork, rvSetting.rvDaemon)
        let _listener = new Listener(TIBCO.Rendezvous.Queue.Default, _transport, rvSetting.rvSubject, null)
        _listener.MessageReceived.Add(fun args->
            printfn "before sent"
            if messageSelector(args.Message) then 
                printfn "Message sent to agent: %A" args.Message
                agent.Post(args.Message))
        let rec dispatch() =
            async{
                try
                   TIBCO.Rendezvous.Queue.Default.Dispatch()
                    return! dispatch()
                with
                    | e -> _log.Error(e.ToString())   
           }
        Async.Start(dispatch())
    with
        |e -> printfn "%A" e.Message
              _log.Error(e.Message)
 let agent = new Agent<Message>(fun inbox ->
    let rec loop() =
        async{
            let! (m : Message) = inbox.Receive()
            // This line causes the problem
            printfn "%A" (getTreasuryYieldFromMessage m)
            Async.Start(treasuryAction m)
            return! loop()    
        }  
    loop())
agent.Error.Add raise
[<EntryPoint>]
let main argv =
    //start rvCorporate agent (fun x -> true)
    agent.Start()
start rvTreasury agent treasurySelector
    Console.ReadLine() |> ignore
    0
0

There are 0 best solutions below