Ejemplo n.º 1
0
        public override void HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey, IBasicProperties properties, byte[] body)
        {
            DataContractKey dataContractKey = properties.GetDataContractKey();

            SerializedBusMessage message = _messageHelper.ConstructMessage(dataContractKey, properties, body);

            try
            {
                _monitor(message);
            }
            catch (Exception ex)
            {
                RawBusMessage raw = new RawBusMessage
                {
                    Data          = message.Data,
                    Namespace     = message.Namespace,
                    Name          = message.Name,
                    BusId         = message.BusId,
                    CorrelationId = message.CorrelationId,
                    Sent          = message.Sent
                };

                foreach (var header in message.Headers)
                {
                    raw.Headers.Add(header);
                }

                _errorSubscriber.MessageDispatchException(raw, ex);
            }
        }
Ejemplo n.º 2
0
        private void ModelOnBasicReturn(object sender, BasicReturnEventArgs args)
        {
            DataContractKey dataContractKey = args.BasicProperties.GetDataContractKey();

            Type dataType = _sendHelper.GetDataType(dataContractKey);

            if (dataType == null)
            {
                dataContractKey = DataContractKey.BinaryBlob;
            }

            object data;

            if (dataContractKey.Equals(DataContractKey.BinaryBlob))
            {
                data = args.Body;
            }
            else
            {
                data = _configuration.Serializer.Deserialize(dataType, args.Body);
            }

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

            OnMessageReturn(args.ReplyCode, args.ReplyText, message);
        }
Ejemplo n.º 3
0
        public override void HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey, IBasicProperties properties, byte[] body)
        {
            DataContractKey dataContractKey = properties.GetDataContractKey();

            string sBody = _encoding.GetString(body);

            RawBusMessage rawBusMessage = _messageHelper.ConstructMessage(dataContractKey, properties, (object)sBody);

            try
            {
                _monitor(rawBusMessage);
            }
            // ReSharper disable once EmptyGeneralCatchClause
            catch
            {
            }
        }
Ejemplo n.º 4
0
        private void ModelOnBasicReturn(object sender, BasicReturnEventArgs args)
        {
            DataContractKey dataContractKey = args.BasicProperties.GetDataContractKey();

            Type dataType = _nameMappings.Where(pair => pair.Value.Equals(dataContractKey)).Select(pair => pair.Key).FirstOrDefault();

            if (dataType == null)
            {
                dataContractKey = DataContractKey.BinaryBlob;
            }

            object data = _configuration.Serializer.Deserialize(dataContractKey, dataType, args.Body);

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

            _configuration.ErrorHandler.DeliveryFailed(args.ReplyCode, args.ReplyText, message);
        }
        public override Task HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey, IBasicProperties properties, byte[] body)
        {
            DataContractKey dataContractKey = properties.GetDataContractKey();

            SerializedBusMessage message = _messageHelper.ConstructMessage(dataContractKey, properties, body);

            try
            {
                _monitor(message);

                Model.BasicAck(deliveryTag, false);
            }
            catch (RejectMessageException)
            {
                // If reject message exception is thrown -> reject message without requeue it.
                // Message will be lost or transfered to dead letter exchange by broker
                Model.BasicNack(deliveryTag, false, false);
            }
            catch (Exception ex)
            {
                RawBusMessage raw = new RawBusMessage
                {
                    Data          = message.Data,
                    Namespace     = message.Namespace,
                    Name          = message.Name,
                    BusId         = message.BusId,
                    CorrelationId = message.CorrelationId,
                    Sent          = message.Sent
                };

                foreach (var header in message.Headers)
                {
                    raw.Headers.Add(header);
                }

                bool requeue = _exceptionFilter.Filter(ex, raw, redelivered, deliveryTag);

                Model.BasicNack(deliveryTag, false, requeue);
            }

            return(Task.FromResult(0));
        }
Ejemplo n.º 6
0
        public override Task HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey, IBasicProperties properties, byte[] body)
        {
            if (!properties.IsCorrelationIdPresent())
            {
                return(Task.FromResult(0));
            }

            CallbackInfo info;

            if (_callbacksDictionary.TryRemove(properties.CorrelationId, out info))
            {
                var rejectHeader =
                    properties.Headers.Where(pair => pair.Key == RejectedHeader.WellknownName)
                    .Select(pair => pair.Value)
                    .FirstOrDefault();

                var exceptionHeader =
                    properties.Headers.Where(pair => pair.Key == ExceptionHeader.WellknownName)
                    .Select(pair => pair.Value)
                    .FirstOrDefault();

                if (exceptionHeader != null)
                {
                    info.SetResponse(null, new RpcCallException(RpcFailureReason.HandlerError, exceptionHeader.ToString()));

                    return(Task.FromResult(0));
                }

                DataContractKey dataContractKey;
                object          data;

                if (body.Length == 0 || info.ReplyType == null)
                {
                    // Reject without data
                    if (rejectHeader != null)
                    {
                        info.SetResponse(null, new RpcCallException(RpcFailureReason.Reject));

                        return(Task.FromResult(0));
                    }

                    // Void reply or sender not interested in reply data, but only interested to be notified that work is done
                    dataContractKey = DataContractKey.Void;
                    data            = null;
                }
                else
                {
                    dataContractKey = properties.GetDataContractKey();

                    if (!_serializers.ContainsKey(properties.ContentType))
                    {
                        info.SetResponse(null,
                                         new RpcCallException(RpcFailureReason.SerializationError,
                                                              string.Format("Unsupported content type {0}", properties.ContentType)));

                        return(Task.FromResult(0));
                    }

                    try
                    {
                        ISerializer serializer = _serializers[properties.ContentType];

                        if (dataContractKey.Equals(DataContractKey.BinaryBlob))
                        {
                            data = body;
                        }
                        else
                        {
                            data = serializer.Deserialize(info.ReplyType, body);
                        }
                    }
                    catch (Exception ex)
                    {
                        info.SetResponse(null, new RpcCallException(RpcFailureReason.SerializationError, ex));

                        return(Task.FromResult(0));
                    }
                }

                // Reject with data
                if (rejectHeader != null)
                {
                    info.SetResponse(null, new RpcCallException(RpcFailureReason.Reject, data));

                    return(Task.FromResult(0));
                }

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

                _trace.MessageArrived(_busId, message, ConsumerTag);

                info.SetResponse(message, null);

                info.RegisteredHandle.Unregister(info.WaitHandle);
            }

            return(Task.FromResult(0));
        }
Ejemplo n.º 7
0
        public BusMessage <TData> ReceiveBusMessage <TData>()
        {
            BasicGetResult result = _model.BasicGet(_queue, true);

            IBasicProperties basicProperties = result.BasicProperties;

            DataContractKey dataContractKey = basicProperties.GetDataContractKey();

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

            if (subscription == null)
            {
                RawBusMessage rawBusMessage = _messageHelper.ConstructMessage(dataContractKey, basicProperties, (object)result.Body);

                _errorSubscriber.UnregisteredMessageArrived(rawBusMessage);

                return(null);
            }

            object data;

            if (!_serializers.ContainsKey(basicProperties.ContentType))
            {
                RawBusMessage rawBusMessage = _messageHelper.ConstructMessage(dataContractKey, basicProperties, (object)result.Body);

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

                return(null);
            }

            try
            {
                data = _serializers[basicProperties.ContentType].Deserialize(dataContractKey, subscription.DataType, result.Body);
            }
            catch (Exception ex)
            {
                RawBusMessage rawBusMessage = _messageHelper.ConstructMessage(dataContractKey, basicProperties, (object)result.Body);

                _errorSubscriber.MessageDeserializeException(rawBusMessage, ex);

                return(null);
            }

            BusMessage <TData> message = _messageHelper.ConstructMessage(dataContractKey, basicProperties, (TData)data);

            if (!_receiveSelfPublish && _busId.Equals(message.BusId))
            {
                RawBusMessage rawBusMessage = _messageHelper.ConstructMessage(dataContractKey, basicProperties, (object)result.Body);

                _errorSubscriber.MessageFilteredOut(rawBusMessage);

                return(null);
            }

            return(message);
        }
Ejemplo n.º 8
0
        private void 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;
            }

            object data;

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

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

                return;
            }

            try
            {
                data = _serializers[properties.ContentType].Deserialize(dataContractKey, subscription.DataType, body);
            }
            catch (Exception ex)
            {
                RawBusMessage rawBusMessage = _messageHelper.ConstructMessage(dataContractKey, properties, (object)body);

                _errorSubscriber.MessageDeserializeException(rawBusMessage, ex);

                return;
            }

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

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

                return;
            }

            try
            {
                HandleMessage(subscription.Handler, message, redelivered, deliveryTag);
            }
            catch (Exception ex)
            {
                _errorSubscriber.MessageDispatchException(message, ex);
            }
        }
Ejemplo n.º 9
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);
        }