消息队列NetMQ使用介绍

1.前言

今天使用了下.NET平台的消息队列NetMQ,在这里记录下.博客园张善友也有较好的例子,只不过我这个例子在张兄的基础上稍微扩展了下.张兄的NetMQ介绍文章只是从客户端向服务端发送字符串,服务端收到后返回一个消息,我这里设想发送委托及参数到服务端执行,服务端返回执行状态.

2.下载NetMQ

Install-Package NetMQ
3.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
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值