An object of this class is an asynchronous subscription representing interest in a subject. The subject can have wildcards (partial:*, full:>). Messages will be delivered to the associated MsgHandler event delegates. While nothing prevents event handlers from being added or removed while processing messages, no messages will be received until Start() has been called. This allows all event handlers to be added before message processing begins.
See MsgHandler.
Inheritance: Subscription, IAsyncSubscription, ISubscription
コード例 #1
0
ファイル: NatsExtensions.cs プロジェクト: granth7/neonKUBE
        /// <summary>
        /// Creates an asynchronous queue subscriber on the given <paramref name="subject"/>, and begins delivering
        /// messages to the given event handler.
        /// </summary>
        /// <param name="connection">The connection.</param>
        /// <param name="subject">
        /// The subject on which to listen for messages.
        /// The subject can have wildcards (partial: <c>*</c>, full: <c>&gt;</c>).
        /// </param>
        /// <param name="queue">The name of the queue group in which to participate.</param>
        /// <param name="handler">
        /// The <see cref="EventHandler{MsgHandlerEventArgs}"/> invoked when messages are received
        /// on the returned <see cref="IAsyncSubscription"/>.
        /// </param>
        /// <returns>
        /// An <see cref="IAsyncSubscription"/> to use to read any messages received
        /// from the NATS Server on the given <paramref name="subject"/>.
        /// </returns>
        /// <remarks>
        /// <para>
        /// All subscribers with the same queue name will form the queue group and
        /// only one member of the group will be selected to receive any given message.
        /// </para>
        /// <para>
        /// The <see cref="IAsyncSubscription"/> returned will start delivering messages
        /// to the event handler as soon as they are received. The caller does not have to invoke
        /// <see cref="IAsyncSubscription.Start"/>.
        /// </para>
        /// </remarks>
        /// <seealso cref="ISubscription.Subject"/>
        /// <seealso cref="ISubscription.Queue"/>
        public static IAsyncSubscription <TMessage> SubscribeAsync <TMessage>(this IConnection connection, string subject, string queue, EventHandler <MsgHandlerEventArgs <TMessage> > handler)
            where TMessage : class, IRoundtripData, new()
        {
            var subscription = new AsyncSubscription <TMessage>(connection.SubscribeAsync(subject, queue));

            if (handler != null)
            {
                subscription.RoundtripMessageHandler +=
                    (sender, args) =>
                {
                    handler.Invoke(sender, args);
                };
            }

            return(subscription);
        }
コード例 #2
0
ファイル: Conn.cs プロジェクト: bendan365/csnats-wip
        private AsyncSubscription subscribeAsync(string subject, string queue)
        {
            AsyncSubscription s = null;

            lock (mu)
            {
                if (isClosed())
                    throw new NATSConnectionClosedException();

                s = new AsyncSubscription(this, subject, queue);

                addSubscription(s);
            }

            return s;
        }
コード例 #3
0
ファイル: Conn.cs プロジェクト: bendan365/csnats-wip
 internal void sendSubscriptonMessage(AsyncSubscription s)
 {
     lock (mu)
     {
         // We will send these for all subs when we reconnect
         // so that we can suppress here.
         if (!isReconnecting())
         {
             writeString(IC.subProto, s.Subject, s.Queue, s.sid);
             kickFlusher();
         }
     }
 }
コード例 #4
0
ファイル: Conn.cs プロジェクト: nats-io/csnats
        internal AsyncSubscription subscribeAsync(string subject, string queue,
            EventHandler<MsgHandlerEventArgs> handler)
        {
            AsyncSubscription s = null;

            lock (mu)
            {
                if (isClosed())
                    throw new NATSConnectionClosedException();

                enableSubChannelPooling();

                s = new AsyncSubscription(this, subject, queue);

                addSubscription(s);

                if (handler != null)
                {
                    s.MessageHandler += handler;
                    s.Start();
                }
            }

            return s;
        }