/// <summary>
        /// Add an asynchronous message handler to this receiver
        /// </summary>
        /// <typeparam name="T">The type of message to receive</typeparam>
        /// <param name="receiveRegistration">The receive registration</param>
        /// <param name="onMessage">The message handler</param>
        /// <returns>'this' for fluent configuration</returns>
        public static IReceiveRegistration Add <T>(this IReceiveRegistration receiveRegistration, Action <T> onMessage)
        {
            Preconditions.CheckNotNull(receiveRegistration, "receiveRegistration");

            var onMessageAsync = TaskHelpers.FromAction <T>((m, c) => onMessage(m));

            return(receiveRegistration.Add(onMessageAsync));
        }
Example #2
0
        public static QueueConsumerPair Consume(IQueue queue, Action <byte[], MessageProperties, MessageReceivedInfo> onMessage)
        {
            Preconditions.CheckNotNull(queue, nameof(queue));
            Preconditions.CheckNotNull(onMessage, nameof(onMessage));

            var onMessageAsync = TaskHelpers.FromAction <byte[], MessageProperties, MessageReceivedInfo>(
                (m, p, i, c) => onMessage(m, p, i)
                );

            return(Consume(queue, onMessageAsync));
        }
Example #3
0
        /// <summary>
        /// Consume a stream of messages
        /// </summary>
        /// <typeparam name="T">The message type</typeparam>
        /// <param name="bus">The bus instance</param>
        /// <param name="queue">The queue to take messages from</param>
        /// <param name="onMessage">The message handler</param>
        /// <param name="configure">
        /// Fluent configuration e.g. x => x.WithPriority(10)</param>
        /// <returns>A disposable to cancel the consumer</returns>
        public static IDisposable Consume <T>(
            this IAdvancedBus bus,
            IQueue queue,
            Action <IMessage <T>, MessageReceivedInfo> onMessage,
            Action <IConsumerConfiguration> configure
            )
        {
            Preconditions.CheckNotNull(bus, "bus");

            var onMessageAsync = TaskHelpers.FromAction <IMessage <T>, MessageReceivedInfo>((m, i, c) => onMessage(m, i));

            return(bus.Consume(queue, onMessageAsync, configure));
        }
Example #4
0
        /// <summary>
        /// Subscribes to a stream of messages that match a .NET type.
        /// </summary>
        /// <typeparam name="T">The type to subscribe to</typeparam>
        /// <param name="pubSub">The pubSub instance</param>
        /// <param name="subscriptionId">
        /// A unique identifier for the subscription. Two subscriptions with the same subscriptionId
        /// and type will get messages delivered in turn. This is useful if you want multiple subscribers
        /// to load balance a subscription in a round-robin fashion.
        /// </param>
        /// <param name="onMessage">
        /// The action to run when a message arrives. When onMessage completes the message
        /// receipt is Ack'd. All onMessage delegates are processed on a single thread so you should
        /// avoid long running blocking IO operations. Consider using SubscribeAsync
        /// </param>
        /// <param name="configure">
        /// Fluent configuration e.g. x => x.WithTopic("uk.london")
        /// </param>
        /// <param name="cancellationToken">The cancellation token</param>
        /// <returns>
        /// An <see cref="ISubscriptionResult"/>
        /// Call Dispose on it or on its <see cref="ISubscriptionResult.ConsumerCancellation"/> to cancel the subscription.
        /// </returns>
        public static ISubscriptionResult Subscribe <T>(
            this IPubSub pubSub,
            string subscriptionId,
            Action <T> onMessage,
            Action <ISubscriptionConfiguration> configure,
            CancellationToken cancellationToken = default
            )
        {
            Preconditions.CheckNotNull(pubSub, "pubSub");

            var onMessageAsync = TaskHelpers.FromAction <T>((m, c) => onMessage(m));

            return(pubSub.Subscribe(
                       subscriptionId,
                       onMessageAsync,
                       configure,
                       cancellationToken
                       ));
        }
Example #5
0
        /// <summary>
        /// Receive a message from the specified queue
        /// </summary>
        /// <typeparam name="T">The type of message to receive</typeparam>
        /// <param name="sendReceive">The sendReceive instance</param>
        /// <param name="queue">The queue to receive from</param>
        /// <param name="onMessage">The synchronous function that handles the message</param>
        /// <param name="configure">Action to configure consumer with</param>
        /// <param name="cancellationToken">The cancellation token</param>
        /// <returns>Consumer cancellation. Call Dispose to stop consuming</returns>
        public static IDisposable Receive <T>(
            this ISendReceive sendReceive,
            string queue,
            Action <T> onMessage,
            Action <IConsumerConfiguration> configure,
            CancellationToken cancellationToken = default
            )
        {
            Preconditions.CheckNotNull(sendReceive, "sendReceive");

            var onMessageAsync = TaskHelpers.FromAction <T>((m, c) => onMessage(m));

            return(sendReceive.Receive(
                       queue,
                       onMessageAsync,
                       configure,
                       cancellationToken
                       ));
        }