Пример #1
0
        private void handleReceivedMessage(object sender, BasicDeliverEventArgs e)
        {
            try
            {
                var msgObj = bodySerializer.GetObject <T>(e.Body);
                if (msgObj == null)
                {
                    DebugText?.Invoke("RPCSubscription Callback Failed to parse received message to type " + typeof(T).Name + " using BodySerializer: " + bodySerializer.GetType().Name);
                    return;
                }

                object result = null;

                try
                {
                    result = Callback?.Invoke(msgObj);
                }
                catch (Exception err)
                {
                    DebugText?.Invoke("RPCSubscription Callback Failed to execute Callback -> " + err.ToString());

                    if (!AutoAck)
                    {
                        channel.BasicNack(e.DeliveryTag, false, AutoRequeue);
                    }
                }

                if (result == null)
                {
                    if (!AutoAck)
                    {
                        channel.BasicNack(e.DeliveryTag, false, AutoRequeue);
                    }

                    DebugText?.Invoke("RPCSubscription Callback Returned null");
                    return;
                }

                if (!AutoAck)
                {
                    channel.BasicAck(e.DeliveryTag, false);
                }

                if (e.BasicProperties == null || e.BasicProperties.ReplyTo == null)
                {
                    return;
                }

                var responseProps = channel.CreateBasicProperties();
                responseProps.CorrelationId = e.BasicProperties.CorrelationId;

                channel.BasicPublish(
                    exchange: "",
                    routingKey: e.BasicProperties.ReplyTo,
                    body: bodySerializer.GetBytes(result),
                    basicProperties: responseProps
                    );
            }
            catch (Exception err)
            {
                DebugText.Invoke("RPCSubscription Callback Failed to handle received message -> " + err.ToString());
            }
        }
        public void Run(CancellationToken stoppingToken)
        {
            this.stoppingToken = stoppingToken;
            Task.Run(async() =>
            {
                while ((!verify_publish_channel() || !verify_response_channel()) && !stoppingToken.IsCancellationRequested)
                {
                    await Task.Delay(1000, stoppingToken);
                }

                if (stoppingToken.IsCancellationRequested)
                {
                    return;
                }

                Connected?.Invoke();


                while (!stoppingToken.IsCancellationRequested)
                {
                    try
                    {
                        var msg = publish_queue.Take(stoppingToken);
                        if (publish_channel == null || !publish_channel.IsOpen || !connection.IsOpen)
                        {
                            publish_queue.Add(msg);
                            await Task.Delay(500, stoppingToken);
                            continue;
                        }
                        try
                        {
                            IBasicProperties props = null;
                            if (msg is IRabbitMqRpcMessage rpcMsg && rpcMsg.CorrelationId != null)
                            {
                                props = publish_channel.CreateBasicProperties();
                                props.CorrelationId = rpcMsg.CorrelationId;
                                props.ReplyTo       = responseQueueName;
                            }

                            var body = BodySerializer.GetBytes(msg.content);

                            publish_channel.BasicPublish(
                                exchange: msg.Exchange,
                                routingKey: msg.RoutingKey,
                                body: body,
                                basicProperties: props
                                );
                        }
                        catch (Exception err)
                        {
                            Console.WriteLine("Failed to publish -> " + err.ToString());
                            publish_queue.Add(msg);
                            await Task.Delay(500, stoppingToken);
                        }
                    }
                    catch (Exception err)
                    {
                        Console.WriteLine("General publish error -> " + err.ToString());
                    }
                }

                stoppingToken.Register(() =>
                {
                    if (publish_channel != null && publish_channel.IsOpen)
                    {
                        publish_channel.Close();
                    }

                    stopSubscriptionsTokenSource.Cancel();

                    if (connection != null)
                    {
                        connection.Close();
                    }
                });
            }, stoppingToken);

            //Houskeeping for rpc_queue. Flush messages that has timed out.
            Task.Run(async() =>
            {
                while (!stoppingToken.IsCancellationRequested)
                {
                    while (rpc_queue.Count() > 0 && rpc_queue.Values.Any(v => DateTime.Now > v.ValidTo))
                    {
                        var entry = rpc_queue.Where(v => DateTime.Now > v.Value.ValidTo).First().Value;
                        entry.Timeout();
                        rpc_queue.TryRemove(entry.CorrelationId, out var val);
                    }

                    await Task.Delay(500, stoppingToken);
                }
            }, stoppingToken);
        }