private void HandleWork()
        {
            using (_cacheUpdateSocket = new SubscriberSocket())
            {
                _cacheUpdateSocket.Options.ReceiveHighWatermark = _configuration.ZmqHighWatermark;

                _cacheUpdateSocket.Subscribe(_configuration.Subject);
                _cacheUpdateSocket.Connect(_configuration.SubscriptionEndpoint);

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

                    var hasMessage = _cacheUpdateSocket.TryReceiveMultipartMessage(_configuration.IsStaleTimeout, ref message);

                    if (_cancel.IsCancellationRequested)
                    {
                        return;
                    }

                    if (hasMessage)
                    {
                        var eventIdBytes      = message[1].Buffer;
                        var eventMessageBytes = message[2].Buffer;

                        var eventId         = _eventSerializer.Serializer.Deserialize <IEventId>(eventIdBytes);
                        var producerMessage = _eventSerializer.Serializer.Deserialize <IProducerMessage>(eventMessageBytes);

                        var @event = _eventSerializer.ToEvent <TKey, TAggregate>(eventId, producerMessage);

                        OnEventReceived(@event);
                    }
                    else
                    {
                        _state.OnNext(DynamicCacheState.Staled);
                    }
                }
            }
        }
        private void HandleWork()
        {
            using (var cacheUpdateSocket = new SubscriberSocket())
            {
                cacheUpdateSocket.Options.ReceiveHighWatermark = _configuration.ZmqHighWatermark;
                cacheUpdateSocket.Subscribe(_configuration.Subject);
                cacheUpdateSocket.Connect(_configuration.SubscriptionEndpoint);

                bool isSocketActive = false;

                var isStaleTimer = new NetMQTimer(_configuration.IsStaleTimeout);

                using (_cacheUpdatePoller = new NetMQPoller {
                    cacheUpdateSocket, isStaleTimer
                })
                {
                    isStaleTimer.Elapsed += (s, e) =>
                    {
                        //if no activity and state is not staled, set as staled
                        if (!isSocketActive && !IsStaled)
                        {
                            _isStaled.OnNext(true);
                        }

                        isSocketActive = false;
                    };

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

                            var events = new List <IEvent <TKey, TAggregate> >();

                            while (e.Socket.TryReceiveMultipartMessage(ref message))
                            {
                                if (_cancel.IsCancellationRequested)
                                {
                                    return;
                                }

                                var eventId         = _eventSerializer.Serializer.Deserialize <EventId>(message[1].Buffer);
                                var producerMessage = _eventSerializer.Serializer.Deserialize <ProducerMessage>(message[2].Buffer);

                                var @event = _eventSerializer.ToEvent <TKey, TAggregate>(eventId, producerMessage);

                                if (_configuration.UseEventBatching)
                                {
                                    events.Add(@event);
                                }
                                else
                                {
                                    OnEventReceived(@event);
                                }

                                isSocketActive = true;

                                if (IsStaled)
                                {
                                    _isStaled.OnNext(false);
                                }
                            }

                            if (_configuration.UseEventBatching)
                            {
                                OnBatchEventReceived(events);
                                events.Clear();
                            }
                        }
                        catch (Exception ex)
                        {
                            _cacheErrors.Add(new ActorMonitoringError()
                            {
                                CacheErrorStatus = ActorErrorType.DynamicCacheEventHandlingFailure,
                                Exception        = ex
                            });
                        }
                    };


                    _cacheUpdatePoller.Run();
                }
            }
        }