private void BrowseMessagesWorker(BlockingCollection <WsMessage> output, IBrowseFilter filter, CancellationToken ct, MQQueue queue, byte[] startingPointMessageId) { var browseOption = MQC.MQGMO_BROWSE_FIRST; if (startingPointMessageId != null) { if (SetBrowseCursorAtMessageId(queue, startingPointMessageId)) { browseOption = MQC.MQGMO_BROWSE_NEXT; } } var getMsgOpts = new MQGetMessageOptions() { Options = MQC.MQGMO_FAIL_IF_QUIESCING | browseOption }; try { while (!ct.IsCancellationRequested) { var msg = new MQMessage(); queue.Get(msg, getMsgOpts); var localMsg = new WsMessage(msg, this); if (filter == null || filter.IsMatch(localMsg)) { output.Add(localMsg, ct); } else { output.Add(null, ct); } getMsgOpts.Options = MQC.MQGMO_FAIL_IF_QUIESCING | MQC.MQGMO_BROWSE_NEXT; } } catch (OperationCanceledException) { } catch (MQException ex) { if (ex.ReasonCode != MQC.MQRC_NO_MSG_AVAILABLE) { throw; } } }
public IEnumerable <IMessage> BrowseMessages(int numberOfMessages, CancellationToken ct, byte[] startingPointMessageId = null, IBrowseFilter filter = null, IProgress <int> progress = null) { MQQueue ibmQueue; var browseOption = MQC.MQGMO_BROWSE_FIRST; try { ibmQueue = OpenQueueCore(OpenQueueMode.ForBrowse); if (startingPointMessageId != null) { if (SetBrowseCursorAtMessageId(ibmQueue, startingPointMessageId)) { browseOption = MQC.MQGMO_BROWSE_NEXT; } } } catch (MQException ibmEx) { throw ibmEx.ToMqException(AddExtraInfoToError); } int count = 0; var getMsgOpts = new MQGetMessageOptions() { Options = MQC.MQGMO_FAIL_IF_QUIESCING | browseOption }; try { while (!ct.IsCancellationRequested) { var msg = new MQMessage(); try { ibmQueue.Get(msg, getMsgOpts); } catch (MQException ibmEx) { if (ibmEx.ReasonCode == MQC.MQRC_NO_MSG_AVAILABLE) { break; } throw ibmEx.ToMqException(AddExtraInfoToError); } count++; var localMsg = new WsMessage(msg, this); if (filter == null || filter.IsMatch(localMsg)) { progress?.Report(count); localMsg.Index = count; yield return(localMsg); } numberOfMessages--; if (numberOfMessages == 0) { break; } getMsgOpts.Options = MQC.MQGMO_FAIL_IF_QUIESCING | MQC.MQGMO_BROWSE_NEXT; } } finally { ibmQueue?.Close(); } }