123
ningshuxia
2023-04-13 eb32b4263f6901bb7ac1915b400e4fe28db20ef0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
 
namespace IStation
{
    /// <summary>
    /// 通道辅助类
    /// </summary>
    public class RabbitMqQueueHelper 
    {
        //连接工厂
        private static ConnectionFactory Factory = new ConnectionFactory()
        {
            HostName = ConfigHelper.RabbitMq_HostName,
            UserName = ConfigHelper.RabbitMq_UserName,
            Password = ConfigHelper.RabbitMq_Password
        };
 
        private IConnection _conn_receive = null;//接收连接对象
        private IModel _channel_receive = null;//接收会话
 
        /// <summary>
        /// 推送
        /// </summary>
        public bool Push<T>(string queueName, T t)
        {
            if (t == null)
                return default;
 
            try
            {
                using (var con = Factory.CreateConnection())//创建连接对象
                {
                    using (var channel = con.CreateModel())
                    {
                        channel.QueueDeclare(queueName, true, false, false, null);//定义通道
                        var properties = channel.CreateBasicProperties();
                        properties.Persistent = true;//数据持久化
                        var message = JsonHelper.Object2Json(t);
                        var body = Encoding.UTF8.GetBytes(message);
                        channel.BasicPublish("", queueName, properties, body);//插入数据
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex.Message, ex);
                return false;
            }
        }
 
        /// <summary>
        /// 接收
        /// </summary>
        public void Receive<T>(string queueName, Func<T, bool> func)
        {
            if (_conn_receive != null)
                return;
            if (_channel_receive != null)
                return;
            _conn_receive = Factory.CreateConnection();//创建连接对象
            _channel_receive = _conn_receive.CreateModel();
            _channel_receive.QueueDeclare(queueName, true, false, false, null);//定义通道
            _channel_receive.BasicQos(0, 1, false);//公平分发
            var consumer = new EventingBasicConsumer(_channel_receive);//创建消费者
            consumer.Received += (model, ea) =>
            {
                byte[] body = ea.Body.ToArray();
                var message = Encoding.UTF8.GetString(body);
                var data = JsonHelper.Json2Object<T>(message);
                var result = func(data);
                if (result)
                {
                    _channel_receive.BasicAck(ea.DeliveryTag, false);
                }
                else
                {
                    _channel_receive.BasicNack(ea.DeliveryTag, false, true);
                }
            };
           
            _channel_receive.BasicConsume(queueName, false, consumer);//开始消费
        }
 
        /// <summary>
        /// 关闭
        /// </summary>
        public void Close()
        {
            if (_channel_receive != null)
            {
                if (_channel_receive.IsOpen)
                {
                    _channel_receive.Close();
                }
            }
            if (_conn_receive != null)
            {
                if (_conn_receive.IsOpen)
                {
                    _conn_receive.Close();
                }
            }
        }
 
    }
}