示例#1
0
        private void HandleWork()
        {
            using (var stateUpdate = new SubscriberSocket())
            {
                stateUpdate.SubscribeToAnyTopic();
                stateUpdate.Bind(_configuration.ToPublisherEndpoint);

                using (var stateUpdatePublish = new PublisherSocket())
                {
                    stateUpdatePublish.Bind(_configuration.ToSubscribersEndpoint);

                    stateUpdate.ReceiveReady += async(s, e) =>
                    {
                        var message = e.Socket.ReceiveMultipartMessage();

                        var subject = message[0].ConvertToString();
                        var payload = message[1];

                        var eventId = await _cache.AppendToStream(subject, payload.Buffer);

                        stateUpdatePublish.SendMoreFrame(message[0].Buffer)
                        .SendMoreFrame(_serializer.Serialize(eventId))
                        .SendFrame(payload.Buffer);
                    };

                    using (_poller = new NetMQPoller {
                        stateUpdate, stateUpdatePublish
                    })
                    {
                        _poller.Run();
                    }
                }
            }
        }
        private void HandleWork()
        {
            using (var stateUpdate = new SubscriberSocket())
            {
                stateUpdate.SubscribeToAnyTopic();
                stateUpdate.Bind(_configuration.ToPublisherEndpoint);

                stateUpdate.Options.ReceiveHighWatermark = _configuration.ZmqHighWatermark;

                using (var stateUpdatePublish = new PublisherSocket())
                {
                    stateUpdatePublish.Bind(_configuration.ToSubscribersEndpoint);

                    stateUpdate.ReceiveReady += async(s, e) =>
                    {
                        try
                        {
                            NetMQMessage message = null;

                            while (e.Socket.TryReceiveMultipartMessage(ref message))
                            {
                                var subject = message[0];
                                var payload = message[1];

                                stateUpdatePublish.SendMoreFrame(subject.Buffer)
                                .SendMoreFrame(_serializer.Serialize(await _cache.AppendToStream(subject.Buffer, payload.Buffer)))
                                .SendFrame(payload.Buffer);
                            }
                        }
                        catch (Exception ex)
                        {
                            Errors.Add(new ActorMonitoringError()
                            {
                                CacheErrorStatus = ActorErrorType.DynamicCacheEventHandlingFailure,
                                Exception        = ex
                            });
                        }
                    };

                    using (_workPoller = new NetMQPoller {
                        stateUpdate, stateUpdatePublish
                    })
                    {
                        _workPoller.Run();
                    }
                }
            }
        }
示例#3
0
        private void SetupFakeBroker(
            CancellationToken cancel,
            IEventCache eventCache,
            bool useHeartbeat       = true,
            bool useEventLoop       = true,
            bool useStateOfTheWorld = true)
        {
            if (useHeartbeat)
            {
                //heartbeat
                Task.Run(() =>
                {
                    using (var heartbeatSocket = new ResponseSocket(HeartbeatEndpoint))
                    {
                        while (!cancel.IsCancellationRequested)
                        {
                            var hasResponse = heartbeatSocket.TryReceiveFrameBytes(TimeSpan.FromSeconds(1), out var messageBytes);

                            if (hasResponse)
                            {
                                heartbeatSocket.SendFrame(_serializer.Serialize(Heartbeat.Response));
                            }
                        }
                    }
                }, cancel);
            }

            if (useEventLoop)
            {
                //event loop
                Task.Run(async() =>
                {
                    using (var stateUpdate = new SubscriberSocket())
                    {
                        stateUpdate.SubscribeToAnyTopic();
                        stateUpdate.Bind(ToPublishersEndpoint);

                        while (!cancel.IsCancellationRequested)
                        {
                            NetMQMessage message = null;

                            var hasResponse = stateUpdate.TryReceiveMultipartMessage(TimeSpan.FromSeconds(1), ref message);

                            if (hasResponse)
                            {
                                var subject = message[0];
                                var payload = message[1];

                                await eventCache.AppendToStream(subject.Buffer, payload.Buffer);
                            }
                        }
                    }
                }, cancel);
            }

            if (useStateOfTheWorld)
            {
                //stateOfTheWorld
                Task.Run(async() =>
                {
                    using (var stateRequestSocket = new RouterSocket())
                    {
                        stateRequestSocket.Bind(StateOfTheWorldEndpoint);

                        while (!cancel.IsCancellationRequested)
                        {
                            NetMQMessage message = null;

                            var hasResponse = stateRequestSocket.TryReceiveMultipartMessage(TimeSpan.FromSeconds(1), ref message);

                            if (hasResponse)
                            {
                                var sender  = message[0].Buffer;
                                var request = _serializer.Deserialize <StateRequest>(message[1].Buffer);

                                var stream = await eventCache.GetStreamBySubject(request.Subject);

                                var response = new StateReply()
                                {
                                    Subject = request.Subject,
                                    Events  = stream.ToList()
                                };

                                stateRequestSocket.SendMoreFrame(sender)
                                .SendFrame(_serializer.Serialize(response));
                            }
                        }
                    }
                }, cancel);
            }
        }