Example #1
0
        // ---------------------------------- consume --------------------------------------
        public IDisposable Consume(IEnumerable <QueueConsumerPair> queueConsumerPairs, Action <IConsumerConfiguration> configure)
        {
            Preconditions.CheckNotNull(queueConsumerPairs, nameof(queueConsumerPairs));
            Preconditions.CheckNotNull(configure, "configure");

            if (disposed)
            {
                throw new MessageBusException("This bus has been disposed");
            }

            var queueOnMessages = queueConsumerPairs.Select(x =>
            {
                var onMessage = x.OnMessage;
                if (onMessage == null)
                {
                    var handlerCollection = handlerCollectionFactory.CreateHandlerCollection(x.Queue);
                    x.AddHandlers(handlerCollection);

                    onMessage = (body, properties, messageReceivedInfo) =>
                    {
                        var deserializedMessage = messageSerializationStrategy.DeserializeMessage(properties, body);
                        var handler             = handlerCollection.GetHandler(deserializedMessage.MessageType);
                        return(handler(deserializedMessage, messageReceivedInfo));
                    };
                }
                return(Tuple.Create(x.Queue, onMessage));
            }).ToList();

            var consumerConfiguration = new ConsumerConfiguration(connectionConfiguration.PrefetchCount);

            configure(consumerConfiguration);
            var consumer = consumerFactory.CreateConsumer(queueOnMessages, connection, consumerConfiguration);

            return(consumer.StartConsuming());
        }
Example #2
0
        public IDisposable Consume(IQueue queue, Action <IHandlerRegistration> addHandlers, Action <IConsumerConfiguration> configure)
        {
            Preconditions.CheckNotNull(queue, "queue");
            Preconditions.CheckNotNull(addHandlers, "addHandlers");
            Preconditions.CheckNotNull(configure, "configure");

            var handlerCollection = handlerCollectionFactory.CreateHandlerCollection();

            addHandlers(handlerCollection);

            return(Consume(queue, (body, properties, messageReceivedInfo) =>
            {
                var deserializedMessage = messageSerializationStrategy.DeserializeMessage(properties, body);
                var handler = handlerCollection.GetHandler(deserializedMessage.MessageType);
                return handler(deserializedMessage, messageReceivedInfo);
            }, configure));
        }
Example #3
0
        public virtual IDisposable Consume(IQueue queue, Action <IHandlerRegistration> addHandlers)
        {
            Preconditions.CheckNotNull(queue, "queue");
            Preconditions.CheckNotNull(addHandlers, "addHandlers");

            var handlerCollection = handlerCollectionFactory.CreateHandlerCollection();

            addHandlers(handlerCollection);

            return(Consume(queue, (body, properties, messageRecievedInfo) =>
            {
                var messageType = typeNameSerializer.DeSerialize(properties.Type);
                var handler = handlerCollection.GetHandler(messageType);

                var messageBody = serializer.BytesToMessage(properties.Type, body);
                var message = Message.CreateInstance(messageType, messageBody);
                message.SetProperties(properties);
                return handler(message, messageRecievedInfo);
            }));
        }