private void OnReceived(MessageEnvelope<byte[]> envelope, IMessageAck ack)
        {
            try
            {
                var typeName = envelope.Properties.Type;
                typeName.AssertNotNullOrEmpty("typename was expected to be added to message properties");

                // PERF: needs caching
                var msgType = Type.GetType(typeName, throwOnError: true);

                Func<IMessageHandler> builder;
                if (!_message2HandlerBuilder.TryGetValue(msgType, out builder))
                {
                    var msg = "No IMessageHandler registered for message type " + msgType.FullName;
                    this.Logger.Error(msg);
                    throw new Exception(msg);
                }

                IMessageHandler handler = builder();

                var message = (IMessage) _bus.Serializer.Deserialize(envelope.Body, msgType);

                _invoker.Invoke(msgType, message, handler);

                ack.Ack();
            }
            catch (Exception e)
            {
                this.Logger.Error("Error processing message", e);

                ack.Reject(requeue: false);
            }
        }
예제 #2
0
        public void StubPublish <T>(T message, IMessageAck acker = null)
        {
            var props = new BasicProperties();
            var bytes = this.DefaultSerializer.TypedSerialize <T>(message, props);
            var env   = new MessageEnvelope(props, bytes);

            StubPublish(env, acker);
        }
예제 #3
0
        public MessageEnvelope StubRespond(MessageEnvelope requestMessage, IMessageAck acker = null)
        {
            if (!_respondCalled)
            {
                throw new Exception("Not responder set up");
            }
            if (acker == null)
            {
                acker = new StubMessageAcker();
            }

            return(_responder(requestMessage, acker));
        }
예제 #4
0
        public void StubPublish(MessageEnvelope message, IMessageAck acker = null)
        {
            if (!_consumedCalled)
            {
                throw new Exception("Not consumer set up");
            }
            if (acker == null)
            {
                acker = new StubMessageAcker();
            }

            _consumer(message, acker);
        }
예제 #5
0
        public MessageEnvelope <TReply> StubRespond <TRequest, TReply>(TRequest requestMessage, IMessageAck acker = null)
            where TReply : class
        {
            var props = new BasicProperties();
            var bytes = this.DefaultSerializer.TypedSerialize <TRequest>(requestMessage, props);
            var env   = new MessageEnvelope(props, bytes);

            return((MessageEnvelope <TReply>)StubRespond(env, acker));
        }
예제 #6
0
 public Envelope(T message, IMessageAck messageAck)
 {
     _messageAck = messageAck;
     Message     = message;
 }