消息队列NetMQ使用介绍
来源:互联网 发布:闹钟软件 编辑:程序博客网 时间:2024/05/17 22:50
1.前言
今天使用了下.NET平台的消息队列NetMQ,在这里记录下.博客园张善友也有较好的例子,只不过我这个例子在张兄的基础上稍微扩展了下.张兄的NetMQ介绍文章只是从客户端向服务端发送字符串,服务端收到后返回一个消息,我这里设想发送委托及参数到服务端执行,服务端返回执行状态.
2.下载NetMQ
Install-Package NetMQ3.Demo
1)客户端
using NetMQ;using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Runtime.Serialization;using System.Runtime.Serialization.Formatters.Binary;using System.Text;using System.Threading.Tasks;using NetMQ.Common;namespace NetMQClient{ class Program { static void Main(string[] args) { using (NetMQContext context = NetMQContext.Create()) { Client(context); } Console.ReadKey(); } static void Client(NetMQContext context) { using (NetMQSocket clientSocket = context.CreateRequestSocket()) { clientSocket.Connect("tcp://127.0.0.1:5555"); Dictionary<string,object> dic = new Dictionary<string,object>(); dic.Add("a",1); dic.Add("b",2); Message msg = new Message() { Id = 0, FunName = "Calc", Params = dic, Result = "" }; byte[] buffer = ByteConvertHelper.Object2Bytes(msg); clientSocket.SendFrame(buffer); string answer = clientSocket.ReceiveFrameString(); Console.WriteLine("Answer from server: {0}", answer); } } }}2)服务端
using NetMQ;using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Runtime.Serialization;using System.Runtime.Serialization.Formatters.Binary;using System.Text;using System.Threading.Tasks;using NetMQ.Common;namespace NetMQDemo{ class Program { static void Main(string[] args) { using (NetMQContext context = NetMQContext.Create()) { Server(context); } Console.ReadKey(); } static int Calc(int a, int b) { return a + b; } static void Server(NetMQContext context) { using (NetMQSocket serverSocket = context.CreateResponseSocket()) { serverSocket.Bind("tcp://127.0.0.1:5555"); byte[] buffer = serverSocket.ReceiveFrameBytes(); Message msg = (Message)ByteConvertHelper.Bytes2Object(buffer); msg.Result = (Calc((int)msg.Params["a"],(int)msg.Params["b"]).ToString()); Console.WriteLine("Result:" + msg.Result); serverSocket.SendFrame("Result:" + msg.Result); } } }}3).Common
3.1)对象和字节数组互相转化的Helper
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Runtime.Serialization;using System.Runtime.Serialization.Formatters.Binary;using System.Text;using System.Threading.Tasks;namespace NetMQ.Common{ public class ByteConvertHelper { /// <summary> /// 将对象转换为byte数组 /// </summary> /// <param name="obj">被转换对象</param> /// <returns>转换后byte数组</returns> public static byte[] Object2Bytes(object obj) { byte[] buff; using (MemoryStream ms = new MemoryStream()) { IFormatter iFormatter = new BinaryFormatter(); iFormatter.Serialize(ms, obj); buff = ms.GetBuffer(); } return buff; } /// <summary> /// 将byte数组转换成对象 /// </summary> /// <param name="buff">被转换byte数组</param> /// <returns>转换完成后的对象</returns> public static object Bytes2Object(byte[] buff) { object obj; using (MemoryStream ms = new MemoryStream(buff)) { IFormatter iFormatter = new BinaryFormatter(); obj = iFormatter.Deserialize(ms); } return obj; } }}3.2)消息实体
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace NetMQ.Common{ [Serializable] public class Message { public int Id { set; get; } public string FunName { set; get; } public Dictionary<string, object> Params { set; get; } public string Result { set; get; } }}注意:必须加Serializable属性
4.结果
5.总结
NetMQ目前还不支持持久化消息,所以,可靠性不是特别高.消息队列基本的特性都满足,效率也很高.
0 0
- 消息队列NetMQ使用介绍
- NetMQ 消息对列
- SquirrelMQ消息队列介绍
- kestrel消息队列介绍
- 消息队列AactiveMQ介绍
- kestrel消息队列介绍
- RabbitMQ消息队列介绍
- 消息队列-Kafka介绍
- 消息队列介绍
- 消息队列介绍
- 消息队列介绍
- 消息队列技术介绍
- 消息队列概念介绍
- 各种消息队列介绍
- 消息队列探秘-RabbitMQ消息队列介绍
- 消息队列的基本使用思路和接口介绍
- 消息队列使用的四种场景介绍
- 消息队列使用的四种场景介绍
- 学习笔记(二)——直方图均衡化和匹配
- 小结NSString 以后还会补充
- nginx机制-从源码分析(1)
- Executor和ExecutorService
- LeetCode-53-Maximum Subarray(DP/分治)-Medium
- 消息队列NetMQ使用介绍
- UNREAL FRAMEWORK & NETWORK
- 算法实现两个数交换(多种方法)
- UITextFielddelegate委托方法注释
- Python 遍历字典——学习笔记
- iOS学习之路-超级猜图
- iOS开发检测项目中哪个库包含了idfa
- vim 配置
- jsp访问WEB-INF下面的资源-jsp