Beispiel #1
0
        private ManagedGroup CreateGroup(List <Car> cars)
        {
            var id        = _groups.Count;
            var queue     = new MessageQueue();
            var processor = _messageProcessorFactory.Create(queue);
            var thread    = CreateThread(processor, id);

            var group = new ManagedGroup
            {
                id               = id,
                Cars             = cars,
                MessageProcessor = processor,
                MessageQueue     = queue,
                Thread           = thread
            };

            _groups.Add(id, group);

            foreach (var car in cars)
            {
                _queueLookup.Add(car, queue);
            }

            return(group);
        }
Beispiel #2
0
        public void Subscribe <TMessage>(string queueName, TMessage messageType) where TMessage : class
        {
            var factory = new ConnectionFactory()
            {
                HostName = _options.Hostname, DispatchConsumersAsync = true
            };

            using (var connection = factory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    Console.WriteLine(" [*] Waiting for logs.");

                    var consumer = new AsyncEventingBasicConsumer(channel);
                    consumer.Received += async(model, ea) =>
                    {
                        var body    = ea.Body;
                        var message = Encoding.UTF8.GetString(body);

                        TMessage deserializedMessage = _serializer.Deserialize(message, messageType);

                        var messageProcessor         = _factory.Create <TMessage, string>(deserializedMessage);
                        var messageProcessorResponse = await messageProcessor.ProcessAsync(deserializedMessage);

                        var jsonMessage = _serializer.Serialize(messageProcessorResponse);

                        if (messageProcessorResponse.Successful)
                        {
                            //Reply to originator
                            var properties = ea.BasicProperties;
                            var replyTo    = properties.ReplyTo;
                            channel.BasicPublish(exchange: string.Empty,
                                                 routingKey: replyTo,
                                                 basicProperties: null,
                                                 body: ToBytes(jsonMessage));
                            Debug.WriteLine(" [x] {0}", message);
                            channel.BasicAck(ea.DeliveryTag, false);
                        }
                        else
                        {
                            Debug.WriteLine("Error trying to process your message. Will re-queue");
                            channel.BasicNack(ea.DeliveryTag, false, true);
                        }
                    };
                    channel.BasicConsume(queue: queueName,
                                         autoAck: false,
                                         consumer: consumer);

                    Console.WriteLine(" Press [enter] to exit.");
                    Console.ReadLine();
                }
        }
Beispiel #3
0
        public ExecutionResult Process(object message, Type messageType)
        {
            var unitOfWork = locator.GetInstance <IUnitOfWork>();
            IMessageProcessor processor = MessageProcessorFactory.Create(unitOfWork, MappingEngine, Configuration);

            return(processor.Process(message, messageType));
        }