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; }