RabbitMQ消息队列帮助类
调用
//消息队列发消息
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消息队列帮助类的更多相关文章
- RabbitMQ学习系列二:.net 环境下 C#代码使用 RabbitMQ 消息队列
一.理论: .net环境下,C#代码调用RabbitMQ消息队列,本文用easynetq开源的.net Rabbitmq api来实现. EasyNetQ 是一个易于使用的RabbitMQ的.Net客 ...
- RabbitMQ 消息队列 应用
安装参考 详细介绍 学习参考 RabbitMQ 消息队列 RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议. M ...
- (十三)RabbitMQ消息队列-VirtualHost与权限管理
原文:(十三)RabbitMQ消息队列-VirtualHost与权限管理 VirtualHost 像mysql有数据库的概念并且可以指定用户对库和表等操作的权限.那RabbitMQ呢?RabbitMQ ...
- SpringCloud之RabbitMQ消息队列原理及配置
本篇章讲解RabbitMQ的用途.原理以及配置,RabbitMQ的安装请查看SpringCloud之RabbitMQ安装 一.MQ用途 1.同步变异步消息 场景:用户下单完成后,发送邮件和短信通知. ...
- .net core使用rabbitmq消息队列 (二)
之前有写过.net core集成使用rabbitmq的博文,见.net core使用rabbitmq消息队列,但是里面的使用很简单,而且还有几个bug,想改下,但是后来想了想,还是算了,之前使用的是. ...
- .net core使用rabbitmq消息队列
看博文的朋友,本文有些过时了,还有些BUG,如果想了解更多用法,看看这篇吧:.net core使用rabbitmq消息队列 (二) 首先,如果你还没有安装好rabbitmq,可以参考我的博客: Ubu ...
- C# .net 环境下使用rabbitmq消息队列
消息队列的地位越来越重要,几乎是面试的必问问题了,不会使用几种消息队列都显得尴尬,正好本文使用C#来带你认识rabbitmq消息队列 首先,我们要安装rabbitmq,当然,如果有现成的,也可以使用, ...
- 基于ASP.NET Core 5.0使用RabbitMQ消息队列实现事件总线(EventBus)
文章阅读请前先参考看一下 https://www.cnblogs.com/hudean/p/13858285.html 安装RabbitMQ消息队列软件与了解C#中如何使用RabbitMQ 和 htt ...
- RabbitMQ消息队列(一): Detailed Introduction 详细介绍
http://blog.csdn.net/anzhsoft/article/details/19563091 RabbitMQ消息队列(一): Detailed Introduction 详细介绍 ...
随机推荐
- NoSQL 是什么
NoSQL 全称 Not only SQL ,是一种相对较新的数据库设计方式,传统的关系型数据库使用的是固定模式,并将数据分割在多个表中,然而,对于大数据集的情况,数据量太大使其难以存放在单一的服务器 ...
- BUU re xor
从13行和18行的0x21(c规定十六进制必须用0x**表示)可以知道这个字符串就是33个字符 shift+e来提取出数组中的字符: 设这个数组是global数组 global[] = { 102, ...
- C语言书籍入门---第三章
=======变量和数据类型========= 说 明:字符型 短整型 整型 长整型 单精度浮点型 双精度浮点型 无类型 数据类型:char short int long float d ...
- DeepLearning算法文章
算法源码: learn_dl : https://github.com/hanbt/learn_dl rnn-from-scratch : https://github.com/pangolulu/r ...
- 页面的五种布局以及嵌套『Android系列八』
转自:http://blog.csdn.net/dazlly/article/details/7860125 因为学习比较晚,我用的相关版本为SDK4.1.eclipse4.2,而自己看的教材都是低版 ...
- input 数值框处理
<input type="text"> input 若设置type=“number” ,再想对其调用处理的函数是不起作用的,为此,首先将其设为文本类型 当前要求是数字 ...
- SIAMATIC S7-1200 中通过 Modbus RTU 如何读取地址范围 9999 到 65535 的输入字
原文地址 说明 除了需要 STEP 7 >= V13 SP1 (TIA Portal) 的软件,还需要 S7-1200 CPU 固件版本 >= V4 (文章编号: 6ES721x-1xx4 ...
- mysql 索引优化法则
建表语句 CREATE TABLE staffs( id INT PRIMARY KEY AUTO_INCREMENT, NAME VARCHAR (24) NOT NULL DEFAULT '' C ...
- python中判断素数的函数
来看这一种判断素数(质数)的函数: form math import sart def is_prime(n): if n==1: return False for i in range(2, int ...
- arm linux 移植 jpeg
背景: host平台 :Ubuntu 16.04 arm平台 : S5P6818 jpeg :v9c arm-gcc :4.8.1 主机准备: 运行以下脚本: ## # Copyright By Sc ...