Message Queue基本使用说明
一、安装Message Queue:
在Win7之前,控制面板,添加删除组件(Windows Message Queue)。
Win7~Win8:控制面板,程序和功能,启用或关闭Windows功能(找到Windows Message Queue服务器)选项,连同所有子类一并勾上即可,自动安装。
二、使用Message Queue:
1)用于各类服务器、计算机之间的通讯:
本地,自己给自己发(直接是.\\Private$\\Queue的私有名字)。
远程计算机:
FormatName:Direct=TCP:121.0.0.1\\private$\\Queue名字
FormatName:Direct=OS:machinename\\private$\\Queue名字 (仅用于远程加了域的计算机)。
FormatName:DIRECT=http://222.10.xx.xx/msmq/Private$/Queue名字
注意:FormatName必须大小写完全按照红色的写法!"Direct"可以随意。
2)默认情况下,Queue会收到该组全部信息。有时候我们需要跨域但是指定某台计算机收到特定信息(比如N台有1,2,3……N编号的计算机都连接到某个服务器Queue,但是不同计算机有唯一编号,我发送的信息只发送给特定的计算机编号)。那么可以使用System.Message(使用前必须引用该类库)的Label,代码如下(包含收到消息后删除该信息,防止Queue里东西越来越多):
此案例是在我本人计算机上运行,界面上一个“发送”按钮和2个文本框(分别接受Label为1和2的消息),防止时间演示过长,使用了异步机制。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Messaging;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Msg = System.Messaging.Message; namespace MessageQueueDemo
{
public partial class Form1 : Form
{ public Form1()
{
InitializeComponent();
} private async Task<string> GetMessage(string labelId)
{
List<Msg> msgs = new List<Msg>();
//监听当前自己计算机上的私有队列
MessageQueue msgQueue = new MessageQueue(@".\private$\myQueue");
string result = null;
Random r = new Random(); await Task.Run(() =>
{
//使用该方法允许您动态删除Queue
var me = msgQueue.GetMessageEnumerator2();
Msg tmpMsg = null; for (IEnumerator pointer = me; pointer.MoveNext();)
{
//先返回当前的Queue
tmpMsg = (Msg)pointer.Current;
//如果该Label是符合当前文本框可以接受的Id,删除该Msg
if (tmpMsg.Label == labelId)
{
tmpMsg = me.RemoveCurrent();
//模拟接收的时间不同
Thread.Sleep(r.Next(, ));
//必须指定每一个Msg的存储信息格式
tmpMsg.Formatter = new XmlMessageFormatter(new Type[] { typeof(string) });
msgs.Add(tmpMsg);
//必须加上去,因为删除当前的Msg之后不使用“重置”,导致MoveNext为false。
me.Reset();
}
}
if (msgs.Count > )
{
result = string.Join(",", (from m in msgs
select m.Body.ToString()));
}
});
return result;
} private async void button1_Click(object sender, EventArgs e)
{
//初始化已经创建的Queue
MessageQueue msgQueue = new MessageQueue(@".\private$\myQueue");
//模拟发送Msg(1和2交替发送)
for (int i = ; i < ; i++)
{
Msg message = new Msg(i % == ? "" : "", new XmlMessageFormatter(new Type[] { typeof(string) })); //Send第一个参数:发送的消息对象;第二个参数:Label
msgQueue.Send(message, i % == ? "" : "");
}
//异步接收
var content1 = await GetMessage("");
var content2 = await GetMessage("");
textBox2.Text = content1;
textBox3.Text = content2;
}
}
}
Message Queue基本使用说明的更多相关文章
- 初识Message Queue之--基础篇
之前我在项目中要用到消息队列相关的技术时,一直让Redis兼职消息队列功能,一个偶然的机会接触到了MSMQ消息队列.秉着技术还是专业的好为原则,对MSMQ进行了学习,以下是我个人的学习笔记. 一.什么 ...
- MSMQ(Microsoft Message Queue)
http://www.cnblogs.com/sk-net/archive/2011/11/25/2232341.html 利用 MSMQ(Microsoft Message Queue),应用程序开 ...
- Message Queue vs. Web Services?
From stackoverflow.com When you use a web service you have a client and a server: If the server fail ...
- hdu 1509 Windows Message Queue
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1509 Windows Message Queue Description Message queue ...
- 为什么要用Message Queue
摘录自博客:http://dataunion.org/9307.html?utm_source=tuicool&utm_medium=referral 为什么要用Message Queue 解 ...
- 单线程模型中Message、Handler、Message Queue、Looper之间的关系
1. Android进程 在了解Android线程之前得先了解一下Android的进程.当一个程序第一次启动的时候,Android会启动一个LINUX进程和一个主线程.默认的情况下,所有该程序的组件都 ...
- Top 10 Uses of a Message Queue
Top 10 Uses of a Message QueueAsynchronicity, Work Dispatch, Load Buffering, Database Offloading, an ...
- hdoj 1509 Windows Message Queue【优先队列】
Windows Message Queue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- 详解boost库中的Message Queue .
Message Queue(后文简写成MQ或消息队列)是boost库中用来封装进程间通信的一种实现,同一台机器上的进程或线程可以通过消息队列来进行通迅.消息队列中的消息由优先级.消息长度.消息数据三部 ...
随机推荐
- MongoDB 主从复制 的设置
今天我们主要讨论mongodb的部署技术. 我们知道sql server能够做到读写分离,双机热备份和集群部署,当然mongodb也能做到,实际应用中我们不希望数据库采用单点部署, 如果碰到数据库宕机 ...
- [数学趣味001]RSA算法原理及示例
可以先看看这个视频: RSA_Encryption_Algorithm 公开密钥 Perwork: 私钥:Sender和Receiver预先约定加密和解密方案,向其他人保密. 这个实现比较难:向其他人 ...
- 萌新web前端从零开始(1)——计算机入门
前言:这是一个萌新从零开始的学习之路,与大家分享自己的看法与见解,还请指出错误与遗漏点方便改正. 1.认识计算机. 计算机语言常见的有C,PHP,Ruby,Java,C#,Basic,JS,C++等, ...
- 在.net Core中使用StackExchange.Redis 2.0
StackExchange.Redis 2.0做了大量的改进包括使用了高性能的IO库System.IO.Pipelines来提升性能以及解决Timeouts问题, 但是在.net Core2.2之前为 ...
- SQLlite 分页
如果我要去11-20的Account表的数据 Select * From Account Limit 9 Offset 10; 以上语句表示从Account表获取数据,跳过10行,取9行 嗯,我觉得这 ...
- 通过api获取句柄控制其他窗体
很多时候,编写程序模拟鼠标和键盘操作可以方便的实现你需要的功能,而不需要对方程序为你开放接口.比如,操作飞信定时发送短信等.我之前开发过飞信耗子,用的是对飞信协议进行抓包,然后分析协议,进而模拟协议的 ...
- 《Think in Java》17~18
chapter 17 容器深入研究 填充容器 package cn.test; import java.util.ArrayList; import java.util.Collections; im ...
- python3 的zip()函数
重点 https://blog.csdn.net/qq826364410/article/details/78259796 为啥会出现几个两个空列表????
- “全栈2019”Java第四十八章:重写方法Override
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- [agc016b]Colorful Hats 分类讨论
Description 有n个人,每个人都戴着一顶帽子.当然,帽子有不同的颜色. 现在,每个人都告诉你,他看到的所有其他人的帽子共有多少种颜色,请问你有没有符合所有人的描述的情况. Input ...