Beispiel #1
0
        public void CreateProducer(BankResponse bankResponse, string queue, string routingKey, string correlationId)
        {
            var connection = GetConnection.ConnectionGetter();

            using var channel = connection.CreateModel();
            channel.ExchangeDeclare(exchange: exchange,
                                    durable: true,
                                    type: ExchangeType.Direct);

            channel.QueueDeclare(queue: queue,
                                 durable: true,
                                 exclusive: false,
                                 autoDelete: false,
                                 arguments: null);

            channel.QueueBind(queue, exchange, routingKey);

            var message = JsonSerializer.Serialize(bankResponse);

            var body = Encoding.UTF8.GetBytes(message);

            var props = channel.CreateBasicProperties();

            props.CorrelationId = correlationId;

            Console.WriteLine($"Sender til {queue}");
            ShowData.DisplayData(bankResponse, correlationId);

            channel.BasicPublish(exchange: exchange,
                                 routingKey: routingKey,
                                 basicProperties: props,
                                 body: body);

            CloseConnection.CloseAll(channel, connection);
        }
Beispiel #2
0
        public void ConsumeFromQueue(string queueName, string exchangeName, string routingKey)
        {
            var connection = GetConnection.ConnectionGetter();
            var channel    = connection.CreateModel();

            channel.ExchangeDeclare(exchangeName, ExchangeType.Direct, true);

            channel.QueueDeclare(queueName, true, false, false);

            channel.QueueBind(queueName, exchangeName, routingKey);

            var consumer = new EventingBasicConsumer(channel);

            consumer.Received += (sender, eventArgs) =>
            {
                var bytes = eventArgs.Body.ToArray();

                var body = Encoding.UTF8.GetString(bytes);

                var customer = JsonSerializer.Deserialize <Customer>(body);

                Console.WriteLine($"Besked modtaget fra {queueName}");

                var correlationId = eventArgs.BasicProperties.CorrelationId;

                if (!string.IsNullOrEmpty(customer.CustomerId))
                {
                    ShowData.DisplayData(customer, correlationId);
                    _redistributeMessages.SortMessages(customer, correlationId);
                }
                else
                {
                    var bankResponse = JsonSerializer.Deserialize <BankResponse>(body);
                    if (bankResponse.Response)
                    {
                        ShowData.DisplayData(bankResponse, correlationId);
                    }
                    else
                    {
                        ShowData.RejectedFunds();
                    }
                    _redistributeMessages.MessagesFromBank(bankResponse, correlationId);
                }
            };

            channel.BasicConsume(queueName, true, consumer);
        }
Beispiel #3
0
        public void CreateProducer(Customer customer, string queue, string routingKey)
        {
            queue      = EndPoints.WebsiteQueueName;
            routingKey = EndPoints.WebsiteRoutingKey;

            var connection = GetConnection.ConnectionGetter();

            using var channel = connection.CreateModel();
            channel.ExchangeDeclare(exchange: exchange,
                                    durable: true,
                                    type: ExchangeType.Direct);

            channel.QueueDeclare(queue: queue,
                                 durable: true,
                                 exclusive: false,
                                 autoDelete: false,
                                 arguments: null);

            channel.QueueBind(queue, exchange, routingKey);

            var message = JsonSerializer.Serialize(customer);

            var body = Encoding.UTF8.GetBytes(message);

            var props = channel.CreateBasicProperties();

            props.CorrelationId = customer.CustomerId;
            props.Persistent    = true;

            Console.WriteLine($"Sender til {queue}");
            ShowData.DisplayData(customer, null);

            channel.BasicPublish(exchange: exchange,
                                 routingKey: routingKey,
                                 basicProperties: props,
                                 body: body);

            CloseConnection.CloseAll(channel, connection);
        }