public void ProcessFiles(CancellationToken token) { using (MessageQueue serverQueue = new MessageQueue(Queues.Server)) { serverQueue.Formatter = new XmlMessageFormatter(new[] { typeof(DataChunk) }); List <DataChunk> chunks = new List <DataChunk>(); do { MessageEnumerator enumerator = serverQueue.GetMessageEnumerator2(); int count = 0; while (enumerator.MoveNext()) { object body = enumerator.Current?.Body; DataChunk section = body as DataChunk; if (section != null) { DataChunk chunk = section; chunks.Add(chunk); if (chunk.Position == chunk.Size) { SaveFile(chunks); chunks.Clear(); } } count++; } for (var i = 0; i < count; i++) { serverQueue.Receive(); } Thread.Sleep(1000); }while (!token.IsCancellationRequested); } }
/// <summary> /// 해당 큐에 있는 메시지 갯수, 에러 발생시 -1 /// </summary> public int queueMsgCnt(string queuePath) { int cnt = 0; try { MessageQueue myQ = myQ = new MessageQueue(queuePath); MessageEnumerator msgEnu = myQ.GetMessageEnumerator2(); while (msgEnu.MoveNext()) { cnt++; } } catch (Exception ex) { libMyUtil.clsFile.writeLog("ERR GET QUEUE MSG COUNT : " + ex.ToString()); cnt = -1; } return(cnt); }
public List <string> GetMessage() { try { MessageQueue myQueue = new MessageQueue(Path); myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) }); MessageEnumerator me = myQueue.GetMessageEnumerator2(); string msg = string.Empty; List <string> msgs = new List <string>(); while (me.MoveNext()) { msg = me.RemoveCurrent().Body.ToString(); msgs.Add(msg); } return(msgs); } catch (Exception ex) { return(null); } }
public void The_enumerator_should_skip_over_messages_that_are_being_processed() { TimeSpan timeout = 10.Seconds(); using (MessageEnumerator enumerator = _queue.GetMessageEnumerator2()) { while (enumerator.MoveNext(timeout)) { using (TransactionScope scope = new TransactionScope()) { Message received = enumerator.RemoveCurrent(timeout, MessageQueueTransactionType.Automatic); Assert.IsNotNull(received); Assert.AreEqual(received.Label, _firstMsg.Label); using (MessageEnumerator enumerator2 = _queue.GetMessageEnumerator2()) { while (enumerator2.MoveNext(timeout)) { using (TransactionScope scope2 = new TransactionScope(TransactionScopeOption.RequiresNew)) { Message received2 = enumerator2.RemoveCurrent(timeout, MessageQueueTransactionType.Automatic); Assert.IsNotNull(received2); Assert.AreEqual(received2.Label, _secondMsg.Label); scope2.Complete(); } } } scope.Complete(); } } } }
//判断队列长度是否到达一定数量 public static bool IsOverSelectLen(string strPath, int iLength) { try { MessageQueue mq = new MessageQueue(strPath, false); int iNum = 0; MessageEnumerator msgEnum = mq.GetMessageEnumerator2(); mq.Close(); while (msgEnum.MoveNext()) { iNum++; if (iNum >= iLength) { return(true); } } return(false); } catch (System.Exception e) { return(false); } }
public Dictionary <string, int> MessageTypes() { var dict = new Dictionary <string, int>(); using (var queue = new MessageQueue(_address.InboundFormatName, QueueAccessMode.Receive)) { using (MessageEnumerator enumerator = queue.GetMessageEnumerator2()) { while (enumerator.MoveNext(TimeSpan.Zero)) { Message q = enumerator.Current; if (!dict.ContainsKey(q.Label)) { dict.Add(q.Label, 0); } dict[q.Label]++; } } } return(dict); }
public void Execute(IJobExecutionContext context) { try { var path = $"{jsonFolder}\\statis.json"; // 统计数据json读成对象,计算后再json写回源文件 List <UserBehaviorStatis> statis = JsonConvert.DeserializeObject <List <UserBehaviorStatis> >(File.ReadAllText(path, Encoding.UTF8)); // Get a cursor into the messages in the queue. MessageEnumerator myEnumerator = mq.GetMessageEnumerator2(); // Specify that the messages's priority should be read. mq.MessageReadPropertyFilter.Priority = true; while (myEnumerator.MoveNext()) { Message msg = myEnumerator.Current; msg.Formatter = formatter; string str = msg.Body.ToString(); File.AppendAllText($"{jsonFolder}\\mq.txt", str + "\r\n", Encoding.UTF8); mqType type = (mqType)Enum.Parse(typeof(mqType), str.Substring(0, 1)); string body = str.Substring(2); statis.Find(s => s.Type == type).Count++; myEnumerator.RemoveCurrent(); myEnumerator.Reset(); } File.WriteAllText(path, JsonConvert.SerializeObject(statis)); } catch (Exception ex) { File.AppendAllText($"{jsonFolder}\\log.txt", ex.Message + "\r\n", Encoding.UTF8); } finally { File.AppendAllText($"{jsonFolder}\\log.txt", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "执行完毕\r\n", Encoding.UTF8); } }
public GxQueueMessage GetNext() { GxQueueMessage gxm = new GxQueueMessage(); bool messageExist = false; bool validMessage = false; while (!validMessage) { try { messageExist = mEnum.MoveNext(); if (messageExist) { Message m = (Message)(mEnum.Current); gxm.SetBaseMessage(m); validMessage = testMessage(gxm); if (validMessage && !browse) { mEnum.RemoveCurrent(); } } else { validMessage = true; } } catch (MessageQueueException mex) { errCode = 1; errDescription = mex.Message; messageExist = false; validMessage = true; } } mEof = !messageExist; return(gxm); }
//[Test] // Not supported with AMQP public void RemoveMessageWithTx() { MessageQueue q = MQUtil.GetQueue(qName); q.Formatter = new BinaryMessageFormatter(); q.Send("foo1"); q.Send("foo2"); MessageEnumerator me1 = q.GetMessageEnumerator(); MessageQueueTransaction tx = new MessageQueueTransaction(); me1.MoveNext(); Message m1 = me1.Current; me1.RemoveCurrent(tx); tx.Commit(); me1.Close(); MessageEnumerator me2 = q.GetMessageEnumerator(); Assert.IsTrue(me1.MoveNext()); me2.RemoveCurrent(); Assert.IsFalse(me2.MoveNext()); }
private void PeekMessages() { try { using (MessageEnumerator messages = orderQueue.GetMessageEnumerator2()) { while (messages.MoveNext(TimeSpan.FromHours(3))) { Message message = messages.Current; MessageInfo item = new MessageInfo() { ID = message.Id, Label = message.Label }; orderList.Add(item); } MessageBox.Show("在过去的3小时内没有订单。退出线程", "提示", MessageBoxButton.OK, MessageBoxImage.Information); } } catch (Exception ex) { MessageBox.Show(ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error); } }
protected bool EnumerateQueue(Func <IReceiveContext, Action <IReceiveContext> > receiver, TimeSpan timeout) { if (_disposed) { throw new ObjectDisposedException("The transport has been disposed: '{0}'".FormatWith(Address)); } bool received = false; _connectionHandler.Use(connection => { using (MessageEnumerator enumerator = connection.Queue.GetMessageEnumerator2()) { // if (_log.IsDebugEnabled) // _log.DebugFormat("Enumerating endpoint: {0} ({1}ms)", Address, timeout); while (enumerator.MoveNext(timeout)) { if (enumerator.Current == null) { if (_log.IsDebugEnabled) { _log.DebugFormat("Current message was null while enumerating endpoint"); } continue; } Message peekMessage = enumerator.Current; using (peekMessage) { IReceiveContext context = ReceiveContext.FromBodyStream(peekMessage.BodyStream); context.SetMessageId(peekMessage.Id); context.SetInputAddress(_address); byte[] extension = peekMessage.Extension; if (extension.Length > 0) { TransportMessageHeaders headers = TransportMessageHeaders.Create(extension); context.SetContentType(headers["Content-Type"]); } Action <IReceiveContext> receive = receiver(context); if (receive == null) { if (_log.IsDebugEnabled) { _log.DebugFormat("SKIP:{0}:{1}", Address, peekMessage.Id); } continue; } ReceiveMessage(enumerator, timeout, message => { if (message == null) { throw new TransportException(Address.Uri, "Unable to remove message from queue: " + context.MessageId); } if (message.Id != context.MessageId) { throw new TransportException(Address.Uri, string.Format( "Received message does not match current message: ({0} != {1})", message.Id, context.MessageId)); } if (_messageLog.IsDebugEnabled) { _messageLog.DebugFormat("RECV:{0}:{1}:{2}", _address.InboundFormatName, message.Label, message.Id); } receive(context); received = true; }); } } } }); return(received); }
protected virtual bool EnumerateQueue(Func <Message, Action <Message> > receiver, TimeSpan timeout) { if (_disposed) { throw NewDisposedException(); } bool received = false; using (MessageEnumerator enumerator = _queue.GetMessageEnumerator2()) { if (_log.IsDebugEnabled) { _log.DebugFormat("Enumerating endpoint: {0} ({1}ms)", Address, timeout); } // ADK - Add MessageReadPorpertyFilter to get Extension _queue.MessageReadPropertyFilter.Extension = true; while (enumerator.MoveNext(timeout)) { Message current = enumerator.Current; if (current == null) { if (_log.IsDebugEnabled) { _log.DebugFormat("Current message was null while enumerating endpoint"); } continue; } // ADK - Get tag here DynaTraceADKFactory.initialize(); var adk = DynaTraceADKFactory.createTagging(); // Get the tag from current.Extension byte[] data = current.Extension; const int magicInt = 395394190; const int tracetagSize = 1 /* Version */ + 1 /* Size */ + 4 /* ServerId */ + 4 * 6; const int traceTagLength = tracetagSize + 4; if (data.Length >= traceTagLength) { var offset = data.Length - traceTagLength; var tagOffset = offset + tracetagSize; var magic = data[tagOffset] << 24 | (data[tagOffset + 1] << 16) | (data[tagOffset + 2] << 8) | (data[tagOffset + 3]); if (magic == magicInt) { byte[] original; if (offset == 0) { // we do not set the extension to new byte[0] to prevent an aliasing bug in System.Messaging original = new byte[1]; } else { original = new byte[offset]; Array.Copy(data, original, offset); } current.Extension = original; byte[] tag = new byte[traceTagLength]; Array.Copy(data, offset, tag, 0, traceTagLength); adk.setTag(tag); adk.startServerPurePath(); } } Action <Message> receive = receiver(current); if (receive == null) { if (_log.IsDebugEnabled) { _log.DebugFormat("SKIP:{0}:{1}", Address, current.Id); } if (SpecialLoggers.Messages.IsInfoEnabled) { SpecialLoggers.Messages.InfoFormat("SKIP:{0}:{1}", Address, current.Id); } continue; } ReceiveMessage(enumerator, timeout, receiveCurrent => { using (Message message = receiveCurrent()) { if (message == null) { throw new TransportException(Address.Uri, "Unable to remove message from queue: " + current.Id); } if (message.Id != current.Id) { throw new TransportException(Address.Uri, string.Format("Received message does not match current message: ({0} != {1})", message.Id, current.Id)); } receive(message); received = true; } }); } } return(received); }
protected virtual bool EnumerateQueue(Func <Message, Action <Message> > receiver, TimeSpan timeout) { if (_disposed) { throw NewDisposedException(); } bool received = false; using (MessageEnumerator enumerator = _queue.GetMessageEnumerator2()) { if (_log.IsDebugEnabled) { _log.DebugFormat("Enumerating endpoint: {0} ({1}ms)", Address, timeout); } while (enumerator.MoveNext(timeout)) { Message current = enumerator.Current; if (current == null) { if (_log.IsDebugEnabled) { _log.DebugFormat("Current message was null while enumerating endpoint"); } continue; } Action <Message> receive = receiver(current); if (receive == null) { if (_log.IsDebugEnabled) { _log.DebugFormat("SKIP:{0}:{1}", Address, current.Id); } if (SpecialLoggers.Messages.IsInfoEnabled) { SpecialLoggers.Messages.InfoFormat("SKIP:{0}:{1}", Address, current.Id); } continue; } ReceiveMessage(enumerator, timeout, receiveCurrent => { using (Message message = receiveCurrent()) { if (message == null) { throw new TransportException(Address.Uri, "Unable to remove message from queue: " + current.Id); } if (message.Id != current.Id) { throw new TransportException(Address.Uri, string.Format("Received message does not match current message: ({0} != {1})", message.Id, current.Id)); } receive(message); received = true; } }); } } return(received); }
public override IEnumerable <QueueItem> GetProcessedMessages(QueueType type, DateTime since, IEnumerable <QueueItem> currentItems) { List <QueueItem> r = new List <QueueItem>(); var queues = GetQueueListByType(type); if (queues.Count() == 0) { return(EMPTY_LIST); } foreach (var q in queues) { string qName = q.GetDisplayName(); if (IsIgnoredQueue(qName) || !q.CanReadJournalQueue) { continue; } SetupMessageReadPropertyFilters(q.Journal, type); try { List <Message> messages = new List <Message>(); // Enumete from the earliest item MessageEnumerator msgs = q.Journal.GetMessageEnumerator2(); try { while (msgs.MoveNext()) { Message msg = msgs.Current; if (msg.ArrivedTime >= since) { messages.Add(msg); } } } finally { msgs.Close(); } foreach (var msg in messages) { QueueItem itm = currentItems.FirstOrDefault(i => i.Id == msg.Id); if (itm == null) { itm = CreateQueueItem(q.Queue, msg); itm.Processed = true; if (!PrepareQueueItemForAdd(itm)) { itm = null; } } if (itm != null) { r.Insert(0, itm); } } } catch (Exception e) { OnError("Error occured when getting processed messages from queue \"" + qName + "\", " + e.Message, e, false); } } return(r); }
protected void EnumerateQueue(Func <IReceiveContext, Action <IReceiveContext> > receiver, TimeSpan timeout) { if (_disposed) { throw new ObjectDisposedException("The transport has been disposed: '{0}'".FormatWith(Address)); } _connectionHandler.Use(connection => { try { using (MessageEnumerator enumerator = connection.Queue.GetMessageEnumerator2()) { while (enumerator.MoveNext(timeout)) { if (enumerator.Current == null) { if (_log.IsDebugEnabled) { _log.DebugFormat("Current message was null while enumerating endpoint"); } continue; } Message peekMessage = enumerator.Current; using (peekMessage) { IReceiveContext context = ReceiveContext.FromBodyStream(peekMessage.BodyStream, _address.IsTransactional); context.SetMessageId(peekMessage.Id); context.SetInputAddress(_address); byte[] extension = peekMessage.Extension; if (extension.Length > 0) { TransportMessageHeaders headers = TransportMessageHeaders.Create(extension); context.SetContentType(headers["Content-Type"]); context.SetOriginalMessageId(headers["Original-Message-Id"]); } Action <IReceiveContext> receive = receiver(context); if (receive == null) { continue; } ReceiveMessage(enumerator, timeout, message => { if (message == null) { throw new TransportException(Address.Uri, "Unable to remove message from queue: " + context.MessageId); } if (message.Id != context.MessageId) { throw new TransportException(Address.Uri, string.Format( "Received message does not match current message: ({0} != {1})", message.Id, context.MessageId)); } receive(context); }); } } } } catch (MessageQueueException ex) { HandleInboundMessageQueueException(ex); } }); }
public static void Test(ITransport t, IQueueStrategy strategy, Endpoint queueEndpoint, Action <Message> send, Func <MessageEnumerator> enumer) { Guid id = Guid.NewGuid(); var serializer = new XmlMessageSerializer(new DefaultReflection(), new DefaultKernel()); var subscriptionStorage = new MsmqSubscriptionStorage(new DefaultReflection(), serializer, queueEndpoint.Uri, new EndpointRouter(), strategy); subscriptionStorage.Initialize(); var wait = new ManualResetEvent(false); subscriptionStorage.SubscriptionChanged += () => wait.Set(); t.AdministrativeMessageArrived += subscriptionStorage.HandleAdministrativeMessage; Message msg = new MsmqMessageBuilder (serializer, new DefaultKernel()).BuildFromMessageBatch(new AddInstanceSubscription { Endpoint = queueEndpoint.Uri.ToString(), InstanceSubscriptionKey = id, Type = typeof(TestMessage2).FullName, }); send(msg); wait.WaitOne(); msg = new MsmqMessageBuilder (serializer, new DefaultKernel()).BuildFromMessageBatch(new RemoveInstanceSubscription { Endpoint = queueEndpoint.Uri.ToString(), InstanceSubscriptionKey = id, Type = typeof(TestMessage2).FullName, }); wait.Reset(); send(msg); wait.WaitOne(); IEnumerable <Uri> uris = subscriptionStorage .GetSubscriptionsFor(typeof(TestMessage2)); Assert.Equal(0, uris.Count()); int count = 0; MessageEnumerator copy = enumer(); while (copy.MoveNext()) { count++; } Assert.Equal(0, count); }
public void Notify() { // TODO: 要增加判断,防止过分频繁地被调用 this.MessageConnection.CleanWebDataTable(); if (this.dp2library.DefaultQueue == "!api") { try { if (this.MessageConnection.ConnectState != Microsoft.AspNet.SignalR.Client.ConnectionState.Connected) { return; } MessageData[] messages = null; string strError = ""; int nRet = this.MessageConnection.GetMsmqMessage( out messages, out strError); if (nRet == -1) { this.WriteErrorLog("Instance.Notify() 中 GetMsmqMessage() 出错: " + strError); return; } if (messages != null) { foreach (MessageData data in messages) { MessageRecord record = new MessageRecord(); record.groups = new string[1] { "gn:_patronNotify" }; // gn 表示 group name record.data = (string)data.strBody; record.format = "xml"; List <MessageRecord> records = new List <MessageRecord> { record }; DigitalPlatform.Message.SetMessageRequest param = new DigitalPlatform.Message.SetMessageRequest("create", "dontNotifyMe", records); SetMessageResult result = this.MessageConnection.SetMessageTaskAsync(param, _cancel.Token).Result; if (result.Value == -1) { this.WriteErrorLog("Instance.Notify() 中 SetMessageAsync() 出错: " + result.ErrorInfo); return; } } nRet = this.MessageConnection.RemoveMsmqMessage( messages.Length, out strError); if (nRet == -1) { this.WriteErrorLog("Instance.Notify() 中 RemoveMsmqMessage() 出错: " + strError); return; } } this._notifyThread.Activate(); return; } catch (ThreadAbortException) { return; } catch (Exception ex) { this.WriteErrorLog("Instance.Notify() 出现异常1: " + ExceptionUtil.GetDebugText(ex)); } } // 如果第一次初始化 Queue 没有成功,这里再试探初始化 InitialQueue(false); // 进行通知处理 if (_queue != null && this.MessageConnection.ConnectState == Microsoft.AspNet.SignalR.Client.ConnectionState.Connected) { try { ServerInfo._recordLocks.LockForWrite(this._queue.Path); } catch (ApplicationException) { // 超时了 return; } bool bSucceed = false; try { MessageEnumerator iterator = _queue.GetMessageEnumerator2(); while (iterator.MoveNext()) { Message message = iterator.Current; MessageRecord record = new MessageRecord(); record.groups = new string[1] { "gn:_patronNotify" }; // gn 表示 group name record.data = (string)message.Body; record.format = "xml"; List <MessageRecord> records = new List <MessageRecord> { record }; int length = record.data.Length; DigitalPlatform.Message.SetMessageRequest param = new DigitalPlatform.Message.SetMessageRequest("create", "dontNotifyMe", records); SetMessageResult result = this.MessageConnection.SetMessageTaskAsync(param, _cancel.Token).Result; if (result.Value == -1) { this.WriteErrorLog("Instance.Notify() 中 SetMessageAsync() 出错: " + result.ErrorInfo); if (result.String == "_connectionNotFound") { Task.Run(() => TryResetConnection(/*result.String*/)); } return; } // http://stackoverflow.com/questions/21864043/with-messageenumerator-removecurrent-how-do-i-know-if-i-am-at-end-of-queue try { iterator.RemoveCurrent(); } finally { iterator.Reset(); } } bSucceed = true; } catch (ThreadAbortException) { return; } catch (MessageQueueException ex) { // 记入错误日志 // Program.WriteWindowsLog("Instance.Notify() 出现异常: " + ExceptionUtil.GetDebugText(ex)); this.WriteErrorLog("Instance.Notify() 出现异常: " + ExceptionUtil.GetDebugText(ex)); // Thread.Sleep(5 * 1000); // 拖延 5 秒 } catch (InvalidCastException ex) { // 记入错误日志 // Program.WriteWindowsLog("Instance.Notify() 出现异常: " + ExceptionUtil.GetDebugText(ex)); this.WriteErrorLog("Instance.Notify() 出现异常: " + ExceptionUtil.GetDebugText(ex)); // Thread.Sleep(5 * 1000); // 拖延 5 秒 } catch (Exception ex) { // 记入错误日志 // Program.WriteWindowsLog("Instance.Notify() 出现异常: " + ExceptionUtil.GetDebugText(ex)); this.WriteErrorLog("Instance.Notify() 出现异常: " + ExceptionUtil.GetDebugText(ex)); // Thread.Sleep(5 * 1000); // 拖延 5 秒 } finally { ServerInfo._recordLocks.UnlockForWrite(this._queue.Path); // 只有当发送到 dp2mserver 成功的情况下才立即重新监控最新 MQ // 否则就等下一轮 Worker() 来处理 if (bSucceed == true) { // _queue.BeginPeek(new TimeSpan(0, 1, 0), null, OnMessageAdded); BeginPeek(); } } } }