Exemplo n.º 1
0
        public void EventPumpQueueLength(int numberOfProducers, bool producerDelay)
        {
            EventQueue q = new EventQueue();
            EventProducer[] producers = new EventProducer[numberOfProducers];
            for (int i = 0; i < numberOfProducers; i++)
            {
                producers[i] = new EventProducer(q, i, producerDelay);
            }

            using (EventPump pump = new EventPump(NullListener.NULL, q, false))
            {
                pump.Name = "EventPumpQueueLength";
                pump.Start();

                foreach (EventProducer p in producers)
                {
                    p.ProducerThread.Start();
                }
                foreach (EventProducer p in producers)
                {
                    p.ProducerThread.Join();
                }
                pump.Stop();
            }
            Assert.That(q.Count, Is.EqualTo(0));

            foreach (EventProducer p in producers)
            {
                Console.WriteLine(
                    "#Events: {0}, MaxQueueLength: {1}", p.SentEventsCount, p.MaxQueueLength);
                Assert.IsNull(p.Exception, "{0}", p.Exception);
            }
        }
Exemplo n.º 2
0
        public async Task <PatientDTO> Handle(CreatePatientCommand request, CancellationToken cancellationToken)
        {
            bool tableInsertStatus;

            try
            {
                _patient = await _patientRepository.AddAsync(request.PatientDTO);

                using (EventProducer eventProducer = _eventStore.CreateEventProducer())
                {
                    EventData eventData = eventProducer.CreateEventData(_patient, "Patient-Creation-Pending");
                    eventProducer.SendEvent($"Patient-{_patient.ID}", eventData);
                }
                tableInsertStatus = true;
            }
            catch (Exception)
            {
                tableInsertStatus = false;
            }
            if (tableInsertStatus == true)
            {
                try
                {
                    PatientMessage patientMessage = new PatientMessage("PatientCreated", _patient);
                    var            message        = await _client.SendObjectMessageAsync("EmailQueue", patientMessage);

                    using (EventProducer eventProducer = _eventStore.CreateEventProducer())
                    {
                        EventData eventData = eventProducer.CreateEventData(message, "PatientCreatedConfirmation-Sent");
                        eventProducer.SendEvent($"AMQMessages-Patient-{_patient.ID}", eventData);
                    }
                }
                catch (Exception)
                {
                    await _patientRepository.DeleteAsync(_patient.ID);

                    using (EventProducer eventProducer = _eventStore.CreateEventProducer())
                    {
                        EventData eventData = eventProducer.CreateEventData(_patient, "Patient-Creation-Rollback");
                        eventProducer.SendEvent($"Patient-{_patient.ID}", eventData);
                    }
                    tableInsertStatus = false;
                }
            }
            using (EventConsumer eventConsumer = _eventStore.CreateEventConsumer($"AMQMessages-Patient-{_patient.ID}", "EmailCheck"))
            {
                bool eventFound = false;
                while (eventFound == false)
                {
                    var events = await eventConsumer.ReadStreamEventsForwardAsync();

                    if (eventConsumer.FindEventType(events, "EmailConfirmation-Sent"))
                    {
                        eventFound       = true;
                        _workerSucceeded = true;
                    }
                    else if (eventConsumer.FindEventType(events, "EmailConfirmation-Failed"))
                    {
                        eventFound       = true;
                        _workerSucceeded = false;
                    }
                }
            }
            if (_workerSucceeded == false)
            {
                await _patientRepository.DeleteAsync(_patient.ID);

                using (EventProducer eventProducer = _eventStore.CreateEventProducer())
                {
                    EventData eventData = eventProducer.CreateEventData(_patient, "Patient-Creation-Rollback");
                    eventProducer.SendEvent($"Patient-{_patient.ID}", eventData);
                }
                tableInsertStatus = false;
            }
            if (tableInsertStatus == false)
            {
                return(null);
            }
            using (EventProducer eventProducer = _eventStore.CreateEventProducer())
            {
                EventData eventData = eventProducer.CreateEventData(_patient, "Patient-Creation-Committed");
                eventProducer.SendEvent($"Patient-{_patient.ID}", eventData);
            }
            return(_patient);
        }