A shared queue that collects requests from a set of clients and distributes these fairly among a set of services.
Requests are fair-queued from frontend connections and load-balanced between backend connections. Replies automatically return to the client that made the original request. This device is part of the request-reply pattern. The frontend speaks to clients and the backend speaks to services.
Inheritance: DeviceBase
Exemple #1
0
        private static void Main()
        {
            Console.Title = "NetMQ Multi-threaded Service";

            var queue = new QueueDevice("tcp://localhost:5555", "tcp://localhost:5556", DeviceMode.Threaded);

            var source = new CancellationTokenSource();
            s_token = source.Token;

            for (int threadId = 0; threadId < 10; threadId++)
                Task.Factory.StartNew(WorkerRoutine, s_token);

            queue.Start();

            var clientThreads = new List<Task>();
            for (int threadId = 0; threadId < 1000; threadId++)
            {
                int id = threadId;
                clientThreads.Add(Task.Factory.StartNew(() => ClientRoutine(id)));
            }

            Task.WaitAll(clientThreads.ToArray());

            source.Cancel();

            queue.Stop();

            Console.WriteLine("Press ENTER to exit...");
            Console.ReadLine();
        }
Exemple #2
0
       public void CallMethod_Using_NProxyWrapper_ReadMessageWithRawActor()
       {
            waitHandle.Reset();
            using (var context = NetMQContext.Create())
            {
                using (var exchange = new Exchange(context))
                {
                    exchange.Start();
                    
                    var queueDevice = new QueueDevice(
                    context,
                    Pipe.PubSubControlBackAddressServer,
                    Pipe.PubSubControlFrontAddressServer,
                    DeviceMode.Threaded);
                    queueDevice.Start();

                    Thread.Sleep(200);

                    var task = Task.Run(() =>
                    {
                        return RunSubscriber(context);
                    });

                    using (var actor = new Actor(context, new BinarySerializer()))
                    {
                        using (var syncService = context.CreateResponseSocket())
                        {
                            syncService.Connect(Pipe.PubSubControlFrontAddressClient);
                            for (int i = 0; i < 1; i++)
                            {
                                syncService.Receive();
                                syncService.Send(string.Empty);
                            }

                            var order = actor.CreateInstance<IOrder>(typeof(Order));
                            Assert.IsInstanceOfType(order, typeof(IOrder));
                            order.UpdateDescription("XXX"); //called without exception    
                            waitHandle.WaitOne();

                            var netMqMessage = new NetMQMessage();
                            netMqMessage.Append(new NetMQFrame(string.Empty));
                            netMqMessage.Append(new NetMQFrame("shutdownallactors"));
                            actor.OutputChannel.SendMessage(netMqMessage);

                            //actor.SendKillSignal(actor.Serializer, actor.OutputChannel, string.Empty);
                        }
                    }

                    Thread.Sleep(200);

                    queueDevice.Stop(true);
                    exchange.Stop(true);
                }
            }
        }
Exemple #3
0
        static void Main(string[] args)
        {
            using (var context = NetMQContext.Create())
            {
                //var queue = new QueueDevice(context, "tcp://localhost:5555", "inproc://workers", DeviceMode.Threaded);
                var queue = new QueueDevice(context, "tcp://localhost:5555", "tcp://localhost:5556", DeviceMode.Threaded);

                CancellationTokenSource source = new CancellationTokenSource();
                token = source.Token;

                List<Task> workerThreads = new List<Task>();
                for (int threadId = 0; threadId < 10; threadId++)
                {
                    NetMQContext ctx = context;
                    workerThreads.Add(Task.Factory.StartNew(() => WorkerRoutine(new Worker(Guid.NewGuid(), ctx)), token));
                }

                queue.Start();

                List<Task> clientThreads = new List<Task>();
                for (int threadId = 0; threadId < 1000; threadId++)
                {
                    int id = threadId;
                    clientThreads.Add(Task.Factory.StartNew(() => ClientRoutine(id)));
                }

                Task.WaitAll(clientThreads.ToArray());

                source.Cancel();

                queue.Stop();
            }

            Console.WriteLine("Press ENTER to exit...");
            Console.ReadLine();
        }
Exemple #4
0
        public void Start(NetMQContext context)
        {
            this.SetUpMonitorChannel(context);
            this.SetUpAddSubscriberCountChannel(context);

            //this should work but the forwarder device appears to be broken - it does not use XSUb and XPUB sockets
            //forwarderDevice = new ForwarderDevice(context, PublishAddressServer, SubscribeAddressServer, DeviceMode.Threaded);
            //forwarderDevice.FrontendSetup.Subscribe(string.Empty);
            //forwarderDevice.Start();
            //while (!forwarderDevice.IsRunning)
            //{ }

            QueueDevce = new QueueDevice(context, PubSubControlBackAddressServer, PubSubControlFrontAddressServer, DeviceMode.Threaded);
            QueueDevce.Start();
            //while (!QueueDevce.IsRunning)
            //{
            //}

            this.Writeline("Control channel started");

            long count = 0;
            this.cancellationTokenSource = new CancellationTokenSource();
            var token = this.cancellationTokenSource.Token;
            Task.Run(() =>
            {
                using (frontend = context.CreateXSubscriberSocket())
                {
                    using (backend = context.CreateXPublisherSocket())
                    {
                        frontend.Bind(Pipe.PublishAddressServer); ////"tcp://*:5550");
                        backend.Bind(Pipe.SubscribeAddressServer); ////"tcp://*:5553");
                       // frontend.ReceiveReady += frontend_ReceiveReady;
                        frontend.ReceiveReady += new EventHandler<NetMQSocketEventArgs>(FrontendReceiveReady);
                        backend.ReceiveReady += new EventHandler<NetMQSocketEventArgs>(BackendReceiveReady);
                       // this.AddSubscriberCountChannel.ReceiveReady += new EventHandler<NetMQSocketEventArgs>(AddSubscriberCountChannelReceiveReady);
                        using (this.poller = new Poller(new NetMQSocket[] { frontend, backend, this.AddSubscriberCountChannel }))
                        {
                            Writeline("About to start polling");

                            while (true)
                            {
                                poller.Start(); // .PollOnce(); .Poll(new TimeSpan(0,0,0,0,5));
                                Writeline("polling" + count);
                                count++;
                                if (token.IsCancellationRequested)
                                {
                                    Writeline("break");
                                    break;
                                }
                            }
                        }

                        Writeline("stopped polling and exiting");
                    }
                }
            },
            token);
        }