Exemplo n.º 1
0
        public static void Main(string[] args)
        {
            ArgumentHelper helper = new ArgumentHelperBuilder("JetStream Publish", args, Usage)
                                    .DefaultStream("example-stream")
                                    .DefaultSubject("example-subject")
                                    .DefaultPayload("Hello")
                                    .DefaultCount(10)
                                    .Build();

            try
            {
                using (IConnection c = new ConnectionFactory().CreateConnection(helper.MakeOptions()))
                {
                    // Use the utility to create a stream stored in memory.
                    JsUtils.CreateStreamOrUpdateSubjects(c, helper.Stream, helper.Subject);

                    // create a JetStream context
                    IJetStream js = c.CreateJetStreamContext();

                    int stop = helper.Count < 2 ? 2 : helper.Count + 1;
                    for (int x = 1; x < stop; x++)
                    {
                        // make unique message data if you want more than 1 message
                        byte[] data = helper.Count < 2
                            ? Encoding.UTF8.GetBytes(helper.Payload)
                            : Encoding.UTF8.GetBytes(helper.Payload + "-" + x);

                        // Publish a message and print the results of the publish acknowledgement.
                        Msg msg = new Msg(helper.Subject, null, helper.Header, data);

                        // We'll use the defaults for this simple example, but there are options
                        // to constrain publishing to certain streams, expect sequence numbers and
                        // more. See the JetStreamPublishWithOptionsUseCases example for details.
                        // An exception will be thrown if there is a failure.
                        PublishAck pa = js.Publish(msg);
                        Console.WriteLine("Published message '{0}' on subject '{1}', stream '{2}', seqno '{3}'.",
                                          Encoding.UTF8.GetString(data), helper.Subject, pa.Stream, pa.Seq);
                    }
                }
            }
            catch (Exception ex)
            {
                helper.ReportException(ex);
            }
        }
Exemplo n.º 2
0
        public static void Main(string[] args)
        {
            ArgumentHelper helper = new ArgumentHelperBuilder("JetStream Publish Async", args, Usage)
                                    .DefaultStream("example-stream")
                                    .DefaultSubject("example-subject")
                                    .DefaultPayload("Hello")
                                    .DefaultCount(10)
                                    .Build();

            try
            {
                using (IConnection c = new ConnectionFactory().CreateConnection(helper.MakeOptions()))
                {
                    // Use the utility to create a stream stored in memory.
                    JsUtils.CreateStreamOrUpdateSubjects(c, helper.Stream, helper.Subject);

                    // create a JetStream context
                    IJetStream js = c.CreateJetStreamContext();

                    IList <Task <PublishAck> > tasks = new List <Task <PublishAck> >();

                    int stop = helper.Count < 2 ? 2 : helper.Count + 1;
                    for (int x = 1; x < stop; x++)
                    {
                        // make unique message data if you want more than 1 message
                        byte[] data = helper.Count < 2
                            ? Encoding.UTF8.GetBytes(helper.Payload)
                            : Encoding.UTF8.GetBytes(helper.Payload + "-" + x);

                        // Publish a message and print the results of the publish acknowledgement.
                        Msg msg = new Msg(helper.Subject, null, helper.Header, data);

                        // We'll use the defaults for this simple example, but there are options
                        // to constrain publishing to certain streams, expect sequence numbers and
                        // more. See the JetStreamPublishWithOptionsUseCases example for details.
                        // An exception will be thrown if there is a failure.
                        tasks.Add(js.PublishAsync(msg));

                        while (tasks.Count > 0)
                        {
                            Task <PublishAck> task = tasks[0];
                            tasks.RemoveAt(0);

                            if (task.IsCompleted)
                            {
                                try
                                {
                                    PublishAck pa = task.Result;
                                    Console.WriteLine("Published message {0} on subject {1}, stream {2}, seqno {3}.",
                                                      Encoding.UTF8.GetString(data), helper.Subject, pa.Stream, pa.Seq);
                                }
                                catch (Exception e)
                                {
                                    Console.WriteLine("Publish Failed: " + e);
                                }
                            }
                            else
                            {
                                // re queue so will be checked for completed again
                                tasks.Add(task);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                helper.ReportException(ex);
            }
        }