Exemple #1
0
        public void StartAdapter()
        {
            management = new ServerManagement(connectionInfo.ClientName, connectionFactory.CreateConnection(connectionInfo.ClientName), connectionFactory.CreateConnection(connectionInfo.ClientName), serializer);

            //Create globale Error Exchange, if not exists
            GlobaleErrorExchange = management.CreateTopicExchange(connectionInfo.ExchangeName + "_GlobalErrorsExchange");

            //Assert Dead Letter Exchange and Queue for this consumer..
            var dle = management.CreateTopicExchange(connectionInfo.ClientName + "_DeadLetterExchange");

            dle.CreateAndBindQueue(connectionInfo.ClientName + "_DeadLetterQueue", "#");

            foreach (var bindingInfo in bindingInfos)
            {
                var theQueue = management.CreateQueueAndBind(bindingInfo.RoutingKey, connectionInfo.ExchangeName, dle.name).ConnectToExchange(connectionInfo.ExchangeName, bindingInfo.RoutingKey);

                IModel model = management.CreateChannel();
                model.BasicQos(0, 1, false);
                var consumer = new EventingBasicConsumer(model);
                channels.Add(model);

                if (bindingInfo.MessageType != null)
                {
                    consumer.Received += (a, theMessageRecieved) =>
                    {
                        var param = serializer.FromStream(new System.IO.MemoryStream(theMessageRecieved.Body), bindingInfo.MessageType);
                        try
                        {
                            OnMessage(param, bindingInfo.HandlerType, new RabbitRecievedMessageData(theMessageRecieved));
                            model.BasicAck(theMessageRecieved.DeliveryTag, false);
                        }
                        catch (Exception ex)
                        {
                            model.BasicNack(theMessageRecieved.DeliveryTag, false, false);
                            SendToError(theMessageRecieved);
                        }
                    };
                }
                else //Anonymouse handler.. Need to handle the
                {
                    consumer.Received += (a, theMessageRecieved) =>
                    {
                        var param = new RabbitRecievedMessageData(theMessageRecieved);
                        try
                        {
                            OnMessage(param, bindingInfo.HandlerType, new RabbitRecievedMessageData(theMessageRecieved));
                            model.BasicAck(theMessageRecieved.DeliveryTag, false);
                        }
                        catch (Exception ex)
                        {
                            model.BasicNack(theMessageRecieved.DeliveryTag, false, false);
                        }
                    };
                }
                model.BasicConsume(theQueue.name, false, consumer);
            }
        }
 public ExchangeInfo(ServerManagement management, string name)
 {
     this.management = management;
     this.name       = name;
 }