DotNetMQ

来源:互联网 发布:知柏地黄丸女人吃多久 编辑:程序博客网 时间:2024/05/02 12:03

DotNetMQ-基于C#和.NET框架的开源消息队列系统

这是一位NB的土耳其开发人员基于C#和.NET框架,编写的开源消息队列系统DotNetMQ。DotNetMQ消息组件支持消息可靠传输、路由、负载均衡、服务器部署图(Server Graphs)等等。
企业级应用系统经常需要通过消息来进行沟通或集成,在Microsoft 平台,还有其他的类似解决方案,如MSMQ、BizTalk、SQL Server Service Broker等等,这些产品或解决方案有各自的特点和适用场景。这里推荐的DotNetMQ消息系统,也是其适应的业务场景。

DotNetMQ 消息系统特性列表:

  • Persistent or non-persistent messaging.
  • Guaranteed delivery of persistent messages even in a system crash.
  • Automatic and manual routing of messages in a custom machine graph.
  • Supports multiple databases (MySQL, SQLite, and memory-based storage for now).
  • Supports don’t store, direct send style messaging.
  • Supports Request/Reply style messaging.
  • Easy to use client library to communicate with the DotNetMQ Message Broker.
  • Built-in framework to easily construct RMI services upon message queues.
  • Supports delivering messages to ASP.NET Web Services.
  • GUI-based management and monitoring tool.
  • Easy to install, manage, and use.
  • Written entirely in C# (using .NET Framework 3.5).

在多个Server 之间自动路由消息:

EntLib.com Team 随后会提供针对DotNetMQ 消息队列系统在.NET项目中更详细的中文使用范例。

DotNetMQ的具体介绍和源码下载,请访问如下链接:
DotNetMQ: A Complete Message Queue System for .NET

 

DotNetMQ-基于C#和.NET框架的开源消息队列系统 – 安装部署及消息发送接收范例

关于DotNetMQ 开源消息队列系统的介绍,可参考如下文章:

DotNetMQ-基于C#和.NET框架的开源消息队列系统

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

1. 先看看DotNetMQ 项目源码

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

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

2. 部署DotNetMQ 服务

该服务编译的DLL程序集在如下目录 — DotNetMQ_Sources\DotNetMQ\bin\Debug

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

installutil dotnetmq.exe

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

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

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

如上图所示,添加好应用程序配置之后,可以到DotNetMQ_Sources\DotNetMQ\bin\Debug 目录下,查看MDSSettings.xml 配置文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xmlversion="1.0"encoding="utf-8"?>
<MDSConfiguration>
<Settings>
<SettingKey="ThisServerName"Value="this_server"/>
<SettingKey="StorageType"Value="SQLite"/>
</Settings>
<Servers>
<ServerIpAddress="127.0.0.1"Port="10905"Adjacents=""/>
</Servers>
<Applications>
<Application/>
<Application/>
</Applications>
</MDSConfiguration>

4. 开发和运行客户端范例程序

在Visual studio 2010 开发工具下,编写2个简单的Console 应用程序:Application1和Application2。其中,Application1 用来发送消息;Application2 用来接收并显示消息。

在上述程序中,需要添加对 MDSCommonLib 程序集的引用,并且在代码中添加MDS.Client 命名空间的引用。

Application1 发送消息部分的代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MDS.Client;
namespaceApplication1
{
class Program
{
staticvoid Main(string[] args)
{
var mdsClient =new MDSClient("Application1");
mdsClient.Connect();
Console.WriteLine("输入文本,并按回车键传送消息给Application2,输入exit退出。");
while(true)
{
var messageText = Console.ReadLine();
if (string.IsNullOrEmpty(messageText) || messageText =="exit")
{
break;
}
  
var message = mdsClient.CreateMessage();
message.DestinationApplicationName ="Application2";
message.MessageData = Encoding.UTF8.GetBytes(messageText);
message.Send();
}
mdsClient.Disconnect();
}
}
}

Application2 接收消息的代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MDS.Client;
namespaceApplication2
{
class Program
{
staticvoid 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("输入回车键退出...");
Console.ReadLine();
//Disconnect from DotNetMQ server
mdsClient.Disconnect();
}
  
/// <summary>
/// This method handles received messages from other applications via DotNetMQ.
/// </summary>
/// <param></param>
/// <param>Message parameters</param>
staticvoid MDSClient_MessageReceived(objectsender, MessageReceivedEventArgs e)
{
//Get message
var messageText = Encoding.UTF8.GetString(e.Message.MessageData);
//Process message
Console.WriteLine();
Console.WriteLine("接收的消息 : "+ messageText);
Console.WriteLine("发送方应用程序 : "+ e.Message.SourceApplicationName);
  
//Acknowledge that message is properly handled
//and processed. So, it will be deleted from queue.
e.Message.Acknowledge();
}
}
}

启动Application1应用程序之后,再次查看MDSManager管理程序,发现Connected Clients由原来的 0 变成 1,说明现在有1个Application1应用程序连接到DotNetMQ服务了。

在Application1的运行窗口,输入消息:

Hello, welcome to www.entlib.com ecommerce system

您好,欢迎访问 www.entlib.com 电子商务平台

启动Application2 应用程序,就可以在Application2的应用程序界面看到接收的消息,如下图所示。

最后,在Application1应用程序输入 exit 退出应用程序。当然在Application2 应用程序最后也可输入 exit 退出应用程序。

如果Application1应用程序启动,并发送消息;而Application2应用程序尚未启动,Application1发送的消息则默认存放在SQLite 数据库中,关于SQLite数据库的简单介绍,可参考:SQLite 开源的嵌入式关系数据库

参考链接:

DotNetMQ-基于C#和.NET框架的开源消息队列系统

SQLite 开源的嵌入式关系数据库

 

DotNetMQ-基于C#和.NET框架的开源消息队列系统 – 多实例发送和接收消息

在《DotNetMQ-基于C#和.NET框架的开源消息队列系统》一文中,简要介绍了DotNetMQ消息组件的功能。

在《DotNetMQ-基于C#和.NET框架的开源消息队列系统 – 安装部署及消息发送接收范例》一文中,详细介绍了DotNetMQ消息组件的安装部署及消息发送接收范例,通过该范例,可以在实际项目中应用DotNetMQ 消息组件。

本文进一步演示一些多实例(Multiple Instance)场景。

针对上述范例,如果Application2 应用程序有多个实例在运行,到底哪一个Application2 应用程序实例接收到消息呢?

在这种情况下,DotNetMQ 将按顺序发送消息给对应的应用程序,如下图所示。

Application1发送的消息,会按顺序、逐个由三个Application2应用程序实例分别接收,但是每一个消息仅由一个应用程序实例接收,每一个应用程序实例接收不同的消息。其实,这一情况一般不太适合实际的业务场景。

下面在分析另一个业务场景,多个消息发送方,一个消息接收方。

如下图所示,有两个Application1 应用程序 – 发送消息给Application2;Application2 应用程序只有一个实例,会同时接收来自多个Application1应用程序实例的消息。

DotNetMQ 支持消息队列,不支持消息主题(Topic),因此消息仅仅发送给唯一一个接收者,而不是同时发送给多个实例。也就是说,DotNetMQ 支持“一个消息发送方 — 一个消息接收方”,而不是“一个消息发送方 — 多个消息接收方”。

MDSClient 仅仅根据应用程序名称来接收消息,DotNetMQ 消息组件和经典的发布/订阅(Pub/Sub)消息系统不一样哦。

其他参考链接:

DotNetMQ-基于C#和.NET框架的开源消息队列系统

原创粉丝点击