/// <summary> /// Method for standard sending of messages /// </summary> /// <param name="sender">sender link</param> /// <param name="options">options</param> private void Send(SenderLink sender, SenderOptions options) { int nSent = 0; Message message; while ((nSent < options.MsgCount)) { message = CreateMessage(options, nSent); if ((options.Duration > 0) && (options.DurationMode == "before-send")) { Utils.Sleep4Next(ts, options.MsgCount, (options.Duration), nSent + 1); } sender.Send(message, options.Timeout); if ((options.Duration > 0) && ((options.DurationMode == "after-send-before-tx-action") || (options.DurationMode == "after-send-after-tx-action"))) { Utils.Sleep4Next(ts, options.MsgCount, (options.Duration), nSent + 1); } Formatter.LogMessage(message, options); nSent++; } }
/// <summary> /// Prepare sender link with options /// </summary> /// <param name="options">sender options</param> /// <returns>built sender link</returns> private SenderLink PrepareSender(SenderOptions options) { Attach attach = new Attach() { Source = new Source(), Target = new Target() { Address = options.Address }, SndSettleMode = options.Settlement.GetSenderFlag(), RcvSettleMode = options.Settlement.GetReceiverFlag() }; return(new SenderLink(this.session, "sender-spout", attach, null)); }
/// <summary> /// Method create method content /// </summary> /// <param name="options">options from parse arguments from cmd</param> /// <returns>content</returns> static object CreateMsgContent(SenderOptions options, int indexOfMessage) { object content = String.Empty; if (!(String.IsNullOrEmpty(options.Content))) { content = options.Content; } else if (options.ListContent.Count > 0) { content = options.ListContent; } else if (options.MapContent.Count > 0) { content = options.MapContent; } else if (!(String.IsNullOrEmpty(options.ContentFromFile))) { content = options.ContentFromFile; } return(content); }
/// <summary> /// Method create message /// </summary> /// <param name="options">options from parse arguments from cmd</param> /// <param name="nSent">count of send message</param> /// <returns>message</returns> static Message CreateMessage(SenderOptions options, int nSent) { object content = CreateMsgContent(options, nSent); Message msg = new Message(content) { //msg properties Properties = new Properties() }; if (!String.IsNullOrEmpty(options.Id)) { msg.Properties.MessageId = options.Id; } if (!String.IsNullOrEmpty(options.CorrelationId)) { msg.Properties.CorrelationId = options.CorrelationId; } if (!String.IsNullOrEmpty(options.Subject)) { msg.Properties.Subject = options.Subject; } if (!String.IsNullOrEmpty(options.ContentType)) { msg.Properties.ContentType = options.ContentType; } if (options.UserId != null && options.UserId.Length != 0) { msg.Properties.UserId = options.UserId; } if (!String.IsNullOrEmpty(options.ReplyTo)) { msg.Properties.ReplyTo = options.ReplyTo; } if (options.MessageAnnotations.Map.Count > 0) { msg.MessageAnnotations = options.MessageAnnotations; } if (!String.IsNullOrEmpty(options.GroupId)) { msg.Properties.GroupId = options.GroupId; } if (!String.IsNullOrEmpty(options.GroupId)) { msg.Properties.GroupId = options.GroupId; } msg.Properties.GroupSequence = (uint)options.GroupSequence; if (!String.IsNullOrEmpty(options.ReplyToGroupId)) { msg.Properties.ReplyToGroupId = options.ReplyToGroupId; } if (!String.IsNullOrEmpty(options.To)) { msg.Properties.To = options.To; } //set up message header msg.Header = new Header(); if (options.Durable.HasValue) { msg.Header.Durable = options.Durable.Value; } if (options.Priority.HasValue) { msg.Header.Priority = options.Priority.Value; } if (options.Ttl.HasValue) { msg.Header.Ttl = options.Ttl.Value; } //set up application properties if (options.Properties.Count > 0) { msg.ApplicationProperties = new ApplicationProperties(); foreach (KeyValuePair <string, object> p in options.Properties) { msg.ApplicationProperties[p.Key.ToString()] = p.Value; } } return(msg); }
/// <summary> /// Main method of sender /// </summary> /// <param name="args">args from command line</param> /// <returns>int status exit code</returns> public void Run(string[] args) { SenderOptions options = new SenderOptions(); try { this.ParseArguments(args, options); //init timestamping this.ptsdata = Utils.TsInit(options.LogStats); Utils.TsSnapStore(this.ptsdata, 'B', options.LogStats); this.SetAddress(options.Url); this.CreateConnection(options); Utils.TsSnapStore(this.ptsdata, 'C', options.LogStats); this.CreateSession(); Utils.TsSnapStore(this.ptsdata, 'D', options.LogStats); SenderLink sender = this.PrepareSender(options); //enable transactions bool tx_batch_flag = String.IsNullOrEmpty(options.TxLoopendAction) ? (options.TxSize > 0) : true; Stopwatch stopwatch = new Stopwatch(); TimeSpan timespan = options.Timeout; stopwatch.Start(); this.ts = Utils.GetTime(); Utils.TsSnapStore(this.ptsdata, 'E', options.LogStats); //sending of messages if (tx_batch_flag) { this.TransactionSend(sender, options); } else { this.Send(sender, options); } if (options.LogStats.IndexOf("endpoints") > -1) { Dictionary <string, object> stats = GetSenderStats(sender); Formatter.PrintStatistics(stats); } Utils.TsSnapStore(this.ptsdata, 'F', options.LogStats); //close-sleep if (options.CloseSleep > 0) { System.Threading.Thread.Sleep(options.CloseSleep); } ///close connection and link this.CloseLink(sender); this.CloseConnection(); Utils.TsSnapStore(this.ptsdata, 'G', options.LogStats); if (this.ptsdata.Count > 0) { Console.WriteLine("STATS " + Utils.TsReport(this.ptsdata, options.MsgCount, options.Content.Length * sizeof(Char), 0)); } this.exitCode = ReturnCode.ERROR_SUCCESS; } catch (ArgumentException ex) { this.ArgumentExceptionHandler(ex, options); } catch (Exception ex) { this.OtherExceptionHandler(ex, options); } finally { this.CloseConnection(); } Environment.Exit(this.exitCode); }
/// <summary> /// Method for transactional sending of messages /// </summary> /// <param name="sender">sender link</param> /// <param name="options">options</param> private void TransactionSend(SenderLink sender, SenderOptions options) { int nSent = 0; bool txFlag = true; Message message; while (txFlag && options.TxSize > 0) { using (var txs = new TransactionScope(TransactionScopeOption.RequiresNew)) { for (int i = 0; i < options.TxSize; i++) { message = CreateMessage(options, nSent); if ((options.Duration > 0) && (options.DurationMode == "before-send")) { Utils.Sleep4Next(this.ts, options.MsgCount, (options.Duration), nSent + 1); } sender.Send(message, options.Timeout); if ((options.Duration > 0) && (options.DurationMode == "after-send-before-tx-action")) { Utils.Sleep4Next(this.ts, options.MsgCount, (options.Duration), nSent + 1); } Formatter.LogMessage(message, options); nSent++; } if (options.TxAction.ToLower() == "commit") { txs.Complete(); } if ((options.Duration > 0) && (options.DurationMode == "after-send-after-tx-action")) { Utils.Sleep4Next(ts, options.MsgCount, (options.Duration), nSent); } } //set up tx_batch_flag if ((options.MsgCount - nSent) < options.TxSize) { txFlag = false; } } //rest of messages using (var txs = new TransactionScope(TransactionScopeOption.RequiresNew)) { while (nSent < options.MsgCount) { message = CreateMessage(options, nSent); sender.Send(message, options.Timeout); Formatter.LogMessage(message, options); nSent++; } if (options.TxLoopendAction.ToLower() == "commit") { txs.Complete(); } } }