当前位置:编程学习 > C#/ASP.NET >>

关于获取MSMQ消息sourcemachine

本人最近用MSMQ,无法获取MSMQ消息sourcemachine。按说我对它掌握的很熟悉了,不仅能够利用它进行消息发送,而且可以把文件分片、消息化、发送、接收、分片重组

等操作
可是就是无法获取消息的sourcemachine。现在的笨方法是发送消息的时候,把机器名写入msg的label里。不知大家还有什么高招。
我的QQ:86152622
欢迎交流 --------------------编程问答-------------------- using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Messaging;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;
using System.Timers;
using System.Net;
namespace WindowsApplication3
{
    public partial class MSMQ_manager : Form
    {
        private System.Messaging.MessageQueue targetMessageQueue;//目标消息队列
        public MSMQ_manager()
        {
            InitializeComponent();
        }
       private void remote_msg_send_Click(object sender, EventArgs e)
        {
            string messageQueueAddr = "FormatName:Direct=" + this.remote_IP_address.Text + "\\Private$\\msg";
            this.targetMessageQueue = new MessageQueue(messageQueueAddr);
            this.targetMessageQueue.MessageReadPropertyFilter.SourceMachine = true;
            System.Messaging.Message message = new System.Messaging.Message();
            message.AdministrationQueue = new MessageQueue(@".\private$\msg_sent");
            message.AcknowledgeType = AcknowledgeTypes.PositiveReceive | AcknowledgeTypes.PositiveArrival;

           //笨方法,在label中设置机器名
            string machineName = System.Net.Dns.GetHostName();
            System.Net.IPAddress addr; 
            addr = new System.Net.IPAddress(Dns.GetHostByName(Dns.GetHostName()).AddressList[0].Address); 

            //IPHostEntry ip=System.Net.Dns.GetHostByName(machineName);
            //string machineIP = System.Net.Dns.GetHostByName((GetHostName();
            message.Label =machineName+"["+addr.ToString()+"]";
            message.Body = this.remote_send_msg.Text.Trim();
            //message.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(string) });
            targetMessageQueue.Send(message);
            //listBox1.Items.Add("[send to " + this.remote_IP_address.Text + ":]");
            listBox1.Items.Add(machineName + "[" + addr.ToString() + "]:");
            listBox1.Items.Add(this.remote_send_msg.Text.Trim());

        }
        
        private void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (this.InvokeRequired)
            {
                try
                {
                    ElapsedEventHandler OnTick = new ElapsedEventHandler(this.timer_Elapsed);
                    this.Invoke(OnTick, new object[] { sender, e });
                }
                catch
                { }
                return;
            }
            //this.label1.Text = DateTime.Now.ToString();

            System.Messaging.MessageQueue Mq;
            Mq = new System.Messaging.MessageQueue(@".\Private$\msg");
            Mq.MessageReadPropertyFilter.SourceMachine = true;
            Mq.MessageReadPropertyFilter.Body = true;
            //mq.MessageReadPropertyFilter.SourceMachine = true;
            //mq.MessageReadPropertyFilter.Body = true; 
            //使用MessageEnumerator遍历消息
            System.Messaging.MessageEnumerator enumerator = Mq.GetMessageEnumerator2();
            while (enumerator.MoveNext())
            {
                System.Messaging.Message Msg = Mq.Receive();
                Msg.Formatter = new XmlMessageFormatter(new Type[] { typeof(String) });
                //listBox1.Items.Add("[from " +Msg.SourceMachine+ ":]" + Msg.Body.ToString());
                //listBox1.Items.Add("[from " +Msg.SourceMachine..ToString() + ":]" + Msg.Body.ToString());
               // try{
               // string str = Msg.SourceMachine;
               // MessageBox.Show(str);}
               // catch (System.Exception ex){ MessageBox.Show(ex.Message.ToString()); }
                //listBox1.Items.Add("[receive:]" + Msg.Body.ToString());
                string str = Msg.Label;
                listBox1.Items.Add(str+":");
                listBox1.Items.Add(Msg.Body.ToString());
                //MessageBox.Show(Msg.SourceMachine.ToString());
                //Msg.getSourceMachine.ToString();SourceMachine { get; };


            }
        } 

        private void MSMQ_manager_Load(object sender, EventArgs e)
        {
            System.Timers.Timer timer = new System.Timers.Timer(3000);
            timer.Enabled = true;
            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed); 
        }

        private void rebuild_mq_Click(object sender, EventArgs e)
        {
            DeleteQueue(@".\Private$\data");
            DeleteQueue(@".\Private$\data_sent");
            DeleteQueue(@".\Private$\msg");
            DeleteQueue(@".\Private$\msg_sent");
            //重新建立约定队列
            CreateQueue(@".\Private$\data");    //数据接收队列
            CreateQueue(@".\Private$\data_sent");    //数据发送回执队列
            CreateQueue(@".\Private$\msg");    //消息接收队列
            CreateQueue(@".\Private$\msg_sent");    //消息发送回执队列

        }

        /**************************************************
        // Creates a new queue.
        @".\Private$\data";    //数据接收队列
        @".\Private$\data_sent";    //数据发送回执队列
        @".\Private$\msg";    //消息接收队列
        @".\Private$\msg_sent";    //消息发送回执队列
        //**************************************************/
        public static void CreateQueue(string queuePath)
        {
            try
            {
                if (!MessageQueue.Exists(queuePath))
                {
                    MessageQueue.Create(queuePath);
                }
                else
                {
                    Console.WriteLine(queuePath + " already exists.");
                }
            }
            catch (MessageQueueException e)
            {
                Console.WriteLine(e.Message);
            }

        }

        /**************************************************
        // Delete a queue.
        @".\Private$\data";    //数据接收队列
        @".\Private$\data_sent";    //数据发送回执队列
        @".\Private$\msg";    //消息接收队列
        @".\Private$\msg_sent";    //消息发送回执队列
        //**************************************************/
        public static void DeleteQueue(string queuePath)
        {
            try
            {
                if (MessageQueue.Exists(queuePath))
                {
                    MessageQueue.Delete(queuePath);
                }
                else
                {
                    Console.WriteLine(queuePath + " is not exists.");
                }
            }
            catch (MessageQueueException e)
            {
                Console.WriteLine(e.Message);
            }

        }

        private void clear_list_Click(object sender, EventArgs e)
        {
            this.listBox1.Items.Clear();
        }



    }
} --------------------编程问答-------------------- namespace WindowsApplication3
{
    partial class MSMQ_manager
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MSMQ_manager));
            this.listBox1 = new System.Windows.Forms.ListBox();
            this.panel1 = new System.Windows.Forms.Panel();
            this.label3 = new System.Windows.Forms.Label();
            this.remote_send_msg = new System.Windows.Forms.TextBox();
            this.remote_msg_send = new System.Windows.Forms.Button();
            this.remote_IP_address = new System.Windows.Forms.TextBox();
            this.rebuild_mq = new System.Windows.Forms.Button();
            this.clear_list = new System.Windows.Forms.Button();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // listBox1
            // 
            this.listBox1.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.listBox1.FormattingEnabled = true;
            this.listBox1.HorizontalScrollbar = true;
            this.listBox1.ItemHeight = 16;
            this.listBox1.Location = new System.Drawing.Point(8, 75);
            this.listBox1.Name = "listBox1";
            this.listBox1.ScrollAlwaysVisible = true;
            this.listBox1.Size = new System.Drawing.Size(295, 244);
            this.listBox1.TabIndex = 8;
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.listBox1);
            this.panel1.Location = new System.Drawing.Point(9, 4);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(312, 335);
            this.panel1.TabIndex = 10;
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(18, 58);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(89, 12);
            this.label3.TabIndex = 16;
            this.label3.Text = "目标机器地址:";
            // 
            // remote_send_msg
            // 
            this.remote_send_msg.Location = new System.Drawing.Point(18, 12);
            this.remote_send_msg.Multiline = true;
            this.remote_send_msg.Name = "remote_send_msg";
            this.remote_send_msg.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.remote_send_msg.Size = new System.Drawing.Size(297, 24);
            this.remote_send_msg.TabIndex = 6;
            this.remote_send_msg.Text = "say hello to everyone.";
            // 
            // remote_msg_send
            // 
            this.remote_msg_send.Location = new System.Drawing.Point(242, 47);
            this.remote_msg_send.Name = "remote_msg_send";
            this.remote_msg_send.Size = new System.Drawing.Size(65, 23);
            this.remote_msg_send.TabIndex = 1;
            this.remote_msg_send.Text = "发送消息";
            this.remote_msg_send.UseVisualStyleBackColor = true;
            this.remote_msg_send.Click += new System.EventHandler(this.remote_msg_send_Click);
            // 
            // remote_IP_address
            // 
            this.remote_IP_address.Location = new System.Drawing.Point(112, 52);
            this.remote_IP_address.Name = "remote_IP_address";
            this.remote_IP_address.Size = new System.Drawing.Size(107, 21);
            this.remote_IP_address.TabIndex = 13;
            this.remote_IP_address.Text = "TCP:192.168.1.211";
            // 
            // rebuild_mq
            // 
            this.rebuild_mq.Location = new System.Drawing.Point(208, 345);
            this.rebuild_mq.Name = "rebuild_mq";
            this.rebuild_mq.Size = new System.Drawing.Size(107, 23);
            this.rebuild_mq.TabIndex = 17;
            this.rebuild_mq.Text = "重构所有队列";
            this.rebuild_mq.UseVisualStyleBackColor = true;
            this.rebuild_mq.Click += new System.EventHandler(this.rebuild_mq_Click);
            // 
            // clear_list
            // 
            this.clear_list.Location = new System.Drawing.Point(14, 345);
            this.clear_list.Name = "clear_list";
            this.clear_list.Size = new System.Drawing.Size(98, 23);
            this.clear_list.TabIndex = 18;
            this.clear_list.Text = "清空列表";
            this.clear_list.UseVisualStyleBackColor = true;
            this.clear_list.Click += new System.EventHandler(this.clear_list_Click);
            // 
            // MSMQ_manager
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(343, 387);
            this.Controls.Add(this.clear_list);
            this.Controls.Add(this.rebuild_mq);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.remote_send_msg);
            this.Controls.Add(this.remote_IP_address);
            this.Controls.Add(this.remote_msg_send);
            this.Controls.Add(this.panel1);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "MSMQ_manager";
            this.Text = "MSMQ消息队列管理程序";
            this.Load += new System.EventHandler(this.MSMQ_manager_Load);
            this.panel1.ResumeLayout(false);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.TextBox remote_send_msg;
        private System.Windows.Forms.Button remote_msg_send;
        private System.Windows.Forms.TextBox remote_IP_address;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.ListBox listBox1;
        private System.Windows.Forms.Button rebuild_mq;
        private System.Windows.Forms.Button clear_list;
    }
}

补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,