Beispiel #1
0
        public void Send(RawBusMessage message, ISerializer serializer, SendParams sendParams)
        {
            if (message.Data != null)
            {
                Type            type        = message.Data.GetType();
                DataContractKey contractKey = _nameMappings.GetOrAdd(type, t => t.GetDataContractKey());

                if (message.Name == null)
                {
                    message.Name = contractKey.Name;
                }

                if (message.Namespace == null)
                {
                    message.Namespace = contractKey.Ns;
                }
            }

            var bytes = message.Data as byte[];

            if (bytes == null)
            {
                bytes = message.Data != null?serializer.Serialize(message) : new byte[0];
            }


            Send(bytes, serializer.ContentType, message.Name, message.Namespace, message, sendParams);
        }
Beispiel #2
0
        public void Send(SendParams sendParams)
        {
            sendParams.BusMessage.Sent = DateTime.Now;
            sendParams.BusMessage.BusId = sendParams.BusId;

            BasicProperties basicProperties = new BasicProperties
            {
                AppId = sendParams.BusMessage.BusId,
                Timestamp = sendParams.BusMessage.Sent.ToAmqpTimestamp(),
                ContentType = sendParams.Serializer.ContentType,
                Headers = new Dictionary<string, object>()
            };

            byte[] bytes;

            if (sendParams.BusMessage.Data != null)
            {
                Type type = sendParams.BusMessage.Data.GetType();
                DataContractKey contractKey = _nameMappings.GetOrAdd(type, t => t.GetDataContractKey());

                basicProperties.Type = contractKey.Name;
                basicProperties.Headers.Add(MessagingConstants.HeaderNames.Name, contractKey.Name);
                basicProperties.Headers.Add(MessagingConstants.HeaderNames.NameSpace, contractKey.Ns);

                bytes = sendParams.Serializer.Serialize(sendParams.BusMessage);
            }
            else
            {
                bytes = new byte[0];
            }

            foreach (BusHeaderBase header in sendParams.BusMessage.Headers)
            {
                basicProperties.Headers.Add(header.Name, header.GetValue());
            }

            if (sendParams.PersistentDelivery)
            {
                basicProperties.SetPersistent(true);
            }

            if (!string.IsNullOrEmpty(sendParams.ReplyTo))
            {
                basicProperties.ReplyTo = sendParams.ReplyTo;
            }

            if (!string.IsNullOrEmpty(sendParams.CorrelationId))
            {
                basicProperties.CorrelationId = sendParams.CorrelationId;
            }

            sendParams.Model.BasicPublish(sendParams.Exchange, sendParams.RoutingKey, sendParams.MandatoryDelivery, false, basicProperties, bytes);
        }
        public void Send(SendParams sendParams)
        {
            sendParams.BusMessage.Sent  = DateTime.Now;
            sendParams.BusMessage.BusId = sendParams.BusId;

            BasicProperties basicProperties = new BasicProperties
            {
                AppId       = sendParams.BusMessage.BusId,
                Timestamp   = sendParams.BusMessage.Sent.ToAmqpTimestamp(),
                ContentType = sendParams.Serializer.ContentType,
                Headers     = new Dictionary <string, object>()
            };

            byte[] bytes;

            if (sendParams.BusMessage.Data != null)
            {
                Type            type        = sendParams.BusMessage.Data.GetType();
                DataContractKey contractKey = _nameMappings.GetOrAdd(type, t => t.GetDataContractKey());

                basicProperties.Type = contractKey.Name;
                basicProperties.Headers.Add(MessagingConstants.HeaderNames.Name, contractKey.Name);
                basicProperties.Headers.Add(MessagingConstants.HeaderNames.NameSpace, contractKey.Ns);

                bytes = sendParams.Serializer.Serialize(sendParams.BusMessage);
            }
            else
            {
                bytes = new byte[0];
            }

            foreach (BusHeaderBase header in sendParams.BusMessage.Headers)
            {
                basicProperties.Headers.Add(header.Name, header.GetValue());
            }

            if (sendParams.PersistentDelivery)
            {
                basicProperties.SetPersistent(true);
            }

            if (!string.IsNullOrEmpty(sendParams.ReplyTo))
            {
                basicProperties.ReplyTo = sendParams.ReplyTo;
            }

            if (!string.IsNullOrEmpty(sendParams.CorrelationId))
            {
                basicProperties.CorrelationId = sendParams.CorrelationId;
            }

            sendParams.Model.BasicPublish(sendParams.Exchange, sendParams.RoutingKey, sendParams.MandatoryDelivery, false, basicProperties, bytes);
        }
        protected void SendMessage <TData>(BusMessage <TData> busMessage, string id, bool persistant)
        {
            var rawBusMessage = CreateRawMessage(busMessage);

            var sendParams = new SendParams
            {
                BusId              = _busId,
                Model              = _model,
                CorrelationId      = id,
                Exchange           = _configuration.Exchange,
                MandatoryDelivery  = true,
                PersistentDelivery = persistant || _configuration.PersistentDelivery,
                RoutingKey         = _configuration.RoutingKey,
                ReplyTo            = _replyTo
            };

            _sendHelper.Send(rawBusMessage, _configuration.Serializer, sendParams);

            _configuration.Trace.MessageSent(_busId, rawBusMessage);
        }
Beispiel #5
0
        protected virtual async Task <bool> ConsumeMessage(bool redelivered, ulong deliveryTag, IBasicProperties properties, byte[] body)
        {
            DataContractKey dataContractKey = properties.GetDataContractKey();

            var subscription =
                _subscriptions.Where(p => p.Value.FilterInfo.ContractKey.Equals(dataContractKey)).Select(
                    pair =>
                    new
            {
                DataType = pair.Key,
                pair.Value.Handler
            }).FirstOrDefault();

            if (subscription == null)
            {
                RawBusMessage rawBusMessage = _messageHelper.ConstructMessage(dataContractKey, properties, (object)body);

                _errorSubscriber.UnregisteredMessageArrived(rawBusMessage);

                return(false);
            }

            object data;

            if (!_serializers.ContainsKey(properties.ContentType))
            {
                RawBusMessage rawBusMessage = _messageHelper.ConstructMessage(dataContractKey, properties, (object)body);

                _errorSubscriber.MessageDeserializeException(rawBusMessage, new Exception("Unsupported content type"));

                return(false);
            }

            ISerializer serializer;

            try
            {
                serializer = _serializers[properties.ContentType];

                if (dataContractKey.Equals(DataContractKey.BinaryBlob))
                {
                    data = body;
                }
                else
                {
                    data = serializer.Deserialize(subscription.DataType, body);
                }
            }
            catch (Exception ex)
            {
                RawBusMessage rawBusMessage = _messageHelper.ConstructMessage(dataContractKey, properties, (object)body);

                _errorSubscriber.MessageDeserializeException(rawBusMessage, ex);

                return(false);
            }

            RawBusMessage message = _messageHelper.ConstructMessage(dataContractKey, properties, data);

            if (!_receiveSelfPublish && _busId.Equals(message.BusId))
            {
                _errorSubscriber.MessageFilteredOut(message);

                return(false);
            }

            _trace.MessageArrived(_busId, message, ConsumerTag);

            RawBusMessage reply;

            try
            {
                reply = await HandleMessage(subscription.Handler, message, redelivered, deliveryTag);
            }
            catch (RejectMessageException ex)
            {
                reply = new RawBusMessage
                {
                    Data = ex.ReplyData
                };

                reply.Headers.Add(new RejectedHeader());
            }
            catch (Exception ex)
            {
                _errorSubscriber.MessageDispatchException(message, ex);

                reply = new RawBusMessage();

                reply.Headers.Add(new ExceptionHeader
                {
                    Message = ex.Message
                });
            }

            if (!_neverReply && reply != null && properties.IsReplyToPresent())
            {
                var sendParams = new SendParams
                {
                    BusId              = _busId,
                    Model              = Model,
                    CorrelationId      = properties.IsCorrelationIdPresent() ? properties.CorrelationId : "",
                    Exchange           = _replyExchange,
                    RoutingKey         = properties.ReplyTo,
                    MandatoryDelivery  = false,
                    PersistentDelivery = false
                };

                _sendHelper.Send(reply, serializer, sendParams);

                _trace.MessageSent(_busId, reply);
            }

            return(true);
        }
Beispiel #6
0
 public void Send(SerializedBusMessage message, SendParams sendParams)
 {
     Send(message.Data, message.ContentType, message.Name, message.Namespace, message, sendParams);
 }
Beispiel #7
0
        private void Send(byte[] bytes, string contentType, string name, string ns, BusMessage busMessage, SendParams sendParams)
        {
            busMessage.Sent  = DateTime.Now;
            busMessage.BusId = sendParams.BusId;

            BasicProperties basicProperties = new BasicProperties
            {
                AppId       = busMessage.BusId,
                Timestamp   = busMessage.Sent.ToAmqpTimestamp(),
                ContentType = contentType,
                Headers     = new Dictionary <string, object>()
            };

            if (!string.IsNullOrEmpty(name))
            {
                basicProperties.Type = name;

                basicProperties.Headers.Add(MessagingConstants.HeaderNames.Name, name);
            }

            if (!string.IsNullOrEmpty(ns))
            {
                basicProperties.Headers.Add(MessagingConstants.HeaderNames.NameSpace, ns);
            }

            foreach (BusHeaderBase header in busMessage.Headers)
            {
                basicProperties.Headers.Add(header.Name, header.GetValue());
            }

            if (sendParams.PersistentDelivery)
            {
                basicProperties.Persistent = true;
            }

            if (!string.IsNullOrEmpty(sendParams.ReplyTo))
            {
                basicProperties.ReplyTo = sendParams.ReplyTo;
            }

            if (!string.IsNullOrEmpty(sendParams.CorrelationId))
            {
                basicProperties.CorrelationId = sendParams.CorrelationId;
            }

            if (sendParams.Priority.HasValue)
            {
                basicProperties.Priority = sendParams.Priority.Value;
            }

            sendParams.Model.BasicPublish(sendParams.Exchange, sendParams.RoutingKey, sendParams.MandatoryDelivery, basicProperties, bytes);
        }