public static void RunTest() { Console.WriteLine("Start"); myMq.Connect(); Console.WriteLine("Connected"); myMq.SubscribeConsumer("xx", true, null); string mes = Console.ReadLine(); myMq.PublishMessage("q1", mes, false, MessageType.TextMessage); myMq.PublishMessage("q2", mes, false, MessageType.TextMessage); myMq.PublishMessage("t1", mes, true, MessageType.TextMessage); myMq.PublishMessage("t2", mes, true, MessageType.TextMessage); mes = Console.ReadLine(); while (mes != "e") { List <KeyValuePair <string, string> > tempMes = myMq.ReadConsumerMessage(); foreach (var ms in tempMes) { Console.WriteLine(ms.Value + " " + ms.Key); } mes = Console.ReadLine(); } }
public MyExecutionDeviceResult ExecutionDeviceRun(ICaseExecutionContent yourExecutionContent, CaseActionActuator.delegateGetExecutiveData yourExecutiveDelegate, string sender, ActuatorStaticDataCollection yourActuatorStaticDataCollection, int caseId) { List <string> errorList = new List <string>(); string tempError = null; MyExecutionDeviceResult myResult = new MyExecutionDeviceResult(); myResult.staticDataResultCollection = new System.Collections.Specialized.NameValueCollection(); Action <string, CaseActuatorOutPutType, string> ExecutiveDelegate = (innerSender, outType, yourContent) => { if (yourExecutiveDelegate != null) { yourExecutiveDelegate(innerSender, outType, yourContent); } }; Action <string> DealExecutiveError = (errerData) => { if (errerData != null) { ExecutiveDelegate(sender, CaseActuatorOutPutType.ExecutiveError, errerData); errorList.Add(errerData); } }; if (yourExecutionContent.MyCaseProtocol == CaseProtocol.activeMQ) { //在调用该函数前保证nowExecutionContent.ErrorMessage为空,且as一定成功 MyActiveMQExecutionContent nowExecutionContent = yourExecutionContent as MyActiveMQExecutionContent; myResult.caseProtocol = CaseProtocol.activeMQ; myResult.caseTarget = nowExecutionContent.MyExecutionTarget; myResult.startTime = DateTime.Now.ToString("HH:mm:ss"); StringBuilder tempCaseOutContent = new StringBuilder(); System.Diagnostics.Stopwatch myWatch = new System.Diagnostics.Stopwatch(); myWatch.Start(); ExecutiveDelegate(sender, CaseActuatorOutPutType.ExecutiveInfo, string.Format("【ID:{0}】[activeMQ]Executive···", caseId)); #region Subscribe foreach (var tempConsumer in nowExecutionContent.consumerSubscribeList) { if (tempConsumer.ConsumerType == "queue" || tempConsumer.ConsumerType == "topic") { if (activeMQ.SubscribeConsumer(tempConsumer.ConsumerName, tempConsumer.ConsumerType == "queue", tempConsumer.ConsumerTopicDurable)) { ExecutiveDelegate(sender, CaseActuatorOutPutType.ExecutiveInfo, string.Format("{0}://{1} subscribe success", tempConsumer.ConsumerType, tempConsumer.ConsumerName)); } else { DealExecutiveError(string.Format("{0}://{1} subscribe fail", tempConsumer.ConsumerType, tempConsumer.ConsumerName)); } } else { DealExecutiveError(string.Format("{0}://{1} subscribe fail [not support this consumer type]", tempConsumer.ConsumerType, tempConsumer.ConsumerName)); } } #endregion #region UnSubscribe foreach (var tempConsumer in nowExecutionContent.unConsumerSubscribeList) { if (tempConsumer.ConsumerType == "queue" || tempConsumer.ConsumerType == "topic") { if (activeMQ.UnSubscribeConsumer(string.Format("{0}://{1}", tempConsumer.ConsumerType, tempConsumer.ConsumerName)) > 0) { ExecutiveDelegate(sender, CaseActuatorOutPutType.ExecutiveInfo, string.Format("{0}://{1} unsubscribe success", tempConsumer.ConsumerType, tempConsumer.ConsumerName)); } else { DealExecutiveError(string.Format("{0}://{1} unsubscribe fail", tempConsumer.ConsumerType, tempConsumer.ConsumerName)); } } else { DealExecutiveError(string.Format("{0}://{1} unsubscribe fail [not support this consumer type]", tempConsumer.ConsumerType, tempConsumer.ConsumerName)); } } #endregion #region Send foreach (var tempOneSender in nowExecutionContent.producerDataSendList) { if (tempOneSender.Key.ProducerType == "queue" || tempOneSender.Key.ProducerType == "topic") { string tempMessageSend = tempOneSender.Value.GetTargetContentData(yourActuatorStaticDataCollection, myResult.staticDataResultCollection, out tempError); if (tempError != null) { DealExecutiveError(string.Format("this case get static data errer with [{0}]", tempOneSender.Value.GetTargetContentData())); tempCaseOutContent.AppendLine(string.Format("{0}://{1} send message fail [get static data errer]", tempOneSender.Key.ProducerType, tempOneSender.Key.ProducerName)); } else { MessageType tempMessageType; if (Enum.TryParse <MessageType>(tempOneSender.Key.MessageType, out tempMessageType)) { if (activeMQ.PublishMessage(tempOneSender.Key.ProducerName, tempMessageSend, tempOneSender.Key.ProducerType == "topic", tempMessageType)) { string tempReportStr = string.Format("{0}://{1} send message success", tempOneSender.Key.ProducerType, tempOneSender.Key.ProducerName); ExecutiveDelegate(sender, CaseActuatorOutPutType.ExecutiveInfo, tempReportStr); tempCaseOutContent.AppendLine(tempReportStr); } else { string tempReportStr = string.Format("{0}://{1} send message fail [{2}]", tempOneSender.Key.ProducerType, tempOneSender.Key.ProducerName, activeMQ.NowErrorMes); ExecutiveDelegate(sender, CaseActuatorOutPutType.ExecutiveInfo, tempReportStr); tempCaseOutContent.AppendLine(tempReportStr); } } else { DealExecutiveError(string.Format("get MessageType errer with [{0}]", tempOneSender.Key.MessageType)); tempCaseOutContent.AppendLine(string.Format("{0}://{1} send message fail [get MessageType errer]", tempOneSender.Key.ProducerType, tempOneSender.Key.ProducerName)); } } } else { tempCaseOutContent.Append(string.Format("{0}://{1} send message fail [not support this producer type]", tempOneSender.Key.ProducerType, tempOneSender.Key.ProducerName)); } } #endregion #region Receive if (nowExecutionContent.consumerMessageReceiveList.Count > 0) { ExecutiveDelegate(sender, CaseActuatorOutPutType.ExecutiveInfo, "Receive your activeMQ messages"); } foreach (var tempConsumer in nowExecutionContent.consumerMessageReceiveList) { if (tempConsumer.ConsumerName != null) { if (tempConsumer.ConsumerType == "queue" || tempConsumer.ConsumerType == "topic") { List <KeyValuePair <string, string> > oneMessageReceive = activeMQ.ReadConsumerMessage(string.Format("{0}://{1}", tempConsumer.ConsumerType, tempConsumer.ConsumerName)); foreach (KeyValuePair <string, string> tempMessage in oneMessageReceive) { string tempNowReceviceData = string.Format("{0} Receive {1}", tempMessage.Key, tempMessage.Value); ExecutiveDelegate(sender, CaseActuatorOutPutType.ExecutiveInfo, tempNowReceviceData); tempCaseOutContent.AppendLine(tempNowReceviceData); } } else { DealExecutiveError(string.Format("{0}://{1} receive fail [not support this consumer type]", tempConsumer.ConsumerType, tempConsumer.ConsumerName)); tempCaseOutContent.AppendLine(string.Format("{0}://{1} receive fail ", tempConsumer.ConsumerType, tempConsumer.ConsumerName)); } } else { List <KeyValuePair <string, string> > oneMessageReceive = activeMQ.ReadConsumerMessage(); foreach (KeyValuePair <string, string> tempMessage in oneMessageReceive) { string tempNowReceviceData = string.Format("{0} Receive {1}", tempMessage.Key, tempMessage.Value); ExecutiveDelegate(sender, CaseActuatorOutPutType.ExecutiveInfo, tempNowReceviceData); tempCaseOutContent.AppendLine(tempNowReceviceData); } } } #endregion myWatch.Stop(); myResult.spanTime = myResult.requestTime = myWatch.ElapsedMilliseconds.ToString(); myResult.backContent = tempCaseOutContent.ToString(); } else { myResult.backContent = "error:your CaseProtocol is not Matching RunTimeActuator"; DealExecutiveError(myResult.backContent); } if (errorList.Count > 0) { myResult.additionalError = errorList.MyToString("\r\n"); } return(myResult); }