public void TestValidBuilderArgs() { PublishOptions.PublishOptionsBuilder builder = PublishOptions.Builder() .WithExpectedStream("expectedstream") .WithExpectedLastMsgId("expectedmsgid") .WithExpectedLastSequence(42) .WithExpectedLastSubjectSequence(43) .WithMessageId("msgid") .WithStream("stream") .WithTimeout(5150); var po = builder.Build(); Assert.Equal("expectedstream", po.ExpectedStream); Assert.Equal("expectedmsgid", po.ExpectedLastMsgId); Assert.Equal(42ul, po.ExpectedLastSeq); Assert.Equal(43ul, po.ExpectedLastSubjectSeq); Assert.Equal("msgid", po.MessageId); Assert.Equal("stream", po.Stream); Assert.Equal(5150, po.StreamTimeout.Millis); po = builder.ClearExpected().Build(); Assert.Null(po.ExpectedLastMsgId); Assert.Equal(PublishOptions.DefaultLastSequence, po.ExpectedLastSeq); Assert.Equal(PublishOptions.DefaultLastSequence, po.ExpectedLastSubjectSeq); Assert.Null(po.MessageId); Assert.Equal("stream", po.Stream); po = PublishOptions.Builder(). WithTimeout(Duration.OfMillis(5150)). Build(); Assert.Equal(Duration.OfMillis(5150), po.StreamTimeout); // check to allow - PublishOptions.Builder(). WithExpectedStream("test-stream"). Build(); }
public static void Main(string[] args) { ArgumentHelper helper = new ArgumentHelperBuilder("JetStream Publish With Options Use Cases", args, Usage) .DefaultStream("pubopts-stream") .DefaultSubject("pubopts-subject") .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); // get a regular context IJetStream js = c.CreateJetStreamContext(); PublishOptions.PublishOptionsBuilder builder = PublishOptions.Builder() .WithExpectedStream(helper.Stream) .WithMessageId("mid1"); PublishAck pa = js.Publish(helper.Subject, Encoding.ASCII.GetBytes("message1"), builder.Build()); Console.WriteLine("Published message on subject {0}, stream {1}, seqno {2}.", helper.Subject, pa.Stream, pa.Seq); // IMPORTANT! // You can reuse the builder in 2 ways. // 1. Manually set a field to null or to DefaultLastSequence if you want to clear it out. // 2. Use the clearExpected method to clear the expectedLastId, expectedLastSequence and messageId fields // Manual re-use 1. Clearing some fields builder .WithExpectedLastMsgId("mid1") .WithExpectedLastSequence(PublishOptions.DefaultLastSequence) .WithMessageId(null); pa = js.Publish(helper.Subject, Encoding.ASCII.GetBytes("message2"), builder.Build()); Console.WriteLine("Published message on subject {0}, stream {1}, seqno {2}.", helper.Subject, pa.Stream, pa.Seq); // Manual re-use 2. Setting all the expected fields again builder .WithExpectedLastMsgId(null) .WithExpectedLastSequence(PublishOptions.DefaultLastSequence) .WithMessageId("mid3"); pa = js.Publish(helper.Subject, Encoding.ASCII.GetBytes("message3"), builder.Build()); Console.WriteLine("Published message on subject {0}, stream {1}, seqno {2}.", helper.Subject, pa.Stream, pa.Seq); // reuse() method clears all the fields, then we set some fields. builder.ClearExpected() .WithExpectedLastSequence(pa.Seq) .WithMessageId("mid4"); pa = js.Publish(helper.Subject, Encoding.ASCII.GetBytes("message4"), builder.Build()); Console.WriteLine("Published message on subject {0}, stream {1}, seqno {2}.", helper.Subject, pa.Stream, pa.Seq); // exception when the expected stream does not match [10060] try { PublishOptions errOpts = PublishOptions.Builder().WithExpectedStream("wrongStream").Build(); js.Publish(helper.Subject, Encoding.ASCII.GetBytes("ex1"), errOpts); } catch (NATSJetStreamException e) { Console.WriteLine($"Exception was: '{e.ErrorDescription}'"); } // exception with wrong last msg ID [10070] try { PublishOptions errOpts = PublishOptions.Builder().WithExpectedLastMsgId("wrongId").Build(); js.Publish(helper.Subject, Encoding.ASCII.GetBytes("ex2"), errOpts); } catch (NATSJetStreamException e) { Console.WriteLine($"Exception was: '{e.ErrorDescription}'"); } // exception with wrong last sequence wrong last sequence: 4 [10071] try { PublishOptions errOpts = PublishOptions.Builder().WithExpectedLastSequence(999).Build(); js.Publish(helper.Subject, Encoding.ASCII.GetBytes("ex3"), errOpts); } catch (NATSJetStreamException e) { Console.WriteLine($"Exception was: '{e.ErrorDescription}'"); } // delete the stream since we are done with it. jsm.DeleteStream(helper.Stream); } } catch (Exception ex) { helper.ReportException(ex); } }