MSMQ操作相关

1:创建队列

public  void CreateQueue(string queuePath)
        {
            try
            {
                if (!MessageQueue.Exists(queuePath))
                {
                    MessageQueue.Create(str);
                    MessageBox.Show("创建队列成功!");
                }
                else
                {
                    MessageBox.Show(queuePath + "已经存在");
                }
            }
            catch (MessageQueueException e)
            {
                MessageBox.Show(e.Message);
 
            }
        }

  远程创建

    public static void CreatQuerry()
        {
            try
            {
                System.Messaging.MessageQueue mq = new System.Messaging.MessageQueue(@"FormatName:DIRECT=TCP:222.18.158.119\private$\test");                
                if (mq != null)
                {
                    MessageBox.Show("创建成功");
                }
            }
            catch (MessageQueueException e)
            {
                Console.WriteLine(e.Message);
            }
        }

接受TextBox传送的值

  

     private void btnCreate_Click(object sender, EventArgs e)
        {
            string str = @".\private$\" + createText.Text;
            
            CreateQueue(str);
        }

  

     public  void CreateQueue(string queuePath)
        {      
           try
            {
                if (!MessageQueue.Exists(queuePath))
                {
                    MessageQueue.Create(queuePath);
                    MessageBox.Show("创建队列成功!");
                }
                else
                {
                    MessageBox.Show(queuePath + "已经存在");
                }
            }
            catch (MessageQueueException e)
            {
                MessageBox.Show(e.Message);

            }
        }

  

 

发送消息:

public static bool SendMessage(Book book)
        {
            bool flag = false;
            try
            {
                //连接到本地的队列
                MessageQueue myQueue = new MessageQueue(".\\private$\\newMyQueue");
 
                System.Messaging.Message myMessage = new System.Messaging.Message();
                myMessage.Body = book;
               myMessage.Formatter = new XmlMessageFormatter(new Type[] { typeof(MSMQ.App.Book) });
                //发送消息到队列中
                myQueue.Send(myMessage);
                flag = true;
            }
            catch (ArgumentException e)
            {
                MessageBox.Show(e.Message);
            }
            return flag;
        }

  

   public static void SendMsg()
        {
            try
            {
                System.Messaging.MessageQueue Queue;
                Queue = new System.Messaging.MessageQueue(@"FormatName:DIRECT=TCP:192.168.0.101\private$\test");                
                System.Messaging.Message Msg = new System.Messaging.Message();                           
                Msg.Formatter = new System.Messaging.BinaryMessageFormatter();
                Msg.Body = "My test";
                Queue.Send(Msg);
                
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.ToString());
            } 
        }

  

   private void sendMessage_Click(object sender, EventArgs e)
        {
            //MessageQueue msg = new MessageQueue(str);
            
            System.Messaging.Message theMessage = new System.Messaging.Message();
            theMessage.Body = txtMessage.Text;
            theMessage.Label = txtName.Text;
            if (highPriority.Checked)
                theMessage.Priority = System.Messaging.MessagePriority.Highest;
            else
                theMessage.Priority = System.Messaging.MessagePriority.Normal;
            helpRequestQueue.Send(theMessage);
            DisplayMessages();
 
        }

  接受消息:

 public static string GetMsg()
        {
            try
            {
                System.Messaging.MessageQueue mq = new System.Messaging.MessageQueue(@".\private$\test");
 
                // Set the queue'ss formatter to decode Point objects
 
                mq.Formatter = new System.Messaging.BinaryMessageFormatter();
                System.Messaging.Trustee trustee = new System.Messaging.Trustee();
 
                System.Messaging.Message msg = mq.Peek(new TimeSpan(1000));
 
                // Convert received message to object that we think was sent
 
                string pt = (string)msg.Body;
 
                // Display it to the user
 
                return pt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return null;
            }
        }

  

public static string ReceiveMessage()
        {
            //连接到本地队列
            MessageQueue myQueue = new MessageQueue(".\\private$\\myQueue");
            myQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(MSMQ.App.Book) });
            try
            {
                //从队列中接收消息
                System.Messaging.Message myMessage = myQueue.Receive();
                Book book = (Book)myMessage.Body; //获取消息的内容
                return string.Format("编号:{0},书名:{1},作者:{2},定价:{3}",
                    book.BookId,
                    book.BookName,
                    book.BookAuthor,
                    book.BookPrice);
            }
            catch (MessageQueueException e)
            {
                MessageBox.Show(e.Message);
            }
            catch (InvalidCastException e)
            {
                MessageBox.Show(e.Message);
            }
            return null;
        }

  

转载于:https://www.cnblogs.com/fromcuit/archive/2012/05/13/2498600.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值