Example #1
0
 public void SubscribeAsync(string channel, Action <string> action)
 {
     LDLR.Core.Utils.ThreadManager.Run(() =>
     {
         pubSub.SubscribeAsync(channel, action);
     });
 }
        public When_autosubscribing_with_subscription_configuration_action()
        {
            pubSub = Substitute.For <IPubSub>();
            bus    = Substitute.For <IBus>();
            bus.PubSub.Returns(pubSub);

            var autoSubscriber = new AutoSubscriber(bus, "my_app")
            {
                ConfigureSubscriptionConfiguration =
                    c => c.WithAutoDelete()
                    .WithExpires(10)
                    .WithPrefetchCount(10)
                    .WithPriority(10)
            };

            pubSub.SubscribeAsync(
                Arg.Is("MyActionTest"),
                Arg.Any <Func <MessageA, CancellationToken, Task> >(),
                Arg.Any <Action <ISubscriptionConfiguration> >()
                )
            .Returns(Task.FromResult(Substitute.For <ISubscriptionResult>()).ToAwaitableDisposable())
            .AndDoes(a => capturedAction = (Action <ISubscriptionConfiguration>)a.Args()[2]);

            autoSubscriber.Subscribe(new[] { typeof(MyConsumerWithAction) });
        }
Example #3
0
        /// <summary>
        /// Subscribes to a stream of messages that match a .NET type.
        /// Allows the subscriber to complete asynchronously.
        /// </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. onMessage can immediately return a Task and
        /// then continue processing asynchronously. When the Task completes the message will be
        /// Ack'd.
        /// </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 AwaitableDisposable <ISubscriptionResult> SubscribeAsync <T>(
            this IPubSub pubSub,
            string subscriptionId,
            Func <T, Task> onMessage,
            CancellationToken cancellationToken = default
            )
        {
            Preconditions.CheckNotNull(pubSub, "pubSub");

            return(pubSub.SubscribeAsync <T>(
                       subscriptionId,
                       (m, c) => onMessage(m),
                       c => { },
                       cancellationToken
                       ));
        }
Example #4
0
        /// <summary>
        /// Subscribes to a stream of messages that match a .NET type.
        /// Allows the subscriber to complete asynchronously.
        /// </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. onMessage can immediately return a Task and
        /// then continue processing asynchronously. When the Task completes the message will be
        /// Ack'd.
        /// </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,
            Func <T, CancellationToken, Task> onMessage,
            Action <ISubscriptionConfiguration> configure,
            CancellationToken cancellationToken = default
            )
        {
            Preconditions.CheckNotNull(pubSub, "pubSub");

            return(pubSub.SubscribeAsync(
                       subscriptionId,
                       onMessage,
                       configure,
                       cancellationToken
                       ).GetAwaiter().GetResult());
        }
        public When_autosubscribing_with_subscription_configuration_attribute_no_expires()
        {
            pubSub = Substitute.For <IPubSub>();
            bus    = Substitute.For <IBus>();
            bus.PubSub.Returns(pubSub);

            var autoSubscriber = new AutoSubscriber(bus, "my_app");

            pubSub.SubscribeAsync(
                Arg.Is("MyAttrTest"),
                Arg.Any <Func <MessageA, CancellationToken, Task> >(),
                Arg.Any <Action <ISubscriptionConfiguration> >()
                )
            .Returns(Task.FromResult(Substitute.For <ISubscriptionResult>()).ToAwaitableDisposable())
            .AndDoes(a => capturedAction = (Action <ISubscriptionConfiguration>)a.Args()[2]);

            autoSubscriber.Subscribe(new[] { typeof(MyConsumerWithAttr) });
        }
Example #6
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 AwaitableDisposable <ISubscriptionResult> SubscribeAsync <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.SubscribeAsync(
                       subscriptionId,
                       onMessageAsync,
                       configure,
                       cancellationToken
                       ));
        }