public void     Dequeue_ShouldDispatchFromSeparateThread()
        {
            var model = new     Mock <IModel>();

            var sharedQConsumer = new SharedQueueConsumer(model.Object);

            var msgsReceived = new List <MessageEnvelope>();

            var curThreadId      = Thread.CurrentThread.ManagedThreadId;
            int dispatchThreadId = 0;


            sharedQConsumer.Subscribe(new ActionAdapter((env) =>
            {
                msgsReceived.Add(env);
                dispatchThreadId = Thread.CurrentThread.ManagedThreadId;
            }));

            var prop = new BasicProperties();

            sharedQConsumer.Queue.Enqueue(new BasicDeliverEventArgs()
            {
                Body            = _serializer.Serialize(new MyMessage(), prop),
                BasicProperties = prop
            });

            Thread.Sleep(100);

            msgsReceived.Count.Should().Be(1);
            dispatchThreadId.Should().BeGreaterThan(0);
            curThreadId.Should().NotBe(dispatchThreadId);
        }
예제 #2
0
        public void     OnNext(MessageEnvelope newMsg)
        {
            var incomingMsgProperties = newMsg.Properties;
            var replyQueue            = incomingMsgProperties.ReplyTo;
            var correlationId         = incomingMsgProperties.CorrelationId;

            IBasicProperties replyProperties = null;

            byte[] replyData = new byte[0];

            var msgAcker = CreateAcker(newMsg);

            try
            {
                var response = _onRespond(newMsg, msgAcker);
                response  = response ?? new MessageEnvelope(new BasicProperties(), new byte[0]);
                replyData = response.Body;

                replyProperties = response.Properties ?? new BasicProperties();
            }
            catch (Exception e)
            {
                replyProperties = replyProperties ?? new BasicProperties();

                if (LogAdapter.LogEnabled)
                {
                    LogAdapter.LogError("Rpc", "OnNext error", e);
                }

                // Empty data
                if (_shouldSerializeExceptions)
                {
                    replyData = _serializer.Serialize(new ErrorResponse()
                    {
                        Exception = e
                    }, replyProperties);
                }

                ErrorResponse.FlagHeaders(replyProperties);
            }

            // which call is which
            replyProperties.CorrelationId = correlationId;

            // Rabbit client will explode if this is not true
            (replyProperties is RabbitMQ.Client.Impl.BasicProperties)
            .AssertIsTrue("expected BasicProperties implementation of IBasicProperties");

            lock (_model)
            {
                _model.BasicPublish("", replyQueue, replyProperties, replyData);
            }
        }
예제 #3
0
        public static byte[] TypedSerialize <T>(this IRabbitSerializer source, T instance, IBasicProperties properties)
        {
            if (properties.Type == null)
            {
                var msgType = instance.GetType();

                if (Castle.Core.ProxyServices.IsDynamicProxy(instance.GetType()))
                {
                    throw new Exception("Serialization of a proxy type will be really bad for your sanity");
                }

                var fullname = msgType.AssemblyQualifiedName;
                var sndComma = fullname.IndexOf("Version=", msgType.FullName.Length, StringComparison.Ordinal);

                properties.Type = fullname.Substring(0, sndComma - 2);;
            }

            return(source.Serialize(instance, properties));
        }