123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- using log4net;
- using Microsoft.AspNetCore.Connections;
- using RabbitMQ.Client;
- using RabbitMQ.Client.Events;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Channels;
- using System.Threading.Tasks;
- namespace Ropin.Inspection.Common.Helper
- {
- public static class RabbitMQHelper
- {
-
-
-
-
-
- public static async Task<bool> SnedRabbitMQ(RabbitMQModel model)
- {
- if (model.IsOfficial == "false")
- {
- model.QueueName = model.QueueName + "Test";
- }
- try
- {
- var factory = new ConnectionFactory()
- {
- HostName = model.HostName,
- Port = model.Port,
- UserName = model.UserName,
- VirtualHost = model.VirtualHost,
- Password = model.Password,
- };
- using (IConnection con = factory.CreateConnection())
- {
- using (IModel channel = con.CreateModel())
- {
-
- var queue = channel.QueueDeclare(
- queue: model.QueueName,
- durable: false,
- exclusive: false,
- autoDelete: false,
- arguments: null
- );
-
-
-
-
-
-
-
- channel.ConfirmSelect();
- var body = Encoding.UTF8.GetBytes(model.msgStr);
- var properties = channel.CreateBasicProperties();
- properties.Persistent = true;
- channel.BasicPublish("", model.QueueName, properties, body);
- bool bol = channel.WaitForConfirms();
- return bol;
- }
- }
- }
- catch (Exception ex)
- {
- return false;
- }
- }
-
-
-
-
-
-
-
- public static async Task<bool> SnedRabbitMQ_ExchangeDirect(RabbitMQModel model)
- {
- if (model.IsOfficial == "false")
- {
- model.QueueName = model.QueueName + "Test";
- }
- model.ExchangeName = model.QueueName + ".DirectExchange";
- model.RoutingKey = model.QueueName + ".key";
- try
- {
- var factory = new ConnectionFactory()
- {
- HostName = model.HostName,
- Port = model.Port,
- UserName = model.UserName,
- VirtualHost = model.VirtualHost,
- Password = model.Password,
- };
- using (IConnection con = factory.CreateConnection())
- {
- using (IModel channel = con.CreateModel())
- {
- channel.ExchangeDeclare(model.ExchangeName, type: ExchangeType.Direct);
-
- var queue = channel.QueueDeclare(
- queue: model.QueueName,
- durable: false,
- exclusive: false,
- autoDelete: false,
- arguments: null
- );
-
- channel.QueueBind(model.QueueName, model.ExchangeName,model.RoutingKey);
- channel.ConfirmSelect();
- var body = Encoding.UTF8.GetBytes(model.msgStr);
- var properties = channel.CreateBasicProperties();
- properties.Persistent = true;
- channel.BasicPublish(model.ExchangeName, model.RoutingKey, properties, body);
- bool bol = channel.WaitForConfirms();
- return bol;
- }
- }
- }
- catch (Exception ex)
- {
- return false;
- }
- }
-
-
-
-
-
-
-
- public static async Task<bool> SnedRabbitMQ_ExchangeTopic(RabbitMQModel model)
- {
- if (model.IsOfficial == "false")
- {
- model.QueueName = model.QueueName + "Test";
- }
- try
- {
- var factory = new ConnectionFactory()
- {
- HostName = model.HostName,
- Port = model.Port,
- UserName = model.UserName,
- VirtualHost = model.VirtualHost,
- Password = model.Password,
- };
- using (IConnection con = factory.CreateConnection())
- {
- using (IModel channel = con.CreateModel())
- {
- channel.ExchangeDeclare(model.ExchangeName, type: ExchangeType.Topic, durable: true);
-
- var queue = channel.QueueDeclare(
- queue: model.QueueName,
- durable: false,
- exclusive: false,
- autoDelete: false,
- arguments: null
- );
-
- channel.QueueBind(queue.QueueName, model.ExchangeName, model.RoutingKey);
- channel.ConfirmSelect();
- var body = Encoding.UTF8.GetBytes(model.msgStr);
- var properties = channel.CreateBasicProperties();
- properties.Persistent = true;
- properties.DeliveryMode = 2;
- channel.BasicPublish(model.ExchangeName, model.RoutingKey, properties, body);
- bool bol = channel.WaitForConfirms();
- return bol;
- }
- }
- }
- catch (Exception ex)
- {
- return false;
- }
- }
-
- }
- public class RabbitMQModel
- {
-
- public string IsOfficial { get; set; }
-
- public string msgStr { get; set; }
-
- public string QueueName { get; set; }
-
- public string HostName { get; set;}
-
- public int Port { get; set;}
-
- public string UserName { get; set; }
-
- public string Password { get; set; }
- public string VirtualHost { get; set; }
-
- public string ExchangeName { get; set; }
-
- public string RoutingKey { get; set; }
- }
- }
|