7
7
using System . Text ;
8
8
using System . Threading ;
9
9
using System . Threading . Tasks ;
10
+ using Microsoft . Extensions . DependencyInjection ;
10
11
using WorkflowCore . Interface ;
11
12
using WorkflowCore . Models ;
13
+ using WorkflowCore . QueueProviders . RabbitMQ . Interfaces ;
12
14
13
15
namespace WorkflowCore . QueueProviders . RabbitMQ . Services
14
16
{
15
17
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
16
18
public class RabbitMQProvider : IQueueProvider
17
19
{
18
- private readonly IConnectionFactory _connectionFactory ;
20
+ private readonly IRabbitMqQueueNameProvider _queueNameProvider ;
21
+ private readonly RabbitMqConnectionFactory _rabbitMqConnectionFactory ;
22
+ private readonly IServiceProvider _serviceProvider ;
23
+
19
24
private IConnection _connection = null ;
20
25
private static JsonSerializerSettings SerializerSettings = new JsonSerializerSettings ( ) { TypeNameHandling = TypeNameHandling . All } ;
21
26
22
27
public bool IsDequeueBlocking => false ;
23
28
24
- public RabbitMQProvider ( IConnectionFactory connectionFactory )
29
+ public RabbitMQProvider ( IServiceProvider serviceProvider )
25
30
{
26
- _connectionFactory = connectionFactory ;
31
+ _serviceProvider = serviceProvider ;
32
+ _queueNameProvider = _serviceProvider . GetRequiredService < IRabbitMqQueueNameProvider > ( ) ;
33
+ _rabbitMqConnectionFactory = _serviceProvider . GetRequiredService < RabbitMqConnectionFactory > ( ) ;
27
34
}
28
35
29
36
public async Task QueueWork ( string id , QueueType queue )
@@ -33,9 +40,9 @@ public async Task QueueWork(string id, QueueType queue)
33
40
34
41
using ( var channel = _connection . CreateModel ( ) )
35
42
{
36
- channel . QueueDeclare ( queue : GetQueueName ( queue ) , durable : true , exclusive : false , autoDelete : false , arguments : null ) ;
43
+ channel . QueueDeclare ( queue : _queueNameProvider . GetQueueName ( queue ) , durable : true , exclusive : false , autoDelete : false , arguments : null ) ;
37
44
var body = Encoding . UTF8 . GetBytes ( id ) ;
38
- channel . BasicPublish ( exchange : "" , routingKey : GetQueueName ( queue ) , basicProperties : null , body : body ) ;
45
+ channel . BasicPublish ( exchange : "" , routingKey : _queueNameProvider . GetQueueName ( queue ) , basicProperties : null , body : body ) ;
39
46
}
40
47
}
41
48
@@ -46,15 +53,15 @@ public async Task<string> DequeueWork(QueueType queue, CancellationToken cancell
46
53
47
54
using ( var channel = _connection . CreateModel ( ) )
48
55
{
49
- channel . QueueDeclare ( queue : GetQueueName ( queue ) ,
56
+ channel . QueueDeclare ( queue : _queueNameProvider . GetQueueName ( queue ) ,
50
57
durable : true ,
51
58
exclusive : false ,
52
59
autoDelete : false ,
53
60
arguments : null ) ;
54
61
55
62
channel . BasicQos ( prefetchSize : 0 , prefetchCount : 1 , global : false ) ;
56
63
57
- var msg = channel . BasicGet ( GetQueueName ( queue ) , false ) ;
64
+ var msg = channel . BasicGet ( _queueNameProvider . GetQueueName ( queue ) , false ) ;
58
65
if ( msg != null )
59
66
{
60
67
var data = Encoding . UTF8 . GetString ( msg . Body ) ;
@@ -76,7 +83,7 @@ public void Dispose()
76
83
77
84
public async Task Start ( )
78
85
{
79
- _connection = _connectionFactory . CreateConnection ( "Workflow-Core" ) ;
86
+ _connection = _rabbitMqConnectionFactory ( _serviceProvider , "Workflow-Core" ) ;
80
87
}
81
88
82
89
public async Task Stop ( )
@@ -88,20 +95,6 @@ public async Task Stop()
88
95
}
89
96
}
90
97
91
- private string GetQueueName ( QueueType queue )
92
- {
93
- switch ( queue )
94
- {
95
- case QueueType . Workflow :
96
- return "wfc.workflow_queue" ;
97
- case QueueType . Event :
98
- return "wfc.event_queue" ;
99
- case QueueType . Index :
100
- return "wfc.index_queue" ;
101
- }
102
- return null ;
103
- }
104
-
105
98
}
106
99
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
107
100
}
0 commit comments