Ejemplo n.º 1
0
        static void Main(string[] args)

        {
            var senders = new KubeMQ.SDK.csharp.Queue.Queue("", "queue-stream-tester", address);

            var counter    = 0;
            var roundsDone = false;

            do
            {
                counter++;
                if (rounds > 0 && counter >= rounds)
                {
                    roundsDone = true;
                }
                else
                {
                    roundsDone = false;
                }

                for (int i = 0; i < tasks; i++)
                {
                    var            channel = $"{queue}.{i+1}";
                    List <Message> msgs    = new List <Message>();
                    for (int t = 0; t < send; t++)
                    {
                        msgs.Add(new KubeMQ.SDK.csharp.Queue.Message
                        {
                            MessageID = i.ToString(),
                            Queue     = channel,
                            Body      = KubeMQ.SDK.csharp.Tools.Converter.ToByteArray($"im Message {t}"),
                            Metadata  = "some-metadata",
                            Tags      = new Dictionary <string, string>()/* ("Action", $"Batch_{testGui}_{i}")*/
                            {
                                { "Action", $"Batch_{t}" }
                            }
                        });
                    }
                    var resBatch = senders.Batch(msgs);
                    if (!resBatch.HaveErrors)
                    {
                        continue;
                    }
                    Console.WriteLine($"message sent batch has errors");
                    System.Environment.Exit(1);
                }

                Console.WriteLine($"sending {send*tasks} messages completed");
                Stopwatch watch = new Stopwatch();
                watch.Start();
                RunTasks(tasks, false).Wait();
                watch.Stop();
                Console.WriteLine($"Receiveing {send*tasks} messages takes " + watch.ElapsedMilliseconds + " milliseconds");
                Thread.Sleep(1000);
            } while (!roundsDone);
        }
Ejemplo n.º 2
0
        static private async Task GetMessagesInTransaction(int id)
        {
            await Task.Run(() =>
            {
                var receiveCounter = 0;
                KubeMQ.SDK.csharp.Queue.Queue localReceiver = new KubeMQ.SDK.csharp.Queue.Queue($"{queue}.{id + 1}", $"local-queue-receiver-{id+1}", address);
                while (receiveCounter < send)
                {
                    var transaction = localReceiver.CreateTransaction();
                    KubeMQ.SDK.csharp.Queue.Stream.TransactionMessagesResponse resRec;
                    try
                    {
                        resRec = transaction.Receive(10, 15);
                        if (resRec.IsError)
                        {
                            continue;
                        }
                        else
                        {
                            try
                            {
                                Thread.Sleep(ackDelay);
                                var ackRes = transaction.AckMessage(resRec.Message.Attributes.Sequence);
                                if (ackRes.IsError)
                                {
                                    Console.WriteLine($"Error in ack Message, error:{ackRes.Error}");
                                    continue;
                                }

                                receiveCounter++;
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine(e);
                            }
                        }
                    }
                    catch (System.Exception ex)
                    {
                        Console.WriteLine($"Message Receive error, error:{ex.Message}");
                    }
                    finally
                    {
                        transaction.Close();
                    }
                }
                ;
            });
        }
Ejemplo n.º 3
0
 internal static IEnumerable <QueueMessage> ToQueueMessages(IEnumerable <Message> queueMessages, KubeMQ.SDK.csharp.Queue.Queue queue)
 {
     foreach (var item in queueMessages)
     {
         item.Queue    = item.Queue ?? queue.QueueName;
         item.ClientID = item.ClientID ?? queue.ClientID;
         yield return(ConvertQueueMessage(item));
     }
 }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Console.WriteLine($"[Demo] ClientID:{ClientID}");
            Console.WriteLine($"[Demo] QueueName:{QueueName}");
            Console.WriteLine($"[Demo] KubeMQServerAddress:{KubeMQServerAddress}");

            KubeMQ.SDK.csharp.Queue.Queue queue = null;
            try
            {
                queue = new KubeMQ.SDK.csharp.Queue.Queue(QueueName, ClientID, KubeMQServerAddress);
            }
            catch (System.Exception ex)
            {
                Console.WriteLine($"Error while pinging to kubeMQ address:{ex.Message}");

                Console.WriteLine($"Error while pinging to kubeMQ address:{queue.ServerAddress}");
                Console.ReadLine();
            }

            #region "non tran"


            //Simple send message
            var res = queue.SendQueueMessage(new KubeMQ.SDK.csharp.Queue.Message
            {
                Body     = KubeMQ.SDK.csharp.Tools.Converter.ToByteArray("hi, new message"),
                Metadata = "Meta",
                Tags     = new Dictionary <string, string>()
                {
                    { "Action", $"SendQueueMessage_{testGui}" }
                }
            });
            if (res.IsError)
            {
                Console.WriteLine($"message enqueue error, error:{res.Error}");
            }
            else
            {
                Console.WriteLine($"message sent at, {res.SentAt}");
            }


            //Simple peak message
            var peakmsg = queue.PeakQueueMessage(1);
            {
                if (peakmsg.IsError)
                {
                    Console.WriteLine($"message peak error, error:{peakmsg.Error}");
                }
                foreach (var item in peakmsg.Messages)
                {
                    Console.WriteLine($"message received body:{KubeMQ.SDK.csharp.Tools.Converter.FromByteArray(item.Body)}");
                }
            }


            //Simple send Bulk of messages
            List <Message> msgs = new List <Message>();
            for (int i = 0; i < 5; i++)
            {
                msgs.Add(new KubeMQ.SDK.csharp.Queue.Message
                {
                    MessageID = i.ToString(),
                    Body      = KubeMQ.SDK.csharp.Tools.Converter.ToByteArray($"im Message {i}"),
                    Metadata  = "Meta",
                    Tags      = new Dictionary <string, string>()/* ("Action", $"Batch_{testGui}_{i}")*/
                    {
                        { "Action", $"Batch_{testGui}_{i}" }
                    }
                });
            }

            //Batch send messages
            var resBatch = queue.SendQueueMessagesBatch(msgs);
            if (resBatch.HaveErrors)
            {
                Console.WriteLine($"message sent batch has errors");
            }
            foreach (var item in resBatch.Results)
            {
                if (item.IsError)
                {
                    Console.WriteLine($"message enqueue error, error:{res.Error}");
                }
                else
                {
                    Console.WriteLine($"message sent at, {res.SentAt}");
                }
            }

            //Queue receive messages
            var msg = queue.ReceiveQueueMessages();
            if (msg.IsError)
            {
                Console.WriteLine($"message dequeue error, error:{msg.Error}");
            }
            foreach (var item in msg.Messages)
            {
                Console.WriteLine($"message received body:{KubeMQ.SDK.csharp.Tools.Converter.FromByteArray(item.Body)}");
            }

            #endregion

            #region "Tran"

            //Transaction queue for rerouting messages
            msgs = new List <Message>();
            for (int i = 0; i < 5; i++)
            {
                msgs.Add(new KubeMQ.SDK.csharp.Queue.Message
                {
                    Body     = KubeMQ.SDK.csharp.Tools.Converter.ToByteArray($"i'm Tran Message {i}"),
                    Metadata = "Meta",
                    Tags     = new Dictionary <string, string>()
                    {
                        { "Action", $"Batch_{testGui}_{i}" }
                    }
                });
            }

            resBatch = queue.SendQueueMessagesBatch(msgs);
            if (resBatch.HaveErrors)
            {
                Console.WriteLine($"message sent batch has errors");
            }
            foreach (var item in resBatch.Results)
            {
                if (item.IsError)
                {
                    Console.WriteLine($"message enqueue error, error:{res.Error}");
                }
                else
                {
                    Console.WriteLine($"message sent at, {res.SentAt}");
                }
            }


            //create a new transaction stream instance
            var transaction = queue.CreateTransaction();
            Console.WriteLine($"Transaction status:");
            TransactionMessagesResponse ms;

            //receive a massage with visibility of 5 seconds
            ms = transaction.Receive(5);
            if (ms.IsError)
            {
                Console.WriteLine($"message dequeue error, error:{ms.Error}");
                return;
            }

            var qm = ms.Message;

            //Modify msg to ExtendVisibility to 5 seconds
            ms = transaction.ExtendVisibility(5);
            if (ms.IsError)
            {
                Console.WriteLine($"message dequeue error, error:{ms.Error}");
                return;
            }

            Thread.Sleep(1000);

            //Acknowledge message
            transaction.AckMessage(qm.Attributes.Sequence);
            //Close the transaction
            transaction.Close();


            #endregion
        }