DotNetMQ的一个小demo

 DotNetMQ是一个新的、独立的、开源的,完全基于C#和.NET Framework3.5的消息队列系统

下载源代码 - 1.28 MB

下载二进制文件 - 933 KB

下载例子 - 534 KB

下面具体介绍如何安装、部署以及使用DotNetMQ 消息组件。

1. 先看看DotNetMQ 项目源码

DotNetMQ 项目是消息组件服务;MDSManager 项目消息组件的管理界面,用来配置系统中的客户端机器。

MDSCommonLib 项目是客户端系统需要引用的DLL程序集,使客户端系统可以和MDS 服务进行交互。

2. 部署DotNetMQ 服务

该服务编译的DLL程序集在如下目录 — DotNetMQ_Sources\DotNetMQ\bin\Debug,该项目的System.Data.SQLite.dll是x86,所以可以考虑把项目改为x86平台

在CMD窗口中,在上述目录下,执行如下命令,安装部署 DotNetMQ 服务:

installutil dotnetmq.exe

看看安装好的DotNetMQ服务,并启动服务。

3. 注册和配置DotNetMQ 消息应用程序

在安装部署好DotNetMQ服务之后,开始运行MDSManager.exe 程序,添加和注册客户端应用程序,如下所示,添加Application1和Application2 应用程序。

下载后的FirstApplication运行结果如下:

相关Code:

using System;
using System.Text;
using MDS.Client;

namespace Application1
{
    class Program
    {
        static void Main()
        {
            //Create MDSClient object to connect to DotNetMQ
            //Name of this application: Application1
            var mdsClient = new MDSClient("Application1");

            //Connect to DotNetMQ server
            mdsClient.Connect();

            Console.WriteLine("Write a text and press enter to send to Application2. Write 'exit' to stop application.");

            while (true)
            {
                //Get a message from user
                var messageText = Console.ReadLine();
                if (string.IsNullOrEmpty(messageText) || messageText == "exit")
                {
                    break;
                }

                //Create a DotNetMQ Message to send to Application2
                var message = mdsClient.CreateMessage();
                //Set destination application name
                message.DestinationApplicationName = "Application2";
                //message.DestinationServerName = "this_server2";
                //Set message data
                message.MessageData = Encoding.UTF8.GetBytes(messageText);

                //Send message
                message.Send();
            }

            //Disconnect from DotNetMQ server
            mdsClient.Disconnect();
        }
    }
}
using System;
using System.Text;
using MDS.Client;

namespace Application2
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create MDSClient object to connect to DotNetMQ
            //Name of this application: Application2
            var mdsClient = new MDSClient("Application2");

            //Register to MessageReceived event to get messages.
            mdsClient.MessageReceived += MDSClient_MessageReceived;

            //Connect to DotNetMQ server
            mdsClient.Connect();

            //Wait user to press enter to terminate application
            Console.WriteLine("Press enter to exit...");
            Console.ReadLine();

            //Disconnect from DotNetMQ server
            mdsClient.Disconnect();
        }

        /// <summary>
        /// This method handles received messages from other applications via DotNetMQ.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e">Message parameters</param>
        static void MDSClient_MessageReceived(object sender, MessageReceivedEventArgs e)
        {
            //Get message
            var messageText = Encoding.UTF8.GetString(e.Message.MessageData);

            //Process message
            Console.WriteLine();
            Console.WriteLine("Text message received : " + messageText);
            Console.WriteLine("Source application    : " + e.Message.SourceApplicationName);

            //Acknowledge that message is properly handled and processed. So, it will be deleted from queue.
            e.Message.Acknowledge();
        }
    }
}

 

posted on 2016-10-26 09:36  dz45693  阅读(2068)  评论(0编辑  收藏  举报

导航