コード例 #1
0
 public void Unsubscribe()
 {
     try
     {
         sub?.Unsubscribe();
     }
     finally
     {
         sub = null;
     }
 }
コード例 #2
0
        private void _testPushDurableSubAsync(IJetStream js, PushAsyncSubSupplier supplier)
        {
            CountdownEvent latch    = new CountdownEvent(5);
            int            received = 0;

            void TestHandler(object sender, MsgHandlerEventArgs args)
            {
                received++;
                args.Message.Ack();
                latch.Signal();
            }

            // Subscribe using the handler
            IJetStreamPushAsyncSubscription sub = supplier.Invoke(TestHandler);

            // Wait for messages to arrive using the countdown latch.
            latch.Wait(10000);

            sub.Unsubscribe();

            Assert.Equal(5, received);
        }
コード例 #3
0
        public void TestHandlerAutoAck()
        {
            Context.RunInJsServer(c =>
            {
                // create the stream.
                CreateDefaultTestStream(c);

                // Create our JetStream context.
                IJetStream js = c.CreateJetStreamContext();

                // publish some messages
                JsPublish(js, SUBJECT, 10);

                // 1. auto ack true
                CountdownEvent latch1 = new CountdownEvent(10);
                int handlerReceived1  = 0;

                // create our message handler, does not ack
                void Handler1(object sender, MsgHandlerEventArgs args)
                {
                    handlerReceived1++;
                    latch1.Signal();
                }

                // subscribe using the handler, auto ack true
                PushSubscribeOptions pso1 = PushSubscribeOptions.Builder()
                                            .WithDurable(Durable(1)).Build();
                IJetStreamPushAsyncSubscription asub = js.PushSubscribeAsync(SUBJECT, Handler1, true, pso1);

                // wait for messages to arrive using the countdown latch.
                latch1.Wait();

                Assert.Equal(10, handlerReceived1);

                asub.Unsubscribe();

                // check that all the messages were read by the durable
                IJetStreamPushSyncSubscription ssub = js.PushSubscribeSync(SUBJECT, pso1);
                AssertNoMoreMessages(ssub);

                // 2. auto ack false
                CountdownEvent latch2 = new CountdownEvent(10);
                int handlerReceived2  = 0;

                // create our message handler, also does not ack
                void Handler2(object sender, MsgHandlerEventArgs args)
                {
                    handlerReceived2++;
                    latch2.Signal();
                }

                // subscribe using the handler, auto ack false
                ConsumerConfiguration cc  = ConsumerConfiguration.Builder().WithAckWait(500).Build();
                PushSubscribeOptions pso2 = PushSubscribeOptions.Builder()
                                            .WithDurable(Durable(2)).WithConfiguration(cc).Build();
                asub = js.PushSubscribeAsync(SUBJECT, Handler2, false, pso2);

                // wait for messages to arrive using the countdown latch.
                latch2.Wait();
                Assert.Equal(10, handlerReceived2);

                Thread.Sleep(2000); // just give it time for the server to realize the messages are not ack'ed

                asub.Unsubscribe();

                // check that we get all the messages again
                ssub = js.PushSubscribeSync(SUBJECT, pso2);
                Assert.Equal(10, ReadMessagesAck(ssub).Count);
            });
        }
コード例 #4
0
        public KeyValueWatchSubscription(KeyValue kv, string keyPattern,
                                         IKeyValueWatcher watcher, params KeyValueWatchOption[] watchOptions)
        {
            string subject = kv.RawKeySubject(keyPattern);

            // figure out the result options
            bool          headersOnly    = false;
            bool          includeDeletes = true;
            DeliverPolicy deliverPolicy  = DeliverPolicy.LastPerSubject;

            foreach (KeyValueWatchOption wo in watchOptions)
            {
                switch (wo)
                {
                case KeyValueWatchOption.MetaOnly: headersOnly = true; break;

                case KeyValueWatchOption.IgnoreDelete: includeDeletes = false; break;

                case KeyValueWatchOption.UpdatesOnly: deliverPolicy = DeliverPolicy.New; break;

                case KeyValueWatchOption.IncludeHistory: deliverPolicy = DeliverPolicy.All; break;
                }
            }

            if (deliverPolicy == DeliverPolicy.New)
            {
                endOfDataSent = new InterlockedBoolean(true);
                watcher.EndOfData();
            }
            else
            {
                KeyValueEntry kveCheckPending = kv._kvGetLastMessage(keyPattern);
                if (kveCheckPending == null)
                {
                    endOfDataSent = new InterlockedBoolean(true);
                    watcher.EndOfData();
                }
                else
                {
                    endOfDataSent = new InterlockedBoolean(false);
                }
            }

            PushSubscribeOptions pso = PushSubscribeOptions.Builder()
                                       .WithStream(kv.StreamName)
                                       .WithOrdered(true)
                                       .WithConfiguration(
                ConsumerConfiguration.Builder()
                .WithAckPolicy(AckPolicy.None)
                .WithDeliverPolicy(deliverPolicy)
                .WithHeadersOnly(headersOnly)
                .WithFilterSubject(subject)
                .Build())
                                       .Build();

            EventHandler <MsgHandlerEventArgs> handler = (sender, args) =>
            {
                KeyValueEntry kve = new KeyValueEntry(args.msg);
                if (includeDeletes || kve.Operation.Equals(KeyValueOperation.Put))
                {
                    watcher.Watch(kve);
                }

                if (endOfDataSent.IsFalse() && kve.Delta == 0)
                {
                    endOfDataSent.Set(true);
                    watcher.EndOfData();
                }
            };

            sub = kv.js.PushSubscribeAsync(subject, handler, false, pso);
            if (endOfDataSent.IsFalse())
            {
                ulong pending = sub.GetConsumerInformation().CalculatedPending;
                if (pending == 0)
                {
                    endOfDataSent.Set(true);
                    watcher.EndOfData();
                }
            }
        }