I'm trying to implement with NetMQ the Shared Queue (DEALER and ROUTER Sockets) from ZeroMQ Examples. The code:
using NetMQ;using NetMQ.Sockets;using System;using System.Threading;namespace ZeroMQ.Examples{ static partial class Program { public static void RequestReplyClient(string[] args) { using (RequestSocket requester = new RequestSocket(">tcp://127.0.0.1:5559")) { for (int requestNumber = 0; requestNumber < 10; requestNumber++) { requester.SendFrame("Hello"); Thread.Sleep(6000); string requestText = requester.ReceiveFrameString(); Console.WriteLine($"received response {requestNumber} [{requestText}]"); } } NetMQConfig.Cleanup(); } }}using NetMQ;using NetMQ.Sockets;using System;using System.Threading;namespace ZeroMQ.Examples{ static partial class Program { public static void RequestReplyWorker(string[] args) { using (ResponseSocket responder = new ResponseSocket(">tcp://127.0.0.1:5560")) { while (true) { string responseText = responder.ReceiveFrameString(); Console.WriteLine($"received request: [{responseText}]"); Thread.Sleep(3000); responder.SendFrame("world"); } } NetMQConfig.Cleanup(); } }}using NetMQ;using NetMQ.Sockets;using System;using System.Threading.Tasks;namespace ZeroMQ.Examples{ static partial class Program { public static void RequestReplyBroker(string[] args) { using (RouterSocket frontend = new RouterSocket("@tcp://*:5559")) using (DealerSocket backend = new DealerSocket("@tcp://*:5560")) { using (NetMQPoller items = new NetMQPoller()) { items.Add(frontend); items.Add(backend); frontend.ReceiveReady += (s, a) => { string message = default(string); bool more = false; while (a.Socket.TryReceiveFrameString(TimeSpan.Zero, out message)) { more = a.Socket.HasIn; backend.SendFrame(message, more); Console.WriteLine($"{message} {more}"); if (!more) break; } }; backend.ReceiveReady += (s, a) => { string message = default(string); bool more = false; while (a.Socket.TryReceiveFrameString(TimeSpan.Zero, out message)) { more = a.Socket.HasIn; frontend.SendFrame(message, more); Console.WriteLine($"{message} {more}"); if (!more) break; } }; Task.Factory.StartNew(items.Run); Console.WriteLine("press any key to quit ..."); Console.ReadKey(true); items.Stop(); } } NetMQConfig.Cleanup(); } }}
Most of the time if breaks. But once in lets say 30 times it works as expected and I don't know what I do different.
I guess there is some kind of synchronization issue. The steps that works before freezing are the following:
- the RequestSocket from the client sends "hello" to RouterSocket from broker
- the message is then forwarded from RouterSocker to DealerSocker
- the message is then received by ResponseSocket from worker which replies with "word" to DealerSocket from broker
- the message is the forwarded from DealerSocket to RouterSocket
- everything freeze