Skip to content

Commit 1ef9798

Browse files
committed
可设置队列名前缀
1 parent dff7d2b commit 1ef9798

5 files changed

Lines changed: 33 additions & 92 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace RabbitMQ.EventBus.AspNetCore.Configurations
2+
{
3+
/// <summary>
4+
/// 队列名前缀
5+
/// </summary>
6+
public enum QueuePrefixType
7+
{
8+
/// <summary>
9+
/// 交换机名
10+
/// </summary>
11+
ExchangeName,
12+
/// <summary>
13+
///
14+
/// </summary>
15+
ClientProvidedName
16+
}
17+
}

src/RabbitMQ.EventBus.AspNetCore/Configurations/RabbitMQEventBusConnectionConfiguration.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ public sealed class RabbitMQEventBusConnectionConfiguration
3333
/// </summary>
3434
public LogLevel Level { get; set; }
3535
/// <summary>
36+
/// 队列名前缀(默认交换机名)
37+
/// </summary>
38+
public QueuePrefixType Prefix { get; set; }
39+
/// <summary>
3640
///
3741
/// </summary>
3842
public RabbitMQEventBusConnectionConfiguration()

src/RabbitMQ.EventBus.AspNetCore/Configurations/RabbitMQEventBusConnectionConfigurationBuild.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,13 @@ public void LoggingWriteLevel(LogLevel level)
5454
{
5555
Configuration.Level = level;
5656
}
57+
/// <summary>
58+
/// 队列名前缀
59+
/// </summary>
60+
/// <param name="queuePrefix"><see cref="QueuePrefixType"/></param>
61+
public void QueuePrefix(QueuePrefixType queuePrefix = QueuePrefixType.ExchangeName)
62+
{
63+
Configuration.Prefix = queuePrefix;
64+
}
5765
}
5866
}

src/RabbitMQ.EventBus.AspNetCore/DefaultRabbitMQEventBus.cs

Lines changed: 4 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using RabbitMQ.Client;
55
using RabbitMQ.Client.Events;
66
using RabbitMQ.EventBus.AspNetCore.Attributes;
7+
using RabbitMQ.EventBus.AspNetCore.Configurations;
78
using RabbitMQ.EventBus.AspNetCore.Events;
89
using RabbitMQ.EventBus.AspNetCore.Factories;
910
using RabbitMQ.EventBus.AspNetCore.Modules;
@@ -68,68 +69,6 @@ public void Publish<TMessage>(TMessage message, string exchange, string routingK
6869
_logger.WriteLog(_persistentConnection.Configuration.Level, $"{DateTimeOffset.Now.ToString("yyyy-MM-dd HH:mm:ss")}\t{exchange}\t{routingKey}\t{body}");
6970
_eventHandlerFactory?.PubliushEvent(new EventBusArgs(_persistentConnection.Endpoint, exchange, "", routingKey, type, _persistentConnection.Configuration.ClientProvidedName, body, true));
7071
}
71-
//public void Subscribe<TEvent, THandler>(string type = ExchangeType.Topic)
72-
// where TEvent : IEvent
73-
// where THandler : IEventHandler<TEvent>
74-
//{
75-
// //Subscribe(typeof(TEvent), typeof(THandler));
76-
// #region MyRegion
77-
// /*object attribute = typeof(TEvent).GetCustomAttributes(typeof(EventBusAttribute), true).FirstOrDefault();
78-
// if (attribute is EventBusAttribute attr)
79-
// {
80-
// string queue = attr.Queue ?? $"{ attr.Exchange }.{ typeof(TEvent).Name }";
81-
// if (!_persistentConnection.IsConnected)
82-
// {
83-
// _persistentConnection.TryConnect();
84-
// }
85-
// IModel channel;
86-
// #region snippet
87-
// try
88-
// {
89-
// channel = _persistentConnection.ExchangeDeclare(exchange: attr.Exchange, type: type);
90-
// channel.QueueDeclarePassive(queue);
91-
// }
92-
// catch
93-
// {
94-
// channel = _persistentConnection.ExchangeDeclare(exchange: attr.Exchange, type: type);
95-
// channel.QueueDeclare(queue: queue,
96-
// durable: true,
97-
// exclusive: false,
98-
// autoDelete: false,
99-
// arguments: null);
100-
// }
101-
// #endregion
102-
// channel.QueueBind(queue, attr.Exchange, attr.RoutingKey, null);
103-
// channel.BasicQos(0, 1, false);
104-
// EventingBasicConsumer consumer = new EventingBasicConsumer(channel);
105-
// consumer.Received += async (model, ea) =>
106-
// {
107-
// string body = Encoding.UTF8.GetString(ea.Body);
108-
// bool isAck = false;
109-
// try
110-
// {
111-
// await ProcessEvent<TEvent, THandler>(body);
112-
// channel.BasicAck(ea.DeliveryTag, multiple: false);
113-
// isAck = true;
114-
// }
115-
// catch (Exception ex)
116-
// {
117-
// _logger.LogError(new EventId(ex.HResult), ex, ex.Message);
118-
// }
119-
// finally
120-
// {
121-
// _logger.Information($"RabbitMQEventBus\t{DateTimeOffset.Now.ToString("yyyy-MM-dd HH:mm:ss")}\t{isAck}\t{ea.Exchange}\t{ea.RoutingKey}\t{body}");
122-
// }
123-
// };
124-
// channel.CallbackException += (sender, ex) =>
125-
// {
126-
127-
// };
128-
// channel.BasicConsume(queue: queue, autoAck: false, consumer: consumer);
129-
// }*/
130-
// #endregion
131-
//}
132-
13372
public void Subscribe(Type eventType, string type = ExchangeType.Topic)
13473
{
13574
var attributes = eventType.GetCustomAttributes(typeof(EventBusAttribute), true);
@@ -138,7 +77,7 @@ public void Subscribe(Type eventType, string type = ExchangeType.Topic)
13877
{
13978
if (attribute is EventBusAttribute attr)
14079
{
141-
string queue = attr.Queue ?? $"{ attr.Exchange }.{ eventType.Name }";
80+
string queue = attr.Queue ?? (_persistentConnection.Configuration.Prefix == QueuePrefixType.ExchangeName ? $"{ attr.Exchange }.{ eventType.Name }" : _persistentConnection.Configuration.ClientProvidedName);
14281
if (!_persistentConnection.IsConnected)
14382
{
14483
_persistentConnection.TryConnect();
@@ -152,8 +91,9 @@ public void Subscribe(Type eventType, string type = ExchangeType.Topic)
15291
}
15392
catch
15493
{
94+
15595
channel = _persistentConnection.ExchangeDeclare(exchange: attr.Exchange, type: type);
156-
channel.QueueDeclare(queue: queue,
96+
channel.QueueDeclare(queue: queue,//_persistentConnection.Configuration.ClientProvidedName
15797
durable: true,
15898
exclusive: false,
15999
autoDelete: false,
@@ -199,24 +139,6 @@ public void Subscribe(Type eventType, string type = ExchangeType.Topic)
199139
/// <summary>
200140
///
201141
/// </summary>
202-
/// <typeparam name="TEvent"></typeparam>
203-
/// <typeparam name="TEventHandle"></typeparam>
204-
/// <param name="body"></param>
205-
/// <returns></returns>
206-
//private async Task ProcessEvent<TEvent, TEventHandle>(string body)
207-
// where TEvent : IEvent
208-
// where TEventHandle : IEventHandler<TEvent>
209-
//{
210-
// using (var scope = _serviceProvider.CreateScope())
211-
// {
212-
// TEventHandle eventHandler = scope.ServiceProvider.GetRequiredService<TEventHandle>();
213-
// TEvent integrationEvent = JsonConvert.DeserializeObject<TEvent>(body);
214-
// await eventHandler.Handle(integrationEvent/*, new MessageEventArgs(body, false)*/);
215-
// }
216-
//}
217-
/// <summary>
218-
///
219-
/// </summary>
220142
/// <param name="body"></param>
221143
/// <param name="eventType"></param>
222144
/// <param name="eventHandleType"></param>

src/RabbitMQ.EventBus.AspNetCore/IRabbitMQEventBus.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using RabbitMQ.Client;
2-
using RabbitMQ.EventBus.AspNetCore.Events;
32
using System;
43

54
namespace RabbitMQ.EventBus.AspNetCore
@@ -22,15 +21,6 @@ public interface IRabbitMQEventBus
2221
/// <summary>
2322
/// 订阅消息
2423
/// </summary>
25-
/// <typeparam name="TEvent">消息体</typeparam>
26-
/// <typeparam name="THandler">消息处理</typeparam>
27-
/// <param name="type">消息类型</param>
28-
//void Subscribe<TEvent, THandler>(string type = ExchangeType.Topic)
29-
// where TEvent : IEvent
30-
// where THandler : IEventHandler<TEvent>;
31-
/// <summary>
32-
/// 订阅消息
33-
/// </summary>
3424
/// <param name="eventType">消息体</param>
3525
/// <param name="type">消息类型</param>
3626
void Subscribe(Type eventType, string type = ExchangeType.Topic);

0 commit comments

Comments
 (0)