public void Start() { if (!string.IsNullOrWhiteSpace(_PubEndPoint)) { ZmqEventPublisher = ZeroMessageQueue.ZmqContext.CreateSocket(SocketType.PUB); ZmqEventPublisher.Bind(_PubEndPoint); using (var messageStore = IoCFactory.Resolve <IMessageStore>()) { messageStore.GetAllUnPublishedEvents() .ForEach(eventContext => MessageQueue.Add(eventContext)); } _WorkTask = Task.Factory.StartNew(PublishEvent, TaskCreationOptions.LongRunning); } }
public void Stop() { if (_WorkTask != null) { MessageQueue.CompleteAdding(); if (_WorkTask.Wait(2000)) { ZmqEventPublisher.Close(); _WorkTask.Dispose(); } else { _Logger.ErrorFormat("consumer can't be stopped!"); } } }
void PublishEvent() { try { while (true) { var eventContext = MessageQueue.Take(); ZmqEventPublisher.Send(eventContext.ToJson(), Encoding.UTF8); Task.Factory.StartNew(() => { using (var messageStore = IoCFactory.Resolve <IMessageStore>()) { messageStore.RemovePublishedEvent(eventContext.MessageID); } }); } } catch (Exception ex) { _Logger.Debug("end publish working", ex); } }