private string blockingRead(LoanBroker.model.LoanRequest loanRequest)
        {
            string returnString = "Could not send the message";
            var    factory      = new ConnectionFactory()
            {
                HostName = Queues.RABBITMQ_HOSTNAME
            };

            using (var connection = factory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    channel.QueueDeclare(queue: Queues.LOANBROKER_OUT,
                                         durable: false,
                                         exclusive: false,
                                         autoDelete: false,
                                         arguments: null);

                    channel.BasicQos(0, 1, false); // Get one at the time.

                    var consumer = new QueueingBasicConsumer(channel);
                    channel.BasicConsume(queue: Queues.LOANBROKER_OUT,
                                         noAck: false,
                                         consumer: consumer);
                    bool weDontHaveIt = true;
                    using (Timer _timeOutTimer = new Timer(TIMEOUT))
                    {
                        _timeOutTimer.Enabled  = true;
                        _timeOutTimer.Elapsed += _timeOutTimer_Elapsed;

                        while (weDontHaveIt)
                        {
                            //Looks like the items stays in the queue
                            BasicDeliverEventArgs         ea           = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
                            LoanBroker.model.LoanResponse loanResponse = JsonConvert.DeserializeObject <LoanBroker.model.LoanResponse>(Encoding.UTF8.GetString(ea.Body));
                            if (loanRequest.SSN == loanResponse.SSN)
                            {
                                weDontHaveIt = false;
                                returnString = JsonConvert.SerializeObject(loanResponse);
                                channel.BasicAck(ea.DeliveryTag, false);
                            }
                            else
                            {
                                // Return it to the queue
                                // what is the difference between true and false?
                                //channel.BasicReject(ea.DeliveryTag, false);
                                //channel.BasicReject(ea.DeliveryTag, true);
                            }
                        }
                    }
                }
            }
            return(returnString);
        }
        private string nonBlockingRead(LoanBroker.model.LoanRequest loanRequest)
        {
            string returnString = "Could not send the message";

            EventingBasicConsumer consumer;

            using (IModel channel = new ConnectionFactory()
            {
                HostName = Queues.RABBITMQ_HOSTNAME
            }.CreateConnection().CreateModel())
            {
                channel.QueueDeclare(queue: Queues.LOANBROKER_OUT,
                                     durable: false,
                                     exclusive: false,
                                     autoDelete: false,
                                     arguments: null);

                consumer = new EventingBasicConsumer(channel);

                bool weDontHaveIt = true;

                consumer.Received += (model, ea) =>
                {
                    LoanBroker.model.LoanResponse loanResponse = JsonConvert.DeserializeObject <LoanBroker.model.LoanResponse>(Encoding.UTF8.GetString(ea.Body));
                    if (loanResponse.SSN == loanRequest.SSN)
                    {
                        weDontHaveIt = false;
                    }
                    else
                    {
                        // Return it to the queue
                        channel.BasicReject(ea.DeliveryTag, true);
                    }
                };
                channel.BasicConsume(queue: Queues.LOANBROKER_OUT,
                                     noAck: true,
                                     consumer: consumer);

                while (weDontHaveIt)
                {
                    System.Threading.Thread.Sleep(5);
                }
            }
            return(returnString);
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            Console.Title = "RabbitMQBank";
            Console.SetWindowPosition(0, 0);
            Console.SetWindowSize(80, 5);
            Console.WriteLine("<--Listening for messages on queue: " + Queues.RABBITMQOURBANK_IN);
            HandleMessaging.RecieveMessage(Queues.RABBITMQOURBANK_IN, (object model, BasicDeliverEventArgs ea) =>
            {
                Console.WriteLine("<--Message recieved on queue: " + Queues.RABBITMQOURBANK_IN);

                byte[] inBody = ea.Body;
                string message = Encoding.UTF8.GetString(inBody);

                Console.WriteLine("<--Message content:");
                Console.WriteLine("<--" + message);

                //SSN;CreditScore;Amount;Duration
                message = message.Replace("\"", "");
                string[] parts = message.Split(';');
                string ssn = parts[0];
                int creditScore = 0;
                int.TryParse(parts[1], out creditScore);
                decimal amount = 0;
                decimal.TryParse(parts[2], out amount);
                int duration = 0;
                int.TryParse(parts[3], out duration);

                LoanBroker.model.LoanResponse loanResponse = new LoanBroker.model.LoanResponse()
                {
                    InterestRate = BankingUtility.ProcessLoanRequest(ssn, creditScore, amount, duration),
                    BankName = "Our RabbitMQ Bank",
                    SSN = ssn
                };

                //decimal sendMessage = BankingUtility.ProcessLoanRequest(ssn, creditScore, amount, duration);

                string msg = JsonConvert.SerializeObject(loanResponse);

                Console.WriteLine("<--Sending message on queue: " + Queues.RABBITMQOURBANK_OUT + " > " + msg);
                Console.WriteLine();

                HandleMessaging.SendMessage<LoanBroker.model.LoanResponse>(Queues.RABBITMQOURBANK_OUT, loanResponse);
            });
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            Console.Title = "RabbitMQBank";
            Console.SetWindowPosition(0, 0);
            Console.SetWindowSize(80, 5);
            Console.WriteLine("<--Listening for messages on queue: " + Queues.RABBITMQOURBANK_IN);
            HandleMessaging.RecieveMessage(Queues.RABBITMQOURBANK_IN, (object model, BasicDeliverEventArgs ea) =>
            {
                Console.WriteLine("<--Message recieved on queue: " + Queues.RABBITMQOURBANK_IN);

                byte[] inBody  = ea.Body;
                string message = Encoding.UTF8.GetString(inBody);

                Console.WriteLine("<--Message content:");
                Console.WriteLine("<--" + message);

                //SSN;CreditScore;Amount;Duration
                message         = message.Replace("\"", "");
                string[] parts  = message.Split(';');
                string ssn      = parts[0];
                int creditScore = 0;
                int.TryParse(parts[1], out creditScore);
                decimal amount = 0;
                decimal.TryParse(parts[2], out amount);
                int duration = 0;
                int.TryParse(parts[3], out duration);

                LoanBroker.model.LoanResponse loanResponse = new LoanBroker.model.LoanResponse()
                {
                    InterestRate = BankingUtility.ProcessLoanRequest(ssn, creditScore, amount, duration),
                    BankName     = "Our RabbitMQ Bank",
                    SSN          = ssn
                };

                //decimal sendMessage = BankingUtility.ProcessLoanRequest(ssn, creditScore, amount, duration);

                string msg = JsonConvert.SerializeObject(loanResponse);

                Console.WriteLine("<--Sending message on queue: " + Queues.RABBITMQOURBANK_OUT + " > " + msg);
                Console.WriteLine();

                HandleMessaging.SendMessage <LoanBroker.model.LoanResponse>(Queues.RABBITMQOURBANK_OUT, loanResponse);
            });
        }