Esempio n. 1
0
        private void PublishCommandMessage(object message, string queueName, string key, Guid correlationId, string exchangeName)
        {
            var eventMessage = new EventMessage
            {
                CorrelationId  = correlationId,
                RoutingKey     = key,
                Type           = key,
                JsonMessage    = JsonConvert.SerializeObject(message),
                ReplyQueueName = _callbackQueue
            };

            switch (exchangeName)
            {
            case null:
                _busProvider.BasicTopicBind(queueName, key);
                _busProvider.BasicPublish(eventMessage);
                break;

            case "":     // "" is actually a exchange of type 'direct' in rabbitMQ. No topicbind needed in this case
                _busProvider.BasicPublish(eventMessage, exchangeName);
                break;

            default:
                _busProvider.BasicTopicBind(queueName, key, exchangeName);
                _busProvider.BasicPublish(eventMessage, exchangeName);
                break;
            }
        }
        public void AddCallbackForQueue(
            string queueName,
            string topic,
            Action <EventMessage> callback,
            string exchangeName = null)
        {
            var callbackForTopic = new CallbackForTopic
            {
                Topic    = topic,
                Callback = callback
            };

            lock (_lockObject)
            {
                if (exchangeName == null)
                {
                    _busProvider.BasicTopicBind(queueName, topic);
                }
                else
                {
                    _busProvider.BasicTopicBind(queueName, topic, exchangeName);
                }

                if (_queueCallbacks.ContainsKey(queueName))
                {
                    if (CanAddCallback(_queueCallbacks[queueName], topic))
                    {
                        _queueCallbacks[queueName].Add(callbackForTopic);
                    }
                }
                else
                {
                    _queueCallbacks[queueName] = new List <CallbackForTopic>
                    {
                        callbackForTopic
                    };

                    RegisterCallbackListener(
                        queueName,
                        eventMessage => InvokeMatchingTopicCallbacks(eventMessage, queueName));
                }
            }
        }
        private void HandleReceivedCommand(CommandReceivedCallback callback, EventMessage receivedEventMessage, Type parameterType, string exchangeName)
        {
            Task.Run(async() =>
            {
                object response = null;
                var isError     = false;

                var replyKey = $"{receivedEventMessage.RoutingKey}.Reply";
                _busProvider.BasicTopicBind(receivedEventMessage.ReplyQueueName, replyKey);

                try
                {
                    var deserializedParameter = JsonConvert.DeserializeObject(receivedEventMessage.JsonMessage, parameterType);
                    response = await callback(deserializedParameter);
                }
                catch (TargetInvocationException ex)
                {
                    response = new CommandPublisherException(ex.InnerException.Message, ex.InnerException);
                    isError  = true;
                }
                catch (Exception ex)
                {
                    response = new CommandPublisherException(ex.Message, ex);
                    isError  = true;
                }
                finally
                {
                    if (exchangeName == null)
                    {
                        _busProvider.BasicPublish(new EventMessage
                        {
                            CorrelationId = receivedEventMessage.CorrelationId,
                            IsError       = isError,
                            JsonMessage   = JsonConvert.SerializeObject(response),
                            Type          = replyKey,
                            RoutingKey    = replyKey
                        });
                    }
                    else
                    {
                        _busProvider.BasicPublish(new EventMessage
                        {
                            CorrelationId = receivedEventMessage.CorrelationId,
                            IsError       = isError,
                            JsonMessage   = JsonConvert.SerializeObject(response),
                            Type          = replyKey,
                            RoutingKey    = replyKey
                        },
                                                  exchangeName);
                    }

                    _busProvider.BasicAcknowledge(receivedEventMessage.DeliveryTag, false);
                }
            });
        }