123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- using Newtonsoft.Json;
- using RabbitMQ.Client;
- using RabbitMQ.Client.Events;
- using System;
- using System.Text;
- namespace MySystem
- {
- class ConfigModel
- {
- }
- public enum ExchangeTypeEnum
- {
-
-
-
-
- fanout = 1,
-
-
-
-
-
- direct = 2,
-
-
-
-
-
- topic = 3,
- header = 4
- }
-
-
-
- public enum ProcessingResultsEnum
- {
-
-
-
- Accept,
-
-
-
- Retry,
-
-
-
- Reject,
- }
-
-
-
- public class RabbitMqConfigModel
- {
- #region host
-
-
-
- public string IP { get; set; }
-
-
-
- public int Port { get; set; }
-
-
-
- public string UserName { get; set; }
-
-
-
- public string Password { get; set; }
-
-
-
- public string VirtualHost { get; set; }
- #endregion
- #region Queue
-
-
-
- public string QueueName { get; set; }
-
-
-
- public bool DurableQueue { get; set; }
- #endregion
- #region exchange
-
-
-
- public string ExchangeName { get; set; }
-
-
-
- public ExchangeTypeEnum ExchangeType { get; set; }
-
-
-
- public string RoutingKey { get; set; }
- #endregion
- #region message
-
-
-
- public bool DurableMessage { get; set; }
- #endregion
- }
-
-
-
- public class BaseService
- {
- public static IConnection _connection;
-
-
-
- public RabbitMqConfigModel RabbitConfig { get; set; }
- #region 构造函数
-
-
-
-
- public BaseService(RabbitMqConfigModel config)
- {
- try
- {
- RabbitConfig = config;
- CreateConn();
- }
- catch (Exception)
- {
- throw;
- }
- }
- #endregion
- #region 方法
- #region 初始化
-
-
-
- public void CreateConn()
- {
- ConnectionFactory cf = new ConnectionFactory();
- cf.Port = RabbitConfig.Port;
- cf.Endpoint = new AmqpTcpEndpoint(new Uri("amqp://" + RabbitConfig.IP + "/"));
- cf.UserName = RabbitConfig.UserName;
- cf.Password = RabbitConfig.Password;
- cf.VirtualHost = RabbitConfig.VirtualHost;
- cf.RequestedHeartbeat = TimeSpan.Parse("60");
- _connection = cf.CreateConnection();
- }
- #endregion
- #region 发送消息
-
-
-
-
-
-
- public bool Send<T>(T messageInfo, ref string errMsg)
- {
- if (messageInfo == null)
- {
- errMsg = "消息对象不能为空";
- return false;
- }
- string value = JsonConvert.SerializeObject(messageInfo);
- return Send(value, ref errMsg);
- }
-
-
-
-
-
-
- public bool Send(string message)
- {
- if (string.IsNullOrEmpty(message))
- {
- return false;
- }
- try
- {
- if (!_connection.IsOpen)
- {
- CreateConn();
- }
- using (var channel = _connection.CreateModel())
- {
-
- byte[] bytes = Encoding.UTF8.GetBytes(message);
- IBasicProperties properties = channel.CreateBasicProperties();
- properties.DeliveryMode = Convert.ToByte(RabbitConfig.DurableMessage ? 2 : 1);
- if (string.IsNullOrEmpty(RabbitConfig.ExchangeName))
- {
-
- channel.ExchangeDeclare(RabbitConfig.ExchangeName, RabbitConfig.ExchangeType.ToString(), RabbitConfig.DurableMessage, false, null);
- channel.BasicPublish("", RabbitConfig.QueueName, properties, bytes);
- }
- else
- {
-
- channel.QueueDeclare(RabbitConfig.QueueName, RabbitConfig.DurableQueue, false, false, null);
- channel.BasicPublish(RabbitConfig.ExchangeName, RabbitConfig.RoutingKey, properties, bytes);
- }
- return true;
- }
- }
- catch (Exception ex)
- {
- Library.function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "发送MQ队列消息异常");
- return false;
- }
- }
- #endregion
- }
- public class RabbitBasicService : BaseService
- {
-
-
-
-
- public RabbitBasicService(RabbitMqConfigModel config)
- : base(config)
- { }
-
-
-
-
-
- public void Receive()
- {
- try
- {
- using (var channel = _connection.CreateModel())
- {
-
- channel.QueueDeclare(RabbitConfig.QueueName, RabbitConfig.DurableQueue, false, false, null);
-
- if (!string.IsNullOrEmpty(RabbitConfig.ExchangeName))
- {
-
- channel.ExchangeDeclare(RabbitConfig.ExchangeName, RabbitConfig.ExchangeType.ToString(), RabbitConfig.DurableQueue);
-
- channel.QueueBind(RabbitConfig.QueueName, RabbitConfig.ExchangeName, RabbitConfig.RoutingKey);
- }
-
- channel.BasicQos(0, 1, false);
-
-
- var customer = new EventingBasicConsumer(channel);
- EventingBasicConsumer consumer = new EventingBasicConsumer(channel);
- consumer.Received += (a, e) =>
- {
- string MsgContent = Encoding.Default.GetString(e.Body.ToArray());
- if (RabbitConfig.QueueName == "SetRedisDataList")
- {
-
- }
- channel.BasicAck(e.DeliveryTag, true);
- };
-
- channel.BasicConsume(RabbitConfig.QueueName, false, customer);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- }
- catch
- {
- }
- }
- }
- #endregion
- }
|