1
1
using System ;
2
2
using System . Text ;
3
3
using System . Threading ;
4
+ using System . Threading . Channels ;
4
5
using System . Threading . Tasks ;
5
6
using Newtonsoft . Json ;
6
7
using RabbitMQ . Client ;
@@ -15,7 +16,6 @@ namespace Resgrid.Providers.Bus.Rabbit
15
16
{
16
17
public class RabbitInboundEventProvider : IRabbitInboundEventProvider
17
18
{
18
- //private ConnectionFactory _factory;
19
19
private IConnection _connection ;
20
20
private IModel _channel ;
21
21
@@ -28,25 +28,25 @@ public class RabbitInboundEventProvider : IRabbitInboundEventProvider
28
28
public Func < int , PersonnelLocationUpdatedEvent , Task > PersonnelLocationUpdated ;
29
29
public Func < int , UnitLocationUpdatedEvent , Task > UnitLocationUpdated ;
30
30
31
- public async Task Start ( )
31
+ public async Task Start ( string clientName , string queueName )
32
32
{
33
- VerifyAndCreateClients ( ) ;
34
- await StartMonitoring ( ) ;
33
+ VerifyAndCreateClients ( clientName ) ;
34
+ await StartMonitoring ( queueName ) ;
35
35
}
36
36
37
- private void VerifyAndCreateClients ( )
37
+ private void VerifyAndCreateClients ( string clientName )
38
38
{
39
39
try
40
40
{
41
- _connection = RabbitConnection . CreateConnection ( ) ;
41
+ _connection = RabbitConnection . CreateConnection ( clientName ) ;
42
42
43
43
if ( _connection != null )
44
44
{
45
45
_channel = _connection . CreateModel ( ) ;
46
46
47
47
if ( _channel != null )
48
48
{
49
- _channel . ExchangeDeclare ( SetQueueNameForEnv ( Topics . EventingTopic ) , "fanout" ) ;
49
+ _channel . ExchangeDeclare ( RabbitConnection . SetQueueNameForEnv ( Topics . EventingTopic ) , "fanout" ) ;
50
50
}
51
51
}
52
52
}
@@ -56,69 +56,69 @@ private void VerifyAndCreateClients()
56
56
}
57
57
}
58
58
59
- private async Task StartMonitoring ( )
59
+ private async Task StartMonitoring ( string queueName )
60
60
{
61
- if ( SystemBehaviorConfig . ServiceBusType == ServiceBusTypes . Rabbit )
62
- {
63
- var queueName = _channel . QueueDeclare ( ) . QueueName ;
61
+ //var queueName = _channel.QueueDeclare().QueueName;
64
62
65
- _channel . QueueBind ( queue : queueName ,
66
- exchange : SetQueueNameForEnv ( Topics . EventingTopic ) ,
67
- routingKey : "" ) ;
63
+ var queue = _channel . QueueDeclare ( RabbitConnection . SetQueueNameForEnv ( queueName ) , durable : true ,
64
+ autoDelete : false , exclusive : false ) ;
68
65
69
- var consumer = new EventingBasicConsumer ( _channel ) ;
70
- consumer . Received += async ( model , ea ) =>
71
- {
72
- var body = ea . Body . ToArray ( ) ;
73
- var message = Encoding . UTF8 . GetString ( body ) ;
66
+ _channel . QueueBind ( queue : queue . QueueName ,
67
+ exchange : RabbitConnection . SetQueueNameForEnv ( Topics . EventingTopic ) ,
68
+ routingKey : "" ) ;
69
+
70
+ var consumer = new EventingBasicConsumer ( _channel ) ;
71
+ consumer . Received += async ( model , ea ) =>
72
+ {
73
+ var body = ea . Body . ToArray ( ) ;
74
+ var message = Encoding . UTF8 . GetString ( body ) ;
74
75
75
- var eventingMessage = JsonConvert . DeserializeObject < EventingMessage > ( message ) ;
76
+ var eventingMessage = JsonConvert . DeserializeObject < EventingMessage > ( message ) ;
76
77
77
- if ( eventingMessage != null )
78
+ if ( eventingMessage != null )
79
+ {
80
+ switch ( ( EventingTypes ) eventingMessage . Type )
78
81
{
79
- switch ( ( EventingTypes ) eventingMessage . Type )
80
- {
81
- case EventingTypes . PersonnelStatusUpdated :
82
- if ( ProcessPersonnelStatusChanged != null )
83
- await ProcessPersonnelStatusChanged ( eventingMessage . DepartmentId , eventingMessage . ItemId ) ;
84
- break ;
85
- case EventingTypes . UnitStatusUpdated :
86
- if ( ProcessUnitStatusChanged != null )
87
- await ProcessUnitStatusChanged . Invoke ( eventingMessage . DepartmentId , eventingMessage . ItemId ) ;
88
- break ;
89
- case EventingTypes . CallsUpdated :
90
- if ( ProcessCallStatusChanged != null )
91
- await ProcessCallStatusChanged . Invoke ( eventingMessage . DepartmentId , eventingMessage . ItemId ) ;
92
- break ;
93
- case EventingTypes . CallAdded :
94
- if ( ProcessCallStatusChanged != null )
95
- await ProcessCallStatusChanged . Invoke ( eventingMessage . DepartmentId , eventingMessage . ItemId ) ;
96
- break ;
97
- case EventingTypes . CallClosed :
98
- if ( ProcessCallStatusChanged != null )
99
- await ProcessCallStatusChanged . Invoke ( eventingMessage . DepartmentId , eventingMessage . ItemId ) ;
100
- break ;
101
- case EventingTypes . PersonnelStaffingUpdated :
102
- if ( ProcessPersonnelStaffingChanged != null )
103
- await ProcessPersonnelStaffingChanged . Invoke ( eventingMessage . DepartmentId , eventingMessage . ItemId ) ;
104
- break ;
105
- case EventingTypes . PersonnelLocationUpdated :
106
- if ( PersonnelLocationUpdated != null )
107
- await PersonnelLocationUpdated . Invoke ( eventingMessage . DepartmentId , JsonConvert . DeserializeObject < PersonnelLocationUpdatedEvent > ( eventingMessage . Payload ) ) ;
108
- break ;
109
- case EventingTypes . UnitLocationUpdated :
110
- if ( UnitLocationUpdated != null )
111
- await UnitLocationUpdated . Invoke ( eventingMessage . DepartmentId , JsonConvert . DeserializeObject < UnitLocationUpdatedEvent > ( eventingMessage . Payload ) ) ;
112
- break ;
113
- default :
114
- throw new ArgumentOutOfRangeException ( ) ;
115
- }
82
+ case EventingTypes . PersonnelStatusUpdated :
83
+ if ( ProcessPersonnelStatusChanged != null )
84
+ await ProcessPersonnelStatusChanged ( eventingMessage . DepartmentId , eventingMessage . ItemId ) ;
85
+ break ;
86
+ case EventingTypes . UnitStatusUpdated :
87
+ if ( ProcessUnitStatusChanged != null )
88
+ await ProcessUnitStatusChanged . Invoke ( eventingMessage . DepartmentId , eventingMessage . ItemId ) ;
89
+ break ;
90
+ case EventingTypes . CallsUpdated :
91
+ if ( ProcessCallStatusChanged != null )
92
+ await ProcessCallStatusChanged . Invoke ( eventingMessage . DepartmentId , eventingMessage . ItemId ) ;
93
+ break ;
94
+ case EventingTypes . CallAdded :
95
+ if ( ProcessCallAdded != null )
96
+ await ProcessCallAdded . Invoke ( eventingMessage . DepartmentId , eventingMessage . ItemId ) ;
97
+ break ;
98
+ case EventingTypes . CallClosed :
99
+ if ( ProcessCallClosed != null )
100
+ await ProcessCallClosed . Invoke ( eventingMessage . DepartmentId , eventingMessage . ItemId ) ;
101
+ break ;
102
+ case EventingTypes . PersonnelStaffingUpdated :
103
+ if ( ProcessPersonnelStaffingChanged != null )
104
+ await ProcessPersonnelStaffingChanged . Invoke ( eventingMessage . DepartmentId , eventingMessage . ItemId ) ;
105
+ break ;
106
+ case EventingTypes . PersonnelLocationUpdated :
107
+ if ( PersonnelLocationUpdated != null )
108
+ await PersonnelLocationUpdated . Invoke ( eventingMessage . DepartmentId , JsonConvert . DeserializeObject < PersonnelLocationUpdatedEvent > ( eventingMessage . Payload ) ) ;
109
+ break ;
110
+ case EventingTypes . UnitLocationUpdated :
111
+ if ( UnitLocationUpdated != null )
112
+ await UnitLocationUpdated . Invoke ( eventingMessage . DepartmentId , JsonConvert . DeserializeObject < UnitLocationUpdatedEvent > ( eventingMessage . Payload ) ) ;
113
+ break ;
114
+ default :
115
+ throw new ArgumentOutOfRangeException ( ) ;
116
116
}
117
- } ;
118
- _channel . BasicConsume ( queue : queueName ,
119
- autoAck : true ,
120
- consumer : consumer ) ;
121
- }
117
+ }
118
+ } ;
119
+ _channel . BasicConsume ( queue : queue . QueueName ,
120
+ autoAck : true ,
121
+ consumer : consumer ) ;
122
122
}
123
123
124
124
public bool IsConnected ( )
@@ -147,17 +147,5 @@ public void RegisterForEvents(Func<int, string, Task> personnelStatusChanged,
147
147
PersonnelLocationUpdated = personnelLocationUpdated ;
148
148
UnitLocationUpdated = unitLocationUpdated ;
149
149
}
150
-
151
- private static string SetQueueNameForEnv ( string cacheKey )
152
- {
153
- if ( Config . SystemBehaviorConfig . Environment == SystemEnvironment . Dev )
154
- return $ "DEV{ cacheKey } ";
155
- else if ( Config . SystemBehaviorConfig . Environment == SystemEnvironment . QA )
156
- return $ "QA{ cacheKey } ";
157
- else if ( Config . SystemBehaviorConfig . Environment == SystemEnvironment . Staging )
158
- return $ "ST{ cacheKey } ";
159
-
160
- return cacheKey ;
161
- }
162
150
}
163
151
}
0 commit comments