public static void Main(string[] args)
        {
            ArgumentHelper helper = new ArgumentHelperBuilder("Pull Subscription using primitive No Wait, Use Cases", args, Usage)
                                    .DefaultStream("nowait-uc-stream")
                                    .DefaultSubject("nowait-uc-subject")
                                    .DefaultDurable("nowait-uc-durable")
                                    .Build();

            try
            {
                using (IConnection c = new ConnectionFactory().CreateConnection(helper.MakeOptions()))
                {
                    // Create a JetStreamManagement context.
                    IJetStreamManagement jsm = c.CreateJetStreamManagementContext();

                    // Use the utility to create a stream stored in memory.
                    JsUtils.CreateStreamExitWhenExists(jsm, helper.Stream, helper.Subject);

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

                    // Build our subscription options. Durable is REQUIRED for pull based subscriptions
                    PullSubscribeOptions pullOptions = PullSubscribeOptions.Builder()
                                                       .WithDurable(helper.Durable) // required
                                                       .Build();

                    // 0.1 Initialize. subscription
                    // 0.2 DO NOT start the pull, no wait works differently than regular pull.
                    //     With no wait, we have to start the pull the first time and every time the
                    //     batch size is exhausted or no waits out.
                    // 0.3 Flush outgoing communication with/to the server, useful when app is both publishing and subscribing.
                    Console.WriteLine("\n----------\n0. Initialize the subscription and pull.");
                    IJetStreamPullSubscription sub = js.PullSubscribe(helper.Subject, pullOptions);
                    c.Flush(1000);

                    // 1. Start the pull, but there are no messages yet.
                    // -  Read the messages
                    // -  Since there are less than the batch size, we get them all (0)
                    Console.WriteLine("----------\n1. There are no messages yet");
                    sub.PullNoWait(10);
                    IList <Msg> messages = JsUtils.ReadMessagesAck(sub);
                    Console.WriteLine("We should have received 0 total messages, we received: " + messages.Count);

                    // 2. Publish 10 messages
                    // -  Start the pull
                    // -  Read the messages
                    // -  Since there are exactly the batch size we get them all
                    Console.WriteLine("----------\n2. Publish 10 which satisfies the batch");
                    JsUtils.Publish(js, helper.Subject, "A", 10);
                    sub.PullNoWait(10);
                    messages = JsUtils.ReadMessagesAck(sub);
                    Console.WriteLine("We should have received 10 total messages, we received: " + messages.Count);

                    // 3. Publish 20 messages
                    // -  Start the pull
                    // -  Read the messages
                    Console.WriteLine("----------\n3. Publish 20 which is larger than the batch size.");
                    JsUtils.Publish(js, helper.Subject, "B", 20);
                    sub.PullNoWait(10);
                    messages = JsUtils.ReadMessagesAck(sub);
                    Console.WriteLine("We should have received 10 total messages, we received: " + messages.Count);

                    // 4. There are still messages left from the last
                    // -  Start the pull
                    // -  Read the messages
                    Console.WriteLine("----------\n4. Get the rest of the publish.");
                    sub.PullNoWait(10);
                    messages = JsUtils.ReadMessagesAck(sub);
                    Console.WriteLine("We should have received 10 total messages, we received: " + messages.Count);

                    // 5. Publish 5 messages
                    // -  Start the pull
                    // -  Read the messages
                    Console.WriteLine("----------\n5. Publish 5 which is less than batch size.");
                    JsUtils.Publish(js, helper.Subject, "C", 5);
                    sub.PullNoWait(10);
                    messages = JsUtils.ReadMessagesAck(sub);
                    Console.WriteLine("We should have received 5 total messages, we received: " + messages.Count);

                    // 6. Publish 14 messages
                    // -  Start the pull
                    // -  Read the messages
                    // -  we do NOT get a nowait status message if there are more or equals messages than the batch
                    Console.WriteLine("----------\n6. Publish 14 which is more than the batch size.");
                    JsUtils.Publish(js, helper.Subject, "D", 14);
                    sub.PullNoWait(10);
                    messages = JsUtils.ReadMessagesAck(sub);
                    Console.WriteLine("We should have received 10 total messages, we received: " + messages.Count);

                    // 7. There are 4 messages left
                    // -  Start the pull
                    // -  Read the messages
                    // -  Since there are less than batch size the last message we get will be a status 404 message.
                    Console.WriteLine("----------\n7. There are 4 messages left, which is less than the batch size.");
                    sub.PullNoWait(10);
                    messages = JsUtils.ReadMessagesAck(sub);
                    Console.WriteLine("We should have received 4 messages, we received: " + messages.Count);

                    Console.WriteLine("----------\n");

                    // delete the stream since we are done with it.
                    jsm.DeleteStream(helper.Stream);
                }
            }
            catch (Exception ex)
            {
                helper.ReportException(ex);
            }
        }
        public static void Main(string[] args)
        {
            ArgumentHelper helper = new ArgumentHelperBuilder("Pull Subscription using primitive Expires In, Use Cases", args, Usage)
                                    .DefaultStream("expires-in-uc-stream")
                                    .DefaultSubject("expires-in-uc-subject")
                                    .DefaultDurable("expires-in-uc-durable")
                                    .Build();

            try
            {
                using (IConnection c = new ConnectionFactory().CreateConnection(helper.MakeOptions()))
                {
                    // Create a JetStreamManagement context.
                    IJetStreamManagement jsm = c.CreateJetStreamManagementContext();

                    // Use the utility to create a stream stored in memory.
                    JsUtils.CreateStreamExitWhenExists(jsm, helper.Stream, helper.Subject);

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

                    // Build our subscription options. Durable is REQUIRED for pull based subscriptions
                    PullSubscribeOptions pullOptions = PullSubscribeOptions.Builder()
                                                       .WithDurable(helper.Durable) // required
                                                       .Build();

                    // 0.1 Initialize. subscription
                    // 0.2 Flush outgoing communication with/to the server, useful when app is both JsUtils.Publishing and subscribing.
                    // 0.3 Start the pull, you don't have to call this again because AckMode.NEXT
                    // -  When we ack a batch message the server starts preparing or adding to the next batch.
                    Console.WriteLine("\n----------\n0. Initialize the subscription and pull.");
                    IJetStreamPullSubscription sub = js.PullSubscribe(helper.Subject, pullOptions);
                    c.Flush(1000);

                    // 1. Publish some that is less than the batch size.
                    Console.WriteLine("\n----------\n1. Publish some amount of messages, but not entire batch size.");
                    JsUtils.Publish(js, helper.Subject, "A", 6);
                    sub.PullExpiresIn(10, 1200);
                    IList <Msg> messages = JsUtils.ReadMessagesAck(sub, timeout: 2000);
                    Console.WriteLine("We should have received 6 total messages, we received: " + messages.Count);

                    // 2. Publish some more covering our pull size...
                    Console.WriteLine("----------\n2. Publish more than the batch size.");
                    sub.PullExpiresIn(10, 1200);
                    JsUtils.Publish(js, helper.Subject, "B", 14);
                    messages = JsUtils.ReadMessagesAck(sub, timeout: 2000);
                    Console.WriteLine("We should have received 10 total messages, we received: " + messages.Count);

                    // 3. There are still 4 messages from B, but the batch was finished
                    // -  won't get any messages until a pull is issued.
                    Console.WriteLine("----------\n3. Read without issuing a pull.");
                    messages = JsUtils.ReadMessagesAck(sub, timeout: 2000);
                    Console.WriteLine("We should have received 0 total messages, we received: " + messages.Count);

                    // 4. re-issue the pull to get the last 4
                    Console.WriteLine("----------\n4. Issue the pull to get the last 4.");
                    sub.PullExpiresIn(10, 1200);
                    messages = JsUtils.ReadMessagesAck(sub, timeout: 2000);
                    Console.WriteLine("We should have received 4 total messages, we received: " + messages.Count);

                    // 5. publish a lot of messages
                    Console.WriteLine("----------\n5. Publish a lot of messages. The last pull was under the batch size.");
                    Console.WriteLine("   Issue another pull with batch size less than number of messages.");
                    JsUtils.Publish(js, helper.Subject, "C", 25);
                    sub.PullExpiresIn(10, 1200);
                    messages = JsUtils.ReadMessagesAck(sub, timeout: 2000);
                    Console.WriteLine("We should have received 10 total messages, we received: " + messages.Count);

                    // 6. there are still more messages
                    Console.WriteLine("----------\n6. Still more messages. Issue another pull with batch size less than number of messages.");
                    sub.PullExpiresIn(10, 1200);
                    messages = JsUtils.ReadMessagesAck(sub, timeout: 2000);
                    Console.WriteLine("We should have received 10 total messages, we received: " + messages.Count);

                    // 7. there are still more messages
                    Console.WriteLine("----------\n7. Still more messages. Issue another pull with batch size more than number of messages.");
                    sub.PullExpiresIn(10, 1200);
                    messages = JsUtils.ReadMessagesAck(sub, timeout: 2000);
                    Console.WriteLine("We should have received 5 total messages, we received: " + messages.Count);

                    // 8. we got them all
                    Console.WriteLine("----------\n8. No messages left.");
                    sub.PullExpiresIn(10, 1200);
                    messages = JsUtils.ReadMessagesAck(sub, timeout: 2000);
                    Console.WriteLine("We should have received 0 total messages, we received: " + messages.Count);

                    Console.WriteLine("----------\n");

                    // delete the stream since we are done with it.
                    jsm.DeleteStream(helper.Stream);
                }
            }
            catch (Exception ex)
            {
                helper.ReportException(ex);
            }
        }
예제 #3
0
        public static void Main(string[] args)
        {
            ArgumentHelper helper = new ArgumentHelperBuilder("Pull Subscription using primitive Batch Size, Use Cases", args, Usage)
                                    .DefaultStream("pull-stream")
                                    .DefaultSubject("pull-subject")
                                    .DefaultDurable("pull-durable")
                                    .Build();

            try
            {
                using (IConnection c = new ConnectionFactory().CreateConnection(helper.MakeOptions()))
                {
                    // Create a JetStreamManagement context.
                    IJetStreamManagement jsm = c.CreateJetStreamManagementContext();

                    // Use the utility to create a stream stored in memory.
                    JsUtils.CreateStreamExitWhenExists(jsm, helper.Stream, helper.Subject);

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

                    // Build our subscription options. Durable is REQUIRED for pull based subscriptions
                    PullSubscribeOptions pullOptions = PullSubscribeOptions.Builder()
                                                       .WithDurable(helper.Durable) // required
                                                       .Build();

                    // 0.1 Initialize. subscription
                    // 0.2 Flush outgoing communication with/to the server, useful when app is both JsUtils.Publishing and subscribing.
                    // 0.3 Start the pull, you don't have to call this again because AckMode.NEXT
                    // -  When we ack a batch message the server starts preparing or adding to the next batch.
                    Console.WriteLine("\n----------\n0. Initialize the subscription and pull.");
                    IJetStreamPullSubscription sub = js.PullSubscribe(helper.Subject, pullOptions);
                    c.Flush(1000);

                    sub.Pull(10);

                    // 1. JsUtils.Publish some that is less than the batch size.
                    // -  Do this first as data will typically be published first.
                    Console.WriteLine("----------\n1. JsUtils.Publish some amount of messages, but not entire batch size.");
                    JsUtils.Publish(js, helper.Subject, "A", 4);
                    IList <Msg> messages = JsUtils.ReadMessagesAck(sub);
                    Console.WriteLine("We should have received 4 total messages, we received: " + messages.Count);

                    // 2. JsUtils.Publish some more covering our pull size...
                    // -  Read what is available, expect only 6 b/c 4 + 6 = 10
                    Console.WriteLine("----------\n2. JsUtils.Publish more than the remaining batch size.");
                    JsUtils.Publish(js, helper.Subject, "B", 10);
                    messages = JsUtils.ReadMessagesAck(sub);
                    Console.WriteLine("We should have received 6 total messages, we received: " + messages.Count);

                    // 3. There are still 4 messages from B, but the batch was finished
                    // -  won't get any messages until a pull is issued.
                    Console.WriteLine("----------\n3. Read without re-issue.");
                    messages = JsUtils.ReadMessagesAck(sub);
                    Console.WriteLine("We should have received 0 total messages, we received: " + messages.Count);

                    // 4. re-issue the pull to get the last 4
                    Console.WriteLine("----------\n4. Re-issue to get the last 4.");
                    sub.Pull(10);
                    messages = JsUtils.ReadMessagesAck(sub);
                    Console.WriteLine("We should have received 4 total messages, we received: " + messages.Count);

                    Console.WriteLine("----------\n");

                    // delete the stream since we are done with it.
                    jsm.DeleteStream(helper.Stream);
                }
            }
            catch (Exception ex)
            {
                helper.ReportException(ex);
            }
        }