예제 #1
0
        private void buttonPollForRequest_Click(object sender, EventArgs e)
        {
            string       remoteq = @"FormatName:direct=OS:provsvc\private$\Provision";
            MessageQueue rq      = new MessageQueue(remoteq);

            System.Messaging.Cursor cursor = rq.CreateCursor();
            TimeSpan timeout = new TimeSpan(0, 0, 10);

            rq.Formatter = new XmlMessageFormatter(new[] { typeof(ProvisionTask) });

            System.Messaging.Message m;
            PeekAction action = PeekAction.Current;

            m = GetPeek(rq, cursor, PeekAction.Current);

            while (m != null)
            {
                ProvisionTask r = (ProvisionTask)m.Body;
                if (r.target.ToLower() == Environment.MachineName.ToLower())
                {
                    m = GetMessage(rq, m.Id);
                }
                m = GetPeek(rq, cursor, PeekAction.Next);
            }



            cursor.Close();
        }
예제 #2
0
 private System.Messaging.Message GetPeek(MessageQueue q, System.Messaging.Cursor c, PeekAction action)
 {
     System.Messaging.Message ret = null;
     try
     {
         ret = q.Peek(new TimeSpan(1), c, action);
     }
     catch (Exception ex)
     {
         Debug.WriteLine(ex.Message);
     }
     return(ret);
 }
예제 #3
0
 private static Message PeekWithoutTimeout(MessageQueue q, Cursor cursor, PeekAction action)
 {
     Message ret = null;
     try
     {
         ret = q.Peek(new TimeSpan(1), cursor, action);
     }
     catch (MessageQueueException mqe)
     {
         if (!mqe.Message.ToLower().Contains("timeout"))
         {
             throw;
         }
     }
     return ret;
 }
예제 #4
0
        protected Message PeekWithoutTimeout(MessageQueue q, System.Messaging.Cursor cursor, PeekAction action)
        {
            Message ret = null;

            try
            {
                ret = q.Peek(new TimeSpan(1), cursor, action);
            }
            catch (MessageQueueException mqe)
            {
                if (!mqe.Message.ToLower().Contains("timeout"))
                {
                    throw;
                }
            }
            return(ret);
        }
예제 #5
0
        /// <summary>
        /// Reads a message from target queue as a String
        /// </summary>
        /// <param name="mq">Message Queue Name</param>
        public void ReadMessageAsString(System.Messaging.MessageQueue mq)
        {
            System.Messaging.Cursor  cursor = mq.CreateCursor();
            System.Messaging.Message m      = PeekWithoutTimeout(mq, cursor, PeekAction.Current);

            m.Formatter = new XmlMessageFormatter(new String[] { "System.String, mscorlib", });
            string body = (string)m.Body;

            Console.Write(body);
            Console.Write(Environment.NewLine);

            {
                while ((m = PeekWithoutTimeout(mq, cursor, PeekAction.Next)) != null)
                {
                    // Construct an XMLMessageFormatter
                    m.Formatter = new XmlMessageFormatter(new String[] { "System.String, mscorlib", });
                    body        = (string)m.Body;
                    Console.Write(body);
                    Console.Write(Environment.NewLine);
                }
            }
        }
예제 #6
0
        private System.Messaging.Message GetProvisionTaskMessage(string queuename, Guid taskid)
        {
            MessageQueue rq = new MessageQueue(String.Format(@"FormatName:direct=OS:provsvc\private$\{0}", queuename));

            rq.Formatter = rq.Formatter = new XmlMessageFormatter(new[] { typeof(ProvisionTask) });
            System.Messaging.Cursor cursor = rq.CreateCursor();
            TimeSpan timeout = new TimeSpan(0, 0, 10);


            System.Messaging.Message m = GetPeek(rq, cursor, PeekAction.Current);
            while (m != null)
            {
                ProvisionTask task = (ProvisionTask)m.Body;
                if (task.taskid == taskid)
                {
                    m = GetMessageByID("Provision", m.Id);
                    rq.Close();
                    return(m);
                }
            }
            return(null);
        }
예제 #7
0
 /// <summary>
 /// Returns without removing (peeks) the current or next message in the queue, using the specified cursor. The <see cref="M:System.Messaging.MessageQueue.Peek"/> method is synchronous, so it blocks the current thread until a message becomes available or the specified time-out occurs.
 /// </summary>
 /// <returns>
 /// A <see cref="T:System.Messaging.Message"/> that represents a message in the queue.
 /// </returns>
 /// <param name="timeout">A <see cref="T:System.TimeSpan"/> that indicates the maximum time to wait for the queue to contain a message. </param><param name="cursor">A <see cref="T:System.Messaging.Cursor"/> that maintains a specific position in the message queue.</param><param name="action">One of the <see cref="T:System.Messaging.PeekAction"/> values. Indicates whether to peek at the current message in the queue, or the next message.</param><exception cref="T:System.ArgumentOutOfRangeException">A value other than PeekAction.Current or PeekAction.Next was specified for the <paramref name="action"/> parameter.</exception><exception cref="T:System.ArgumentNullException">The <paramref name="cursor"/> parameter is null.</exception><exception cref="T:System.ArgumentException">The value specified for the <paramref name="timeout"/> parameter is not valid. Possibly <paramref name="timeout"/> is less than <see cref="F:System.TimeSpan.Zero"/> or greater than <see cref="F:System.Messaging.MessageQueue.InfiniteTimeout"/>. </exception><exception cref="T:System.Messaging.MessageQueueException">An error occurred when accessing a Message Queuing method. </exception>
 public Message Peek(TimeSpan timeout, Cursor cursor, PeekAction action)
 {
     return _queue.Peek(timeout, cursor, action);
 }
예제 #8
0
 /// <summary>
 /// Initiates an asynchronous receive operation that has a specified time-out and uses a specified cursor and a specified state object. The state object provides associated information throughout the lifetime of the operation. This overload receives notification, through a callback, of the identity of the event handler for the operation. The operation is not complete until either a message becomes available in the queue or the time-out occurs.
 /// </summary>
 /// <returns>
 /// The <see cref="T:System.IAsyncResult"/> that identifies the posted asynchronous request.
 /// </returns>
 /// <param name="timeout">A <see cref="T:System.TimeSpan"/> that indicates the interval of time to wait for a message to become available. </param><param name="cursor">A <see cref="T:System.Messaging.Cursor"/> that maintains a specific position in the message queue.</param><param name="state">A state object, specified by the application, that contains information associated with the asynchronous operation. </param><param name="callback">The <see cref="T:System.AsyncCallback"/> that receives the notification of the asynchronous operation completion. </param><exception cref="T:System.ArgumentNullException">The <paramref name="cursor"/> parameter is null.</exception><exception cref="T:System.ArgumentException">The value specified for the <paramref name="timeout"/> parameter is not valid. </exception><exception cref="T:System.Messaging.MessageQueueException">An error occurred when accessing a Message Queuing method. </exception>
 public IAsyncResult BeginReceive(TimeSpan timeout, Cursor cursor, object state, AsyncCallback callback)
 {
     return _queue.BeginReceive(timeout, cursor, state, callback);
 }
예제 #9
0
 /// <summary>
 /// Initiates an asynchronous peek operation that has a specified time-out and that uses a specified cursor, a specified peek action, and a specified state object. The state object provides associated information throughout the lifetime of the operation. This overload receives notification, through a callback, of the identity of the event handler for the operation. The operation is not complete until either a message becomes available in the queue or the time-out occurs.
 /// </summary>
 /// <returns>
 /// The <see cref="T:System.IAsyncResult"/> that identifies the posted asynchronous request.
 /// </returns>
 /// <param name="timeout">A <see cref="T:System.TimeSpan"/> that indicates the interval of time to wait for a message to become available. </param><param name="cursor">A <see cref="T:System.Messaging.Cursor"/> that maintains a specific position in the message queue.</param><param name="action">One of the <see cref="T:System.Messaging.PeekAction"/> values. Indicates whether to peek at the current message in the queue, or the next message.</param><param name="state">A state object, specified by the application, that contains information associated with the asynchronous operation. </param><param name="callback">The <see cref="T:System.AsyncCallback"/> that receives the notification of the asynchronous operation completion. </param><exception cref="T:System.ArgumentOutOfRangeException">A value other than PeekAction.Current or PeekAction.Next was specified for the <paramref name="action"/> parameter.</exception><exception cref="T:System.ArgumentNullException">The <paramref name="cursor"/> parameter is null.</exception><exception cref="T:System.ArgumentException">The value specified for the <paramref name="timeout"/> parameter is not valid. </exception><exception cref="T:System.Messaging.MessageQueueException">An error occurred when accessing a Message Queuing method. </exception>
 public IAsyncResult BeginPeek(TimeSpan timeout, Cursor cursor, PeekAction action, object state, AsyncCallback callback)
 {
     return _queue.BeginPeek(timeout, cursor, action, state, callback);
 }
예제 #10
0
 private Message PeekMessage(MessageQueue queue, Cursor cursor, PeekAction action)
 {
     try
     {
         return queue.Peek(_timeout, cursor, action);
     }
     catch
     {
         return null;
     }
 }
예제 #11
0
        private Message PeekMessage(MessageQueue queue, Cursor cursor, PeekAction action)
        {
            ResetUnderlyingMessageData();

            try
            {
                underlyingMessageData = queue.Peek(timeout, cursor, action);

                return (Message)underlyingMessageData;
            }
            catch
            {
                return null;
            }
        }
        // Modified PeekByCorrelationId that peeks forward for correlation id
        private Message PeekByCorrelationId(string correlationId, TimeSpan timeout, Cursor cursor, PeekAction action)
        {
            string compare = string.Empty;
            if (!string.IsNullOrEmpty(correlationId))
            {
                compare = correlationId;
            }

            Message m = this.queue.Peek(timeout, cursor, action);
            while (m != null)
            {
                if (String.Compare(m.CorrelationId, 0, Utilities.GetEmptyCorrelationIdStr(), 0, m.CorrelationId.Length, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    if (string.IsNullOrEmpty(compare))
                    {
                        return m;
                    }
                }
                else
                {
                    if (String.Compare(m.CorrelationId, 0, compare, 0, m.CorrelationId.Length, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        return m;
                    }
                }

                // Continue search, peek next message
                m = this.queue.Peek(timeout, cursor, PeekAction.Next);
            }

            return null;
        }
        // This method peeks the fragments by calling the overloaded Peek methods of the System.Messaging API
        private Message PeekFromMsmq(string correlationId, TimeSpan timeout, Cursor cursor, PeekAction action)
        {
            if (correlationId == null)
            {
                this.message = this.queue.Peek(timeout, cursor, action);
            }
            else
            {
                this.message = this.PeekByCorrelationId(correlationId, timeout, cursor, action);
            }

            return this.message;
        }
예제 #14
0
 public FilteredReceiver(MSMQ.MessageQueue queue, bool isTransactional, string subject) : base(queue, isTransactional)
 {
     _subject   = subject;
     _cursor    = queue.CreateCursor();
     _stopWatch = new Stopwatch();
 }
예제 #15
0
 /// <summary>
 /// Receives the current message in the queue, using a specified cursor. If no message is available, this method waits until either a message is available, or the time-out expires.
 /// </summary>
 /// <returns>
 /// A <see cref="T:System.Messaging.Message"/> that references the first message available in the queue.
 /// </returns>
 /// <param name="timeout">A <see cref="T:System.TimeSpan"/> that indicates the time to wait until a new message is available for inspection. </param><param name="cursor">A <see cref="T:System.Messaging.Cursor"/> that maintains a specific position in the message queue.</param><exception cref="T:System.ArgumentException">The value specified for the <paramref name="timeout"/> parameter is not valid, possibly <paramref name="timeout"/> is less than <see cref="F:System.TimeSpan.Zero"/> or greater than <see cref="F:System.Messaging.MessageQueue.InfiniteTimeout"/>. </exception><exception cref="T:System.Messaging.MessageQueueException">A message did not arrive in the queue before the time-out expired.-or- An error occurred when accessing a Message Queuing method </exception>
 public Message Receive(TimeSpan timeout, Cursor cursor)
 {
     return _queue.Receive(timeout, cursor);
 }
예제 #16
0
 public static Task <MSMQ.Message> PeekAsync(this MSMQ.MessageQueue queue, TimeSpan timeout, MSMQ.Cursor cursor, MSMQ.PeekAction peekAction)
 {
     queue.MessageReadPropertyFilter = Filters.Peek; // just peek minimal details
     return(Task.Factory.FromAsync(queue.BeginPeek(timeout, cursor, peekAction, queue, null), EndPeek));
 }
예제 #17
0
        private void WaitMessages(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            object[]       par           = e.Argument as object[];
            string         _curQueueName = par[0] as string;
            bool           useCurUser    = (bool)par[1];
            int            waitPeriod    = (int)par[2];
            List <Message> messages      = new List <Message>();

            if (useCurUser)
            {
                var queue = new MessageQueue(_curQueueName)
                {
                    MessageReadPropertyFilter = new MessagePropertyFilter
                    {
                        Body = true,
                        Id   = true,
                    }
                };

                using (System.Messaging.Cursor c = queue.CreateCursor())
                {
                    System.Messaging.Message m = null;
                    try
                    {
                        m = queue.Peek(new TimeSpan(0, 0, waitPeriod), c, PeekAction.Current);
                        messages.Add(m);
                    }
                    catch (MessageQueueException mex)
                    {
                        m = null;
                    }

                    while (m != null)
                    {
                        try
                        {
                            m = queue.Peek(new TimeSpan(0, 0, 1), c, PeekAction.Next);
                            messages.Add(m);
                        }
                        catch (Exception ex)
                        {
                            m = null;
                        }
                    }
                }
            }
            else
            {
                string username = "******";
                string domain   = "avant";
                using (Impersonator imp = new Impersonator(username, domain, "Daw18!otM"))
                {
                    var queue = new MessageQueue(_curQueueName)
                    {
                        MessageReadPropertyFilter = new MessagePropertyFilter
                        {
                            Body = true,
                            Id   = true,
                        }
                    };

                    using (System.Messaging.Cursor c = queue.CreateCursor())
                    {
                        System.Messaging.Message m = null;
                        try
                        {
                            m = queue.Peek(new TimeSpan(0, 0, waitPeriod), c, PeekAction.Current);
                            messages.Add(m);
                        }
                        catch (MessageQueueException mex)
                        {
                            m = null;
                        }

                        while (m != null)
                        {
                            try
                            {
                                m = queue.Peek(new TimeSpan(0, 0, 1), c, PeekAction.Next);
                                messages.Add(m);
                            }
                            catch (Exception ex)
                            {
                                m = null;
                            }
                        }
                    }
                }
            }

            e.Result = ParseMessages(messages.ToArray());
        }
        public void StartMonitoring()
        {
            cursor = errorQueue.CreateCursor();

            errorQueue.BeginPeek(MessageQueue.InfiniteTimeout, cursor, PeekAction.Current, null, OnPeekCompleted);
        }
예제 #19
0
 /// <summary>
 /// http://jopinblog.wordpress.com/2008/03/12/counting-messages-in-an-msmq-messagequeue-from-c/
 /// </summary>
 /// <param name="cursor"></param>
 /// <param name="action"></param>
 /// <returns></returns>
 protected Message CursorPeekWithoutTimeout( Cursor cursor, PeekAction action)
 {
     Message ret = null;
     try
     {
         ret = _messageQueue.Peek(new TimeSpan(1), cursor, action);
     }
     catch (MessageQueueException mqe)
     {
         if (mqe.MessageQueueErrorCode != MessageQueueErrorCode.IOTimeout)
         {
             throw;
         }
     }
     return ret;
 }
예제 #20
0
 /// <summary>
 /// Receives the current message in the queue, using a specified cursor. If no message is available, this method waits until either a message is available, or the time-out expires.
 /// </summary>
 /// <returns>
 /// A <see cref="T:System.Messaging.Message"/> that references a message in the queue.
 /// </returns>
 /// <param name="timeout">A <see cref="T:System.TimeSpan"/> that indicates the time to wait until a new message is available for inspection. </param><param name="cursor">A <see cref="T:System.Messaging.Cursor"/> that maintains a specific position in the message queue.</param><param name="transactionType">One of the <see cref="T:System.Messaging.MessageQueueTransactionType"/> values that describes the type of transaction context to associate with the message. </param><exception cref="T:System.ArgumentNullException">The <paramref name="cursor"/> parameter is null.</exception><exception cref="T:System.ArgumentException">The value specified for the <paramref name="timeout"/> parameter is not valid. Possibly <paramref name="timeout"/> is less than <see cref="F:System.TimeSpan.Zero"/> or greater than <see cref="F:System.Messaging.MessageQueue.InfiniteTimeout"/>. </exception><exception cref="T:System.ComponentModel.InvalidEnumArgumentException">The <paramref name="transactionType"/> parameter is not one of the <see cref="T:System.Messaging.MessageQueueTransactionType"/> members. </exception><exception cref="T:System.Messaging.MessageQueueException">A message did not arrive in the queue before the time-out expired.-or- An error occurred when accessing a Message Queuing method. </exception>
 public Message Receive(TimeSpan timeout, Cursor cursor, MessageQueueTransactionType transactionType)
 {
     return _queue.Receive(timeout, cursor, transactionType);
 }
예제 #21
0
        public MessageInfo PeekAllMessage(StructuredQueue oQueueTemp, int myID, int MaxQuery)
        {
            //Message messages = null;
            MessageInfo MyInfo = new MessageInfo();

            try
            {
                MessageQueue            MessageInQueue = new System.Messaging.MessageQueue(oQueueTemp.sPath, QueueAccessMode.SendAndReceive);
                System.Messaging.Cursor cursor         = MessageInQueue.CreateCursor();
                Message m = PeekWithoutTimeout(MessageInQueue, cursor, PeekAction.Current);
                Application.DoEvents();
                ArrayList MyMessage = new ArrayList();
                MessageInQueue.MessageReadPropertyFilter.Id          = true;
                MessageInQueue.MessageReadPropertyFilter.Priority    = true;
                MessageInQueue.MessageReadPropertyFilter.SentTime    = true;
                MessageInQueue.MessageReadPropertyFilter.MessageType = true;
                MessageInQueue.MessageReadPropertyFilter.Label       = true;
                MessageInQueue.MessageReadPropertyFilter.Body        = true;
                MessageInQueue.BeginPeek(new TimeSpan(0, 0, 10, 0));
                //MessageEnumerator msgQenum = MessageInQueue.GetMessageEnumerator2();
                int index = 0;
                //  MyInfo.ID = MessageInQueue.GetAllMessages().Length;
                if (m != null)
                {
                    while ((m = PeekWithoutTimeout(MessageInQueue, cursor, PeekAction.Current)) != null)
                    //while (msgQenum.MoveNext())
                    {
                        Application.DoEvents();

                        if (!(index > MaxQuery))
                        {
                            //                       messages = msgQenum.Current;
                            QueueInfos QueueInfo = new QueueInfos();
                            //QueueInfo.ID = messages.Id;
                            //QueueInfo.SentTime = messages.SentTime.ToString();
                            //QueueInfo.Body = ReadMessageStream(messages.BodyStream);
                            //QueueInfo.Label = messages.Label;
                            //QueueInfo.Priority = messages.Priority.ToString();
                            //QueueInfo.MessageID = index.ToString();
                            //QueueInfo.Transact = oQueueTemp.bTransactional;
                            //QueueInfo.Queue = oQueueTemp.sQueueName;
                            QueueInfo.ID        = m.Id;
                            QueueInfo.SentTime  = m.SentTime.ToString();
                            QueueInfo.Body      = ReadMessageStream(m.BodyStream);
                            QueueInfo.Label     = m.Label;
                            QueueInfo.Priority  = m.Priority.ToString();
                            QueueInfo.MessageID = index.ToString();
                            QueueInfo.Transact  = oQueueTemp.bTransactional;

                            QueueInfo.Queue = oQueueTemp.sQueueName;
                            MyMessage.Add(QueueInfo);
                        }
                        index++;
                        PeekWithoutTimeout(MessageInQueue, cursor, PeekAction.Next);
                    }
                }
                MyInfo.ID      = index;
                MyInfo.Message = MyMessage;
            }
            catch  {}

            return(MyInfo);
        }