zoukankan      html  css  js  c++  java
  • 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();
            }
        }
    }
  • 相关阅读:
    如何把python最小化安装在客户机上面
    c++的历史-异常
    插件系统的构建
    ora-01445:无法从不带保留关键字的表的连接视图中选择ROWID或采样
    转--Oracle 审计和测试操作
    foreach属性-动态-mybatis中使用map类型参数,其中key为列名,value为列值
    C语言中 *.c和*.h文件的区别!
    解决方法:loadrunner 场景下执行webservice脚本是---报错10492 Error: Exception was raised when calling per-process-init function in extens
    NoSQL之Redis高级实用命令详解--安全和主从复制
    Expect安装方法
  • 原文地址:https://www.cnblogs.com/majiang/p/5999126.html
Copyright © 2011-2022 走看看