Esempio n. 1
0
        public void Publish(T messageEntity)
        {
            var serializer = new Serializer <T>();
            var service    = new MessagingService();

            using (var connection = service.GetRabbitMqConnection())
            {
                var model = connection.CreateModel();
                service.SetUpExchangeAndQueues(model);
                var basicProperties = model.CreateBasicProperties();
                basicProperties.SetPersistent(true);
                var messageBytes = serializer.Serialize(messageEntity);
                model.BasicPublish(_exchangeName, "", basicProperties, messageBytes);
                _log.Info("Message has been published to Exchange " + _exchangeName);
            }
        }
Esempio n. 2
0
        /// <summary>
        ///Begin consuming messages from given queue Sync
        /// </summary>
        /// <param name="queue"></param>
        public void BeginConsumingFrom(string queue)
        {
            var serializer = new Serializer <T>();
            var service    = new MessagingService();

            using (var connection = service.GetRabbitMqConnection())
            {
                var model = connection.CreateModel();
                service.SetUpExchangeAndQueues(model);
                var subscription = new Subscription(model, queue, true);
                while (true)
                {
                    var message = subscription.Next();
                    var body    = message.Body;
                    var entity  = serializer.Deserialize(body);
                    OnMessageReceived(entity);
                    subscription.Ack(message);
                    _log.Info("Received Message from queue :" + queue + " message: " + body);
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Begin consuming messages from given queue Async
        /// </summary>
        /// <param name="queue"></param>
        /// <returns></returns>
        public async Task BeginConsumingFromAsync(string queue)
        {
            var serializer = new Serializer <T>();
            var service    = new MessagingService();

            using (var connection = service.GetRabbitMqConnection())
            {
                var model = connection.CreateModel();
                service.SetUpExchangeAndQueues(model);
                var subscription = new Subscription(model, queue, true);
                await Task.Factory.StartNew(() =>
                {
                    while (true)
                    {
                        var message = subscription.Next();
                        var body    = message.Body;
                        var entity  = serializer.Deserialize(body);
                        OnMessageReceived(entity);
                        subscription.Ack(message);
                    }
                });
            }
        }