Esempio n. 1
0
        public void SendMessage(IMessage message, MessageMetadata metadata)
        {
            try
            {
                if (!IsConnected)
                {
                    return;
                }

                byte[] body = SerializeMessage(message, metadata.SerializationType);

                using (IModel channel = RabbitMqConnection.CreateChannel(this))
                {
                    IBasicProperties basicProperties = RabbitMqConnection.CreateBasicProperties(channel, message, metadata);

                    string routingKey = string.IsNullOrEmpty(metadata.ReplyTo)
                        ? string.IsNullOrEmpty(metadata.Topic) ? message.GetType().Name : metadata.Topic
                        : metadata.ReplyTo;

                    Logger.LogInformation($"Send message to {metadata.Destination} of type {message.GetType().Name} with routing key: {routingKey}");

                    RabbitMqConnection.PublishMessage(channel, metadata.Destination, basicProperties, body, routingKey);
                }
            }
            catch (Exception e)
            {
                Logger.LogError(e, $"Send message exception. HostName: {BrokerConfiguration.HostName} ... ");
            }
        }
Esempio n. 2
0
        public Task <Tuple <TResponse, MessageMetadata, string> > RequestAsync <TResponse>(IMessage message, MessageMetadata metadata)
            where TResponse : class, IMessage
        {
            string queueName = null;

            try
            {
                var    taskCompletionSource = new TaskCompletionSource <Tuple <TResponse, MessageMetadata, string> >();
                string routingKey           = string.IsNullOrEmpty(metadata.ReplyTo)
                    ? string.IsNullOrEmpty(metadata.Topic) ? message.GetType().Name : metadata.Topic
                    : metadata.ReplyTo;

                IModel channel = RabbitMqConnection.CreateChannel(this);
                queueName = RabbitMqConnection.CreateQueue(channel, string.Empty, true, false, false);

                metadata.MessageId = string.IsNullOrEmpty(metadata.ReplyTo) ? queueName : metadata.ReplyTo;
                metadata.ReplyTo   = queueName;

                RabbitMqConnection.CreateBinding(channel, queueName, MessageMetadata.RpcDestination, metadata.ReplyTo);
                EventingBasicConsumer consumer = RabbitMqConnection.CreateEventingBasicConsumer(channel);

                consumer.Received += ConsumerOnReceived;
                RabbitMqConnection.ConsumeQueue(this, channel, queueName, string.Empty, true, consumer);

                IBasicProperties basicProperties = RabbitMqConnection.CreateBasicProperties(channel, message, metadata);
                basicProperties.ReplyTo = string.IsNullOrEmpty(metadata.ReplyTo) ? queueName : metadata.ReplyTo;

                if (!basicProperties.IsHeadersPresent())
                {
                    basicProperties.Headers = new Dictionary <string, object>();
                }
                basicProperties.Headers[MessageMetadata.ResponseDestinationHeaderKey] = MessageMetadata.RpcDestination;
                RegisterResponseAction(basicProperties.CorrelationId, queueName, consumer, typeof(TResponse), taskCompletionSource);

                byte[] body = SerializeMessage(message, metadata.SerializationType);

                Logger.LogInformation(
                    $"Send request to: {metadata.Destination} with routingkey: {routingKey} - ReplyTo: {basicProperties.ReplyTo} of type: {typeof(TResponse).Name} id: {basicProperties.CorrelationId}");
                RabbitMqConnection.PublishMessage(channel, metadata.Destination, basicProperties, body, routingKey);
                return(taskCompletionSource.Task);
            }
            catch (Exception e)
            {
                Logger.LogError(e, $"{nameof(RequestAsync)}");
                if (queueName != null)
                {
                    ForgetMessage(queueName);
                }
                throw;
            }
        }
Esempio n. 3
0
        public void SendMessage(byte[] body, IMessage message, MessageMetadata metadata)
        {
            if (!IsConnected)
            {
                return;
            }

            using (IModel channel = RabbitMqConnection.CreateChannel(this))
            {
                IBasicProperties basicProperties = RabbitMqConnection.CreateBasicProperties(channel, message, metadata);

                string routingKey = string.IsNullOrEmpty(metadata.ReplyTo)
                    ? string.IsNullOrEmpty(metadata.Topic) ? message.GetType().Name : metadata.Topic
                    : metadata.ReplyTo;

                Logger.LogInformation($"Send message to {metadata.Destination} of type {message.GetType().Name} with routing key: {routingKey}");

                RabbitMqConnection.PublishMessage(channel, metadata.Destination, basicProperties, body, routingKey);
            }
        }