简体   繁体   English

如何找到我在msmq中添加的消息

[英]How can I find the message I added in my msmq

I wet through this except that I added it to a windows service like this 我通过湿只是我把它加入到这样一个窗口服务

public partial class TriggerHostProcesses : ServiceBase
{
    private const string MESSAGE_QUEUE = @".\Private$\Sample Queue";
    private MessageQueue _queue;
    public TriggerHostProcesses()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        SendMessage("Hope This Works");
    }

    protected override void OnStop()
    {
    }

    internal void start()
    {
        OnStart(null);
    }


    private void SendMessage(string message)
    {
        _queue = new MessageQueue(MESSAGE_QUEUE);
        Message msg = new Message();
        msg.Body = message;
        msg.Label = "Testing " + DateTime.Now.ToString();
        _queue.Send(msg,new MessageQueueTransaction());
    }
}

and to get the message 并得到消息

 partial class HostListener : ServiceBase
{
      private const string MESSAGE_QUEUE = @".\Private$\Sample Queue";
    private MessageQueue _queue;

    public HostListener()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        try
        {
            var myTransaction = new MessageQueueTransaction();

            var queue = new MessageQueue(MESSAGE_QUEUE);
            var message = queue.Receive(new TimeSpan(0, 0, 20),myTransaction);
            message.Formatter = new XmlMessageFormatter(
                                new String[] { "System.String,mscorlib" });
            Console.WriteLine(message.Body.ToString());
        }
        catch(Exception ex)
        {
            Console.WriteLine("No Message");
        }
    }

    protected override void OnStop()
    {
        // TODO: Add code here to perform any tear-down necessary to stop your service.
    }

    internal void start()
    {
        OnStart(null);
    }
}

in my main I added this 在我的主要我添加了这个

  var ServiceToRun1 = new TriggerHostProcesses();
        var ServiceToRun2 = new HostListener();

        if (Environment.UserInteractive)
        {
            // This used to run the service as a console (development phase only)

            ServiceToRun1.start();
            ServiceToRun2.start();

            Console.WriteLine("Press Enter to terminate ...");
            Console.ReadLine();

            ServiceToRun1.Stop();
            ServiceToRun2.Stop();
        }
        else
        {
            ServiceBase.Run(ServiceToRun1);
        }

I get the exception Timeout for the requested operation has expired. 我得到异常Timeout for the requested operation has expired.

Can someone please check if they can see what the problem is? 有人可以检查他们是否可以看到问题是什么?

I don't believe you are using transactions correctly. 我不相信你正确使用交易。 For example, when sending a message you use: 例如,在发送消息时,您使用:

_queue.Send(msg,new MessageQueueTransaction());

However, this does not begin or commit a transaction. 但是,这不会开始或提交事务。 Looking in MSDN the example uses the following code (edited by me): MSDN中查看该示例使用以下代码(由我编辑):

var myTransaction = new MessageQueueTransaction();
myTransaction.Begin();
myQueue.Send("hello world", myTransaction);
myTransaction.Commit();

I don't believe your message is getting sent, and so your Receive times out. 我不相信您的邮件已发送,因此您的Receive时间已过去。

Similarly your receive logic doesn't seem to correctly use transactions: 同样,您的接收逻辑似乎没有正确使用事务:

myTransaction.Begin();
var myMessage = myQueue.Receive(myTransaction); 
var body myOrder = (string)myMessage.Body;
myTransaction.Commit();

You should Rollback in the event of an exception processing your messages so they can be placed back on the queue. 如果处理消息的异常,您应该Rollback ,以便将它们放回队列。

Here is my final product. 这是我的最终产品。 I'm using this in a windows service. 我在Windows服务中使用它。 20 s at a time to see if I have a message then do my processes. 一次20秒,看看我是否有消息然后做我的过程。

public class MSMQueue:IQueue
    {

        public MSMQueue(string queueName)
        {
            Message_Queue = queueName;
        }

        public string Message_Queue { get; private set; }

        public string Pop()
        {
            MessageQueue queue = new MessageQueue(Message_Queue);

            if (queue.Transactional)
                return popTransactionalQueue(queue, new TimeSpan(0, 0, 1));
            else
                return popNormalQueue(queue, new TimeSpan(0, 0, 1));
        }

        public string Pop(TimeSpan timeSpan)
        {
            MessageQueue myQueue = new MessageQueue(Message_Queue);

            if (myQueue.Transactional)
                return popTransactionalQueue(myQueue, timeSpan);
            else
                return popNormalQueue(myQueue, timeSpan);
        }

        public void Add(string message)
        {
            // Connect to a queue on the local computer.
            MessageQueue myQueue = new MessageQueue(Message_Queue);

            // Send a message to the queue.
            if (myQueue.Transactional)
            {
                var myTransaction = new MessageQueueTransaction();
                myTransaction.Begin();
                myQueue.Send(message, myTransaction);
                myTransaction.Commit();
            }
            else
                myQueue.Send(message);
        }

        #region private methods

        private string popNormalQueue(MessageQueue queue, TimeSpan timeOut)
        {
            var message = queue.Receive(timeOut);
            message.Formatter = new XmlMessageFormatter(
                                new String[] { "System.String,mscorlib" });
            return message.Body.ToString();
        }

        private string popTransactionalQueue(MessageQueue queue, TimeSpan timeOut)
        {

            // Set the formatter.
            queue.Formatter = new XmlMessageFormatter(new Type[]
                {typeof(String)});

            // Create a transaction.
            MessageQueueTransaction myTransaction = new 
                MessageQueueTransaction();

            String message=string.Empty;

            try
            {
                myTransaction.Begin();

                Message myMessage = queue.Receive(timeOut, myTransaction); 
                message = (String)myMessage.Body;

                myTransaction.Commit();

            }

            catch (MessageQueueException e)
            {
                myTransaction.Abort();
                throw e;
            }

            return message;
        }

        #endregion
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM