/// <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>></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); }
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; }
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(); } } }
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; }