调用

          //消息队列发消息
MqConfigInfo config = new MqConfigInfo();
config.MQExChange = "DrawingOutput";
config.MQQueueName = "DrawingOutput";
config.MQRoutingKey = "DrawingOutput";
MqHelper heper = new MqHelper(config);
byte[] body = Encoding.UTF8.GetBytes("98K");//发送的内容
heper.SendMsg(body);

消息队列帮助类MqHelper

using Newtonsoft.Json;
using RabbitMQ.Client;
using RabbitMQ.Client.Content;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace LogTest
{
public class MqHelper : IDisposable
{
#region 消息队列的配置信息 public IConnection MQConnection { get; set; } public IModel MQModel { get; set; } public MqConfigInfo MqConfigInfo { get; set; } #endregion
public MqHelper(MqConfigInfo configInfo)
{
MqConfigInfo = configInfo;
var username = "guest";//用户名
//if (string.IsNullOrEmpty(username))
//{
// throw new ConfigurationErrorsException("MQHelper配置节MQUserName错误");
//}
var password = "guest";//密码
//if (string.IsNullOrEmpty(password))
//{
// throw new ConfigurationErrorsException("MQHelper配置节MQPassWord错误");
//}
var virtualhost = "mq_test";//虚拟主机名
//if (string.IsNullOrEmpty(virtualhost))
//{
// throw new ConfigurationErrorsException("MQHelper配置节MQVirtualHost错误");
//} var connectionFactory = new ConnectionFactory
{
UserName = username,
Password = password,
VirtualHost = virtualhost,
RequestedHeartbeat = ,
HostName = "192.168.1.49",//消息队列的ip
Port =
}; try
{
MQConnection = connectionFactory.CreateConnection();
MQModel = MQConnection.CreateModel();
if (MqConfigInfo.MQExChangeType != null)
{
MQModel.ExchangeDeclare(MqConfigInfo.MQExChange, MqConfigInfo.MQExChangeType);
QueueDeclareOk ok = MQModel.QueueDeclare(MqConfigInfo.MQQueueName, true, false, false, null); MQModel.QueueBind(MqConfigInfo.MQQueueName, MqConfigInfo.MQExChange, MqConfigInfo.MQRoutingKey);
}
}
catch (Exception ex)
{
throw new Exception("MQHelper创建连接失败", ex);
}
} /// <summary>
/// 发送消息
/// </summary>
/// <typeparam name="T">消息类型</typeparam>
/// <param name="message">消息主体</param>
/// <returns></returns>
public bool SendMsg(object message)
{
try
{
IMapMessageBuilder mmb = new MapMessageBuilder(MQModel);
System.Collections.Generic.IDictionary<string, object> header = mmb.Headers;
//header["Header"] =MqConfigInfo.MQHeader; string json = JsonConvert.SerializeObject(message); byte[] body = Encoding.UTF8.GetBytes(json);
if (MqConfigInfo.MQPersistModel)
{
((IBasicProperties)mmb.GetContentHeader()).DeliveryMode = ;
}
MQModel.BasicPublish(MqConfigInfo.MQExChange, MqConfigInfo.MQRoutingKey, (IBasicProperties)mmb.GetContentHeader(), body);
}
catch (Exception ex)
{
throw ex;
}
return true;
} /// <summary>
/// 发送消息
/// </summary>
/// <param name="message">消息主体</param>
/// <returns></returns>
public bool SendMsg(byte[] message)
{
try
{
IMapMessageBuilder mmb = new MapMessageBuilder(MQModel);
System.Collections.Generic.IDictionary<string, object> header = mmb.Headers;
//header["Header"] =MqConfigInfo.MQHeader;
if (MqConfigInfo.MQPersistModel)
{
((IBasicProperties)mmb.GetContentHeader()).DeliveryMode = ;
}
MQModel.BasicPublish(MqConfigInfo.MQExChange, MqConfigInfo.MQRoutingKey, (IBasicProperties)mmb.GetContentHeader(), message);
}
catch (Exception ex)
{
throw ex;
}
return true;
} /// <summary>
/// 发送消息
/// </summary>
/// <param name="message">消息主体</param>
/// <returns></returns>
public bool SendMsg(string message)
{
try
{
IMapMessageBuilder mmb = new MapMessageBuilder(MQModel);
System.Collections.Generic.IDictionary<string, object> header = mmb.Headers;
//header["Header"] =MqConfigInfo.MQHeader;
byte[] body = Encoding.UTF8.GetBytes(message);
if (MqConfigInfo.MQPersistModel)
{
((IBasicProperties)mmb.GetContentHeader()).DeliveryMode = ;
}
MQModel.BasicPublish(MqConfigInfo.MQExChange, MqConfigInfo.MQRoutingKey, (IBasicProperties)mmb.GetContentHeader(), body);
}
catch (Exception ex)
{
throw ex;
}
return true;
} public void Dispose()
{
if (MQModel != null)
{
MQModel.Dispose();
} if (MQConnection != null)
{
MQConnection.Dispose();
}
}
} /// <summary>
/// 消息队列配置信息
/// </summary>
public class MqConfigInfo
{
public MqConfigInfo()
{
MQExChangeType = "direct";
MQPersistModel = true;
} /// <summary>
/// 交换机
/// </summary>
public string MQExChange { get; set; } /// <summary>
/// 交换机类型(fanout,direct,topic, headers)默认direct
/// </summary>
public string MQExChangeType { get; set; } /// <summary>
/// 路由Key
/// </summary>
public string MQRoutingKey { get; set; } /// <summary>
/// 消息头
/// </summary>
public string MQHeader { get; set; } /// <summary>
/// 消息的持久化
/// </summary>
public bool MQPersistModel { get; set; } /// <summary>
/// 队列名称
/// </summary>
public string MQQueueName { get; set; }
}
}

RabbitMQ消息队列帮助类的更多相关文章

  1. RabbitMQ学习系列二:.net 环境下 C#代码使用 RabbitMQ 消息队列

    一.理论: .net环境下,C#代码调用RabbitMQ消息队列,本文用easynetq开源的.net Rabbitmq api来实现. EasyNetQ 是一个易于使用的RabbitMQ的.Net客 ...

  2. RabbitMQ 消息队列 应用

    安装参考    详细介绍   学习参考 RabbitMQ 消息队列 RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议. M ...

  3. (十三)RabbitMQ消息队列-VirtualHost与权限管理

    原文:(十三)RabbitMQ消息队列-VirtualHost与权限管理 VirtualHost 像mysql有数据库的概念并且可以指定用户对库和表等操作的权限.那RabbitMQ呢?RabbitMQ ...

  4. SpringCloud之RabbitMQ消息队列原理及配置

    本篇章讲解RabbitMQ的用途.原理以及配置,RabbitMQ的安装请查看SpringCloud之RabbitMQ安装 一.MQ用途 1.同步变异步消息 场景:用户下单完成后,发送邮件和短信通知. ...

  5. .net core使用rabbitmq消息队列 (二)

    之前有写过.net core集成使用rabbitmq的博文,见.net core使用rabbitmq消息队列,但是里面的使用很简单,而且还有几个bug,想改下,但是后来想了想,还是算了,之前使用的是. ...

  6. .net core使用rabbitmq消息队列

    看博文的朋友,本文有些过时了,还有些BUG,如果想了解更多用法,看看这篇吧:.net core使用rabbitmq消息队列 (二) 首先,如果你还没有安装好rabbitmq,可以参考我的博客: Ubu ...

  7. C# .net 环境下使用rabbitmq消息队列

    消息队列的地位越来越重要,几乎是面试的必问问题了,不会使用几种消息队列都显得尴尬,正好本文使用C#来带你认识rabbitmq消息队列 首先,我们要安装rabbitmq,当然,如果有现成的,也可以使用, ...

  8. 基于ASP.NET Core 5.0使用RabbitMQ消息队列实现事件总线(EventBus)

    文章阅读请前先参考看一下 https://www.cnblogs.com/hudean/p/13858285.html 安装RabbitMQ消息队列软件与了解C#中如何使用RabbitMQ 和 htt ...

  9. RabbitMQ消息队列(一): Detailed Introduction 详细介绍

     http://blog.csdn.net/anzhsoft/article/details/19563091 RabbitMQ消息队列(一): Detailed Introduction 详细介绍 ...

随机推荐

  1. Lesson 11 How to grow old

    What, accoroding to the author is the best way to overcome the fear of death as you get older? Some ...

  2. redis之Set(无序)类型常用方法总结

    redis之Set(无序)类型常用方法总结 存--sadd key member [member ...] 取--SMEMBERS key sadd key member [member ...] 向 ...

  3. routes 学习

    对于routes的学习,感觉还是看官方文档理解的比较快,主要说明connect和resource Setting up routes¶ It is assumed that you are using ...

  4. Linux--add the PPA to your system

    Add a PPA to your system with a single line in your terminal step1:on the PPA's overview page,look f ...

  5. Python 动态从文件中导入类或函数的方法

    假设模块文件名是data_used_to_test.py,放在tests文件夹下 文件夹结构如下: project |-tests |-data_used_to_test.py 文件内包含一个test ...

  6. 有时间会做系列一(Dice)

    题目大意:给n个骰子的每面安排点数,第i个骰子有a[i]面,a[i]和为m,点数范围从1到m,不重复.问怎么安排掷骰子的点数和的期望最大.输出期望和每个骰子的点数(按骰子输入顺序). 思路: EX=x ...

  7. 吴裕雄--天生自然JAVAIO操作学习笔记:字符编码与对象序列化

    public class CharSetDemo01{ public static void main(String args[]){ System.out.println("系统默认编码: ...

  8. greenplum 导入数据方式

    参考: http://gpdb.docs.pivotal.io/4390/admin_guide/load/topics/g-working-with-file-based-ext-tables.ht ...

  9. flask-Bootstrap Jinja2 原生 模板 和 jumpserver 模板

    #模板 {% block doc -%} <!DOCTYPE html> <html{% block html_attribs %}{% endblock html_attribs ...

  10. msf生成软件

    生成软件后门程序的命令 win7获取 生成内网软件 启动kali上的http服务 systemctl start apache2 开始msf控制台 msfconsole 使用监听模块 并查看配置  s ...