コード例 #1
0
        public IHandlerRegistration Add <T>(Action <IMessage <T>, MessageReceivedInfo> handler) where T : class
        {
            Preconditions.CheckNotNull(handler, "handler");

            Add <T>((message, info) => TaskHelpers.ExecuteSynchronously(() => handler(message, info)));
            return(this);
        }
コード例 #2
0
        public static QueueConsumerPair Create <T>(IQueue queue, Action <IMessage <T>, MessageReceivedInfo> onMessage) where T : class
        {
            Preconditions.CheckNotNull(queue, nameof(queue));
            Preconditions.CheckNotNull(onMessage, nameof(onMessage));

            return(Create <T>(queue, (message, info) => TaskHelpers.ExecuteSynchronously(() => onMessage(message, info))));
        }
コード例 #3
0
        public When_a_consumer_has_multiple_handlers()
        {
            mockBuilder = new MockBuilder();

            var queue = new Queue("test_queue", false);

            var countdownEvent = new CountdownEvent(3);

            mockBuilder.Bus.Advanced.Consume(queue, x => x
                                             .Add <MyMessage>((message, info) => TaskHelpers.ExecuteSynchronously(() =>
            {
                myMessageResult = message.Body;
                countdownEvent.Signal();
            }))
                                             .Add <MyOtherMessage>((message, info) => TaskHelpers.ExecuteSynchronously(() =>
            {
                myOtherMessageResult = message.Body;
                countdownEvent.Signal();
            }))
                                             .Add <IAnimal>((message, info) => TaskHelpers.ExecuteSynchronously(() =>
            {
                animalResult = message.Body;
                countdownEvent.Signal();
            })));

            Deliver(new MyMessage {
                Text = "Hello Polymorphs!"
            });
            Deliver(new MyOtherMessage {
                Text = "Hello Isomorphs!"
            });
            Deliver(new Dog());

            countdownEvent.Wait(1000);
        }
コード例 #4
0
        public static QueueConsumerPair Consume(IQueue queue, Action <byte[], MessageProperties, MessageReceivedInfo> onMessage)
        {
            Preconditions.CheckNotNull(queue, nameof(queue));
            Preconditions.CheckNotNull(onMessage, nameof(onMessage));

            return(new QueueConsumerPair(queue, (bytes, properties, info) => TaskHelpers.ExecuteSynchronously(() => onMessage(bytes, properties, info)), null));
        }
コード例 #5
0
        public IHandlerRegistration Add <T>(Action <T> handler) where T : class
        {
            Preconditions.CheckNotNull(handler, "handler");

            this.Add <T>(message => TaskHelpers.ExecuteSynchronously(() => handler(message)));
            return(this);
        }
コード例 #6
0
        public IDisposable Receive <T>(string queue, Action <T> onMessage)
            where T : class
        {
            Preconditions.CheckNotNull(queue, "queue");
            Preconditions.CheckNotNull(onMessage, "onMessage");

            return(Receive <T>(queue, message => TaskHelpers.ExecuteSynchronously(() => onMessage(message))));
        }
コード例 #7
0
        public IDisposable Consume <T>(IQueue queue, Action <IMessage <T>, MessageReceivedInfo> onMessage, Action <IConsumerConfiguration> configure) where T : class
        {
            Preconditions.CheckNotNull(queue, "queue");
            Preconditions.CheckNotNull(onMessage, "onMessage");
            Preconditions.CheckNotNull(configure, "configure");

            return(Consume <T>(queue, (message, info) => TaskHelpers.ExecuteSynchronously(() => onMessage(message, info)), configure));
        }
コード例 #8
0
ファイル: RabbitBus.cs プロジェクト: jackyzhou/ZSharp
 public ISubscriptionResult Subscribe <T>(
     string subscriptionId,
     Action <T> onMessage,
     Action <ISubscriptionConfiguration> configure = null)
     where T : class
 {
     return(SubscribeAsync <T>(
                subscriptionId,
                msg => TaskHelpers.ExecuteSynchronously(() => onMessage(msg)),
                configure));
 }
コード例 #9
0
        public static IDisposable Subscribe(
            this IBus bus,
            Type messageType,
            Action <object> onMessage,
            Action <ISubscriptionConfiguration> configure)
        {
            Preconditions.CheckNotNull(onMessage, "onMessage");

            Func <object, Task> asyncOnMessage = x => TaskHelpers.ExecuteSynchronously(() => onMessage(x));

            return(SubscribeAsync(bus, messageType, asyncOnMessage, configure));
        }
コード例 #10
0
        public static Func <byte[], MessageProperties, MessageReceivedInfo, Task> GetConsumeActionWrapper(HandlerRegistration handlerRegistration, IDependencyResolver dependencyResolver)
        {
            Func <byte[], MessageProperties, MessageReceivedInfo, Task> consumeAction = (body, messageProps, info) =>
            {
                var bodyString    = Encoding.UTF8.GetString(body);
                var messageConent = typeof(string) == handlerRegistration.MessageType
                        ? bodyString
                        : JsonConvert.DeserializeObject(bodyString, handlerRegistration.MessageType, SerializerSettings);

                Logger.Debug("Message {0} recieved with content {1}", handlerRegistration.MessageType, bodyString);

                var message = Message.CreateInstance(handlerRegistration.MessageType, messageConent);
                message.SetProperties(messageProps);
                return
                    (TaskHelpers.ExecuteSynchronously(
                         () =>
                         GetHandler(message, handlerRegistration.GetConsumerContextFunc,
                                    handlerRegistration.Consumer, dependencyResolver)));
            };

            return(consumeAction);
        }
コード例 #11
0
 public IHandlerRegistration Add <T>(Action <IMessage <T>, MessageReceivedInfo> handler) where T : class
 {
     Add <T>((message, info) => TaskHelpers.ExecuteSynchronously(() => handler(message, info)));
     return(this);
 }
コード例 #12
0
 public IDisposable Consume(IQueue queue, Action <byte[], MessageProperties, MessageReceivedInfo> onMessage, Action <IConsumerConfiguration> configure)
 {
     return(Consume(queue, (bytes, properties, info) => TaskHelpers.ExecuteSynchronously(() => onMessage(bytes, properties, info)), configure));
 }
コード例 #13
0
ファイル: RabbitBus.cs プロジェクト: jackyzhou/ZSharp
 public IDisposable Receive <T>(string queue, Action <T> onMessage, Action <IConsumerConfiguration> configure = null) where T : class
 {
     return(ReceiveAsync <T>(queue, message => TaskHelpers.ExecuteSynchronously(() => onMessage(message)), configure));
 }