Ejemplo n.º 1
0
        /// <summary>
        /// Entry-point to register a reactive subscription
        /// </summary>
        /// <remarks>
        /// You should specify only one handler: either sync or async.
        /// </remarks>
        /// <param name="subject">The subject to subscribe to.</param>
        /// <param name="queue">The queue group name. Specify null or empty if not used.</param>
        /// <param name="syncHandler">The sync callback to handle the messages.</param>
        /// <param name="asyncHandler">The async callback to handle the messages.</param>
        /// <returns>An instance of <see cref="ReactiveSubscription"/>.</returns>
        internal IReactiveSubscription SubscribeReactive(
            string subject,
            string queue,
            Action <MsgOut, CancellationToken> syncHandler,
            Func <MsgOut, CancellationToken, Task> asyncHandler
            )
        {
            if (string.IsNullOrWhiteSpace(subject))
            {
                throw new NATSBadSubscriptionException();
            }

            lock (this._sublocker)
            {
                //creates a new instance of subscription proxy
                this._ssid++;
                var sub = new ReactiveSubscription(
                    this,
                    this._ssid,
                    subject,
                    queue,
                    syncHandler,
                    asyncHandler
                    );

                //then register it
                this._subRegister.Add(this._ssid, sub);

                //finally, enqueues the request of subscription to be
                //sent to the NATS broker.
                this._connmgr.EnqueueSubscriptionOperation(sub.ProtoMessageSub);
                return(sub);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Signals the local dispatcher about new messages related
        /// to the specified subscription
        /// </summary>
        /// <param name="sub"></param>
        internal void ReactiveSubscriptionSignal(ReactiveSubscription sub)
        {
            if (sub.AnyPending == false)
            {
                lock (this._reactProcessorLocker)
                {
                    this._reactPendingQueue.AddLast(sub);
                    sub.AnyPending = true;
                }

                this._reactSemaphore.Release(1);
            }
        }