Exemplo n.º 1
0
        public static EventingBasicConsumer CreateConsumer(IModel channel, IModel resultChannel)
        {
            var consumer = new EventingBasicConsumer(channel);

            consumer.Received += (model, ea) =>
            {
                if (Encoding.UTF8.GetString((Byte[])ea.BasicProperties.Headers["language"]) != null)
                {
                    var language = (Encoding.UTF8.GetString((Byte[])ea.BasicProperties.Headers["language"])).ToLower();

                    var     body           = ea.Body;
                    string  message        = Encoding.UTF8.GetString(body);
                    JObject messageJObject = new JObject();

                    if (language.Equals(LANGUAGEJSON))
                    {
                        messageJObject = JObject.Parse(message);
                        Console.WriteLine("Received: {0}", messageJObject.ToString());
                        messageJObject.Add("bankName", JSONBANK);
                    }
                    else if (language.Equals(LANGUAGEXML))
                    {
                        try
                        {
                            XmlSerializer serializer = new XmlSerializer(typeof(LoanResponse));
                            MemoryStream  memStream  = new MemoryStream(Encoding.UTF8.GetBytes(message));
                            LoanResponse  tempLR     = (LoanResponse)serializer.Deserialize(memStream);
                            messageJObject = JObject.FromObject(tempLR);

                            Console.WriteLine("Received: {0}", messageJObject.ToString());
                            messageJObject.Add("bankName", XMLBANK);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("Message was: \n");
                            Console.WriteLine(message + "\n");
                            Console.WriteLine("ERROR: \n");
                            Console.WriteLine(e.Message + "\n");

                            Console.WriteLine("Header was: " + Encoding.UTF8.GetString((Byte[])ea.BasicProperties.Headers["language"]) + "\n");
                        }
                    }
                    else if (language.Equals(LANGUAGESTRING))
                    {
                        string ssn          = message.Split(',')[0].Split(':')[1];
                        string interestRate = message.Split(',')[1].Split(':')[1];
                        Console.WriteLine("Received: ssn: {0} : interestRate: {1}", ssn, interestRate);
                        messageJObject.Add("ssn", ssn);
                        messageJObject.Add("interestRate", interestRate);
                        messageJObject.Add("bankName", LAURBAERBANK);
                    }
                    else
                    {
                        Console.WriteLine("Language-Header was not null, but not correct either. \nHeader was:" + Encoding.UTF8.GetString((Byte[])ea.BasicProperties.Headers["language"]));
                    }
                    var resultBody = Encoding.UTF8.GetBytes(messageJObject.ToString());

                    Console.WriteLine("Header was: " + Encoding.UTF8.GetString((Byte[])ea.BasicProperties.Headers["language"]) + "\n");
                    resultChannel.BasicPublish(exchange: RESULTEXCHANGE, routingKey: RESULTROUTINGKEY, basicProperties: ea.BasicProperties, body: resultBody);
                }
                else
                {
                    Console.WriteLine("Language-Header was null");
                }
            };
            return(consumer);
        }
Exemplo n.º 2
0
        public void StartReceiving()
        {
            LoanResponse loanResponse;
            //Setup the consumer and where sets what queues it should consume from
            var consumer = new EventingBasicConsumer(rabbitConn.Channel);

            //We set noAck to false so consumer would not acknowledge that you have received and processed the message and remove it from the queue;
            //This is done to ensure that the message has been processed before it is remove from the queue it is consumed from.
            //this way, if the program has a breakdown, the message will still be on the queue and another can consume the message and process it.
            rabbitConn.Channel.BasicConsume(queue: "groupB.json.bank.reply",
                                            noAck: false,
                                            consumer: consumer);
            rabbitConn.Channel.BasicConsume(queue: "groupB.xml.bank.reply",
                                            noAck: false,
                                            consumer: consumer);
            rabbitConn.Channel.BasicConsume(queue: "groupB.our.bank.reply",
                                            noAck: false,
                                            consumer: consumer);
            rabbitConn.Channel.BasicConsume(queue: "groupB.web.bank.reply",
                                            noAck: false,
                                            consumer: consumer);

            //If a message is detected then it consumes it, and process it
            consumer.Received += (model, ea) =>
            {
                //get the header and body of the message
                var body   = ea.Body;
                var header = ea.BasicProperties;

                //we find what bank the message is from out from the queue name(in RabiitMQ default messaging is the RoutingKey equal to the queue name)
                string[] values = ea.RoutingKey.Split('.');

                //we know that the queue name follows this format "groupB.bankname.bank.reply" so the bank name is value 1 in the array
                switch (values[1])
                {
                case "json":
                    //we have a try catch in case, that the message cannot be Deserialized, then a error message is send to the Deadletter queue
                    //this will prevent the program from crashing.
                    try
                    {
                        loanResponse = JsonConvert.DeserializeObject <LoanResponse>(Encoding.UTF8.GetString(body));
                        Console.WriteLine();
                        Console.WriteLine(" [x] Received {0}", loanResponse.ToString());
                        loanResponse.BankName = values[1];
                        rabbitConn.Send(loanResponse.ToString(), sendToQueueName, header, false);
                    }
                    catch (Exception)
                    {
                        errorMsg = "Normalizer Error: Deserializing of" + values[1] + " bank message failed, message send:" + Encoding.UTF8.GetString(body);
                        rabbitConn.Send(errorMsg, "groupB.deadletter", header, false);
                        Console.WriteLine();
                        Console.WriteLine("Error message send to deadletter queue: " + errorMsg);
                    }

                    break;

                case "xml":
                    try
                    {
                        TextReader reader = new StringReader(Encoding.UTF8.GetString(body));
                        loanResponse = (LoanResponse)myXmlSerializer.Deserialize(reader);
                        Console.WriteLine();
                        Console.WriteLine(" [x] Received {0}", loanResponse.ToString());
                        loanResponse.BankName = values[1];
                        rabbitConn.Send(loanResponse.ToString(), sendToQueueName, header, false);
                    }
                    catch (Exception)
                    {
                        errorMsg = "Normalizer Error: Deserializing of" + values[1] + " bank message failed, message send: " + Encoding.UTF8.GetString(body);
                        rabbitConn.Send(errorMsg, "groupB.deadletter", header, false);
                        Console.WriteLine();
                        Console.WriteLine("Error message send to deadletter queue: " + errorMsg);
                    }

                    break;

                case "web":
                    try
                    {
                        loanResponse = JsonConvert.DeserializeObject <LoanResponse>(Encoding.UTF8.GetString(body));
                        Console.WriteLine();
                        Console.WriteLine(" [x] Received {0}", loanResponse.ToString());
                        loanResponse.BankName = values[1];
                        rabbitConn.Send(loanResponse.ToString(), sendToQueueName, header, false);
                    }
                    catch (Exception)
                    {
                        errorMsg = "Normalizer Error: Deserializing of" + values[1] + " bank message failed, message send: " + Encoding.UTF8.GetString(body);
                        rabbitConn.Send(errorMsg, "groupB.deadletter", header, false);
                        Console.WriteLine();
                        Console.WriteLine("Error message send to deadletter queue: " + errorMsg);
                    }

                    break;

                case "our":
                    try
                    {
                        string   reply      = Encoding.UTF8.GetString(body);
                        string[] replyArray = reply.Split('*');
                        loanResponse              = new LoanResponse();
                        loanResponse.SSN          = replyArray[1];
                        loanResponse.InterestRate = Double.Parse(replyArray[2]);
                        Console.WriteLine();
                        Console.WriteLine(" [x] Received {0}", loanResponse.ToString());
                        loanResponse.BankName = values[1];
                        rabbitConn.Send(loanResponse.ToString(), sendToQueueName, header, false);
                    }
                    catch (Exception)
                    {
                        errorMsg = "Normalizer Error: Deserializing of" + values[1] + " bank message failed, message send: " + Encoding.UTF8.GetString(body);
                        rabbitConn.Send(errorMsg, "groupB.deadletter", header, false);
                        Console.WriteLine();
                        Console.WriteLine("Error message send to deadletter queue: " + errorMsg);
                    }

                    break;

                default:
                    errorMsg = "Normalizer Error: do not know a bank named" + values[1] + ", message send: " + ea.RoutingKey;
                    rabbitConn.Send(errorMsg, "groupB.deadletter", header, false);
                    Console.WriteLine();
                    Console.WriteLine("Error message send to deadletter queue: " + errorMsg);
                    break;
                }

                //Acknowledge that the message has been received and processed, then release the message from the queue, allowing us to take in the next message
                rabbitConn.Channel.BasicAck(ea.DeliveryTag, false);
            };
        }