调用

          //消息队列发消息
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. mysql 通过localhost可以连接IP连接不上

    因为MySQL默认没开 所以需要手动设置 开MySQL启远程连接的功能,在MySQL服务器控制台上执行以下命令: 设置权限: grant all privileges on *.* to 用户名@&q ...

  2. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表单:水平表单

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  3. java基础多线程

    线程的创建 方式1:继承Java.lang.Thread类,并覆盖run() 方法 package com.demo.Thread; public class ThreadDemo01 extends ...

  4. IDEA报 : Lombok Requires Annotation Processing

    Lombok Requires Annotation Processing Annotation processing seems to be disabled for the project &qu ...

  5. 093、Java中String类之字符串是匿名对象

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  6. MVC5仓库管理系统

    下载

  7. SciPy fftpack(傅里叶变换)

    章节 SciPy 介绍 SciPy 安装 SciPy 基础功能 SciPy 特殊函数 SciPy k均值聚类 SciPy 常量 SciPy fftpack(傅里叶变换) SciPy 积分 SciPy ...

  8. 009、MySQL取当前时间Unix时间戳,取今天Unix时间戳

    #取Unix时间戳 SELECT unix_timestamp( ) ; #取今天时间戳 SELECT unix_timestamp( curdate( ) ); 显示如下: 不忘初心,如果您认为这篇 ...

  9. 文献阅读报告 - Social BiGAT + Cycle GAN

    原文文献 Social BiGAT : Kosaraju V, Sadeghian A, Martín-Martín R, et al. Social-BiGAT: Multimodal Trajec ...

  10. Excel中列宽、行高与像素的换算公式

    DPI             Scale      ColumnWidth             RowHeight 72dpi           75%       cw=(pix-5)/6  ...