• 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_SourcesDotNetMQinDebug,该项目的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();
            }
        }
    }
  • 相关阅读:
    淘宝技术分享
    15个富有创意的单页设计
    jQuery全能图片滚动插件
    jquery性能优化
    Algs4-1.4.40随机输入3-sum问题
    Algs4-1.4.39 改进倍率测试的精度
    Algs4-1.4.38 3-sum的初级算法与ThreeSum性能比较
    Algs4-1.4.37自动装箱的性能代价
    Algs4-1.4.35下压栈的时间成本
    Algs4-1.4.36下压栈的空间成本
  • 原文地址:https://www.cnblogs.com/majiang/p/5999126.html
Copyright © 2020-2023  润新知