Exemplo n.º 1
0
    public string MakePayment(CardPayment payment)
    {
        replyQueueName = channel.QueueDeclare("rpc_reply", true, false, false, null).QueueName;

        props = channel.CreateBasicProperties();

        var correlationId = Guid.NewGuid().ToString();

        props.CorrelationId = correlationId;
        props.ReplyTo       = replyQueueName;

        consumer.Received += (model, ea) =>
        {
            var body     = ea.Body.ToArray();
            var response = Encoding.UTF8.GetString(body);
            if (ea.BasicProperties.CorrelationId == correlationId)
            {
                respQueue.Add(response);
            }
        };

        channel.BasicPublish(
            exchange: "",
            routingKey: "rpc_queue",
            basicProperties: props,
            body: payment.Serialize());

        channel.BasicConsume(
            consumer: consumer,
            queue: replyQueueName,
            autoAck: true);

        return(respQueue.Take());
    }
Exemplo n.º 2
0
        public string MakePayment(CardPayment payment)
        {
            _corrId = Guid.NewGuid().ToString();
            var props = _channel.CreateBasicProperties();

            props.ReplyTo       = _replyQueueName;
            props.CorrelationId = _corrId;

            _channel.BasicPublish("", "rpc_queue", props, payment.Serialize());

            consumer.Received += Consumer_Received;

            while (true)
            {
                consumer.Received += Consumer_Received;

                //var ea = _consumer.Queue.Dequeue();

                //if (ea.BasicProperties.CorrelationId != corrId) continue;

                //var authCode = Encoding.UTF8.GetString(ea.Body);
                //return authCode;
            }

            return(_authCode);
        }
        public string MakePayment(CardPayment payment)
        {
            // correleation id is useful when talking between micro services, we hence make sure we are recieving correct messages/replies
            var corrId = Guid.NewGuid().ToString();
            var props  = _channel.CreateBasicProperties();

            props.ReplyTo       = _replyQueueName;
            props.CorrelationId = corrId;

            // defaulting to default exchange
            _channel.BasicPublish("", "rpc_queue", props, payment.Serialize());

            // we made a payment then started wauiting for a response
            while (true)
            {
                var ea = _consumer.Queue.Dequeue();
                // checking that is the correct correlation id of the stuff that I just sent
                if (ea.BasicProperties.CorrelationId != corrId)
                {
                    continue;
                }

                var authCode = Encoding.UTF8.GetString(ea.Body);
                return(authCode);
            }
        }
Exemplo n.º 4
0
        public Task <string> MakePaymentAsync(CardPayment payment)
        {
            var tcs           = new TaskCompletionSource <string>();
            var correlationId = Guid.NewGuid().ToString();

            pendingMessages[correlationId] = tcs;

            SendMessage(payment.Serialize(), correlationId);

            Console.WriteLine($"Sent: ${payment.Amount} from {payment.Name} with CorrelationId {correlationId}");

            return(tcs.Task);
        }
Exemplo n.º 5
0
 public void SendPayment(CardPayment payment)
 {
     SendMessage(payment.Serialize(), "payment.card");
     Console.WriteLine(" Payment Sent {0}, £{1}", payment.CardNumber,
                       payment.Amount);
 }
Exemplo n.º 6
0
        /*
         * The client application posts messages directly onto a queue.
         * For each message that gets posted, the application waits for a reply from a reply queue.
         * This essentially makes this a synchronous process.
         */

        public string MakePayment(CardPayment payment)
        {
            //we need a guid which is global unique identifier the consumer application
            //will put that same correlation ID onto the reply
            //i.e. assure that we're dealing with the correct reply by checking the ID.
            var corrId = Guid.NewGuid().ToString();
            var props  = _channel.CreateBasicProperties();

            props.ReplyTo = _replyQueueName;

            /*
             * When a message is posted to the server from the client,
             * a correlation ID is generated and attached to the message properties.
             * The same correlation ID is put onto the properties in a reply message.
             * This is  useful, as it allows you to easily tie together the replies in
             * the originating messages if you store them for retrieval later.
             * The client posts a message to the RPC queue that has a correlation ID of e.g. 12345.
             * This message is received by the server and a reply is sent back to the client
             * on a reply queue with the same correlation ID of e.g 12345.
             */
            props.CorrelationId = corrId;

            //create the RPC queue and bind it to the default exchange with routingKey (rpc_queue)
            //also pass our properties into basic publish along with a serialized card payment message
            _channel.BasicPublish(exchange: "", routingKey: "rpc_queue", basicProperties: props, body: payment.Serialize());


            //just sitting and wait for a response from the direct card payment consumer
            while (true) //run an infinite loop to listen to messages in the queue
            {
                //de-queue the message
                var ea = _consumer.Queue.Dequeue();

                //check that the correlation ID from the message coming back is the same
                //as the ID we stored earlier
                if (ea.BasicProperties.CorrelationId != corrId)
                {
                    continue;
                }

                var authCode = Encoding.UTF8.GetString(ea.Body);
                return(authCode);
            }
        }
Exemplo n.º 7
0
        public string MakePayment(CardPayment payment)
        {
            var corrId = Guid.NewGuid().ToString();
            var props  = _channel.CreateBasicProperties(); // Pegando as propriedades do objeto de volta

            props.ReplyTo = _replyQueueName;               // Fila de resposta
            // Colocando uma guid no CorrelationId
            // A aplicação consumer irá colocar o mesmo id na resposta
            // Isso garante que estamos lendo a mensagem enviada
            props.CorrelationId = corrId;

            // Passando as propriedades para a fila (ReplyTo e CorrelationId)
            Console.WriteLine($" Direct Payment Sent {payment.CardNumber}, £{payment.Amount}");
            _channel.BasicPublish("", "rpc_queue", props, payment.Serialize());

            string authCode = "";
            //bool responseReceived = true;// Teste para esperar resposta, não recomendado
            // Alternativa utilizando pull api (não recomendada)
            //https://www.rabbitmq.com/dotnet-api-guide.html

            BasicGetResult result = null;

            while (result == null) // Recomendado TimeOut
            {
                result = _channel.BasicGet(_replyQueueName, autoAck: false);
                if (result != null)
                {
                    // Somente processar a resposta da mensagem enviada
                    //http://www.matthiassommer.it/programming/remote-procedure-calls-with-rabbitmq/
                    if (result.BasicProperties.CorrelationId != corrId)
                    {
                        continue;
                    }
                    var body = result.Body.ToArray();
                    authCode = Encoding.UTF8.GetString(body);
                    _channel.BasicAck(result.DeliveryTag, false);
                }
            }



            // Anonymous function event handling
            // Ver como faria para retornar a mensagem ou acionar algum método com ela
            // _consumer.Received += (model, ea) => // suporta async
            // {
            //     // Checando se o ID é o mesmo da menasgem enviada
            //     if (ea.BasicProperties.CorrelationId == corrId)
            //     {
            //         var body = ea.Body.ToArray();
            //         authCode = Encoding.UTF8.GetString(body);
            //         responseReceived = false;
            //         Console.WriteLine($"Message received with authcode: {authCode}");
            //     }
            // };
            // Registra o consumer para ouvir a fila
            //_channel.BasicConsume(_replyQueueName, true, _consumer);

            //TODO: Pesquisar forma correta de retornar resposta síncrona
            // while (responseReceived)
            // {
            //     // prender no loop
            // }

            return(authCode);
        }
Exemplo n.º 8
0
 public void SendPayment(CardPayment payment)
 {
     SendMessage(payment.Serialize(), "payment.card");
     Console.WriteLine($" Payment Sent {payment.CardNumber} : {payment.AmountToPay}");
 }
Exemplo n.º 9
0
 public void SendCardPayment(CardPayment cardPayment)
 {
     SendMessage(cardPayment.Serialize(), CardRoutingKey);
 }