4
4
5
5
use Enqueue \Client \Config ;
6
6
use Enqueue \Client \Message ;
7
- use Enqueue \Client \MessagePriority ;
8
- use Enqueue \Client \Meta \QueueMetaRegistry ;
7
+ use Enqueue \Client \RouteCollection ;
9
8
use Enqueue \Stomp \StompContext ;
10
9
use Enqueue \Stomp \StompDestination ;
11
10
use Enqueue \Stomp \StompMessage ;
11
+ use Enqueue \Stomp \StompProducer ;
12
12
use Interop \Queue \PsrMessage ;
13
+ use Interop \Queue \PsrProducer ;
13
14
use Interop \Queue \PsrQueue ;
15
+ use Interop \Queue \PsrTopic ;
14
16
use Psr \Log \LoggerInterface ;
15
17
use Psr \Log \NullLogger ;
16
18
17
19
class RabbitMqStompDriver extends StompDriver
18
20
{
19
- /**
20
- * @var StompContext
21
- */
22
- private $ context ;
23
-
24
- /**
25
- * @var Config
26
- */
27
- private $ config ;
28
-
29
- /**
30
- * @var array
31
- */
32
- private $ priorityMap ;
33
-
34
21
/**
35
22
* @var StompManagementClient
36
23
*/
37
24
private $ management ;
38
25
39
- /**
40
- * @var QueueMetaRegistry
41
- */
42
- private $ queueMetaRegistry ;
43
-
44
- public function __construct (StompContext $ context , Config $ config , QueueMetaRegistry $ queueMetaRegistry , StompManagementClient $ management )
26
+ public function __construct (StompContext $ context , Config $ config , RouteCollection $ routeCollection , StompManagementClient $ management )
45
27
{
46
- parent ::__construct ($ context , $ config , $ queueMetaRegistry );
28
+ parent ::__construct ($ context , $ config , $ routeCollection );
47
29
48
- $ this ->context = $ context ;
49
- $ this ->config = $ config ;
50
- $ this ->queueMetaRegistry = $ queueMetaRegistry ;
51
30
$ this ->management = $ management ;
52
-
53
- $ this ->priorityMap = [
54
- MessagePriority::VERY_LOW => 0 ,
55
- MessagePriority::LOW => 1 ,
56
- MessagePriority::NORMAL => 2 ,
57
- MessagePriority::HIGH => 3 ,
58
- MessagePriority::VERY_HIGH => 4 ,
59
- ];
60
31
}
61
32
62
33
/**
@@ -71,15 +42,17 @@ public function createTransportMessage(Message $message): PsrMessage
71
42
}
72
43
73
44
if ($ priority = $ message ->getPriority ()) {
74
- if (false == array_key_exists ($ priority , $ this ->priorityMap )) {
45
+ $ priorityMap = $ this ->getPriorityMap ();
46
+
47
+ if (false == array_key_exists ($ priority , $ priorityMap )) {
75
48
throw new \LogicException (sprintf ('Cant convert client priority to transport: "%s" ' , $ priority ));
76
49
}
77
50
78
- $ transportMessage ->setHeader ('priority ' , $ this -> priorityMap [$ priority ]);
51
+ $ transportMessage ->setHeader ('priority ' , $ priorityMap [$ priority ]);
79
52
}
80
53
81
54
if ($ message ->getDelay ()) {
82
- if (false == $ this ->config ->getTransportOption ('delay_plugin_installed ' , false )) {
55
+ if (false == $ this ->getConfig () ->getTransportOption ('delay_plugin_installed ' , false )) {
83
56
throw new \LogicException ('The message delaying is not supported. In order to use delay feature install RabbitMQ delay plugin. ' );
84
57
}
85
58
@@ -89,68 +62,6 @@ public function createTransportMessage(Message $message): PsrMessage
89
62
return $ transportMessage ;
90
63
}
91
64
92
- /**
93
- * @param StompMessage $message
94
- */
95
- public function createClientMessage (PsrMessage $ message ): Message
96
- {
97
- $ clientMessage = parent ::createClientMessage ($ message );
98
-
99
- $ headers = $ clientMessage ->getHeaders ();
100
- unset(
101
- $ headers ['x-delay ' ],
102
- $ headers ['expiration ' ],
103
- $ headers ['priority ' ]
104
- );
105
- $ clientMessage ->setHeaders ($ headers );
106
-
107
- if ($ delay = $ message ->getHeader ('x-delay ' )) {
108
- if (false == is_numeric ($ delay )) {
109
- throw new \LogicException (sprintf ('x-delay header is not numeric. "%s" ' , $ delay ));
110
- }
111
-
112
- $ clientMessage ->setDelay ((int ) ((int ) $ delay ) / 1000 );
113
- }
114
-
115
- if ($ expiration = $ message ->getHeader ('expiration ' )) {
116
- if (false == is_numeric ($ expiration )) {
117
- throw new \LogicException (sprintf ('expiration header is not numeric. "%s" ' , $ expiration ));
118
- }
119
-
120
- $ clientMessage ->setExpire ((int ) ((int ) $ expiration ) / 1000 );
121
- }
122
-
123
- if ($ priority = $ message ->getHeader ('priority ' )) {
124
- if (false === $ clientPriority = array_search ($ priority , $ this ->priorityMap , true )) {
125
- throw new \LogicException (sprintf ('Cant convert transport priority to client: "%s" ' , $ priority ));
126
- }
127
-
128
- $ clientMessage ->setPriority ($ clientPriority );
129
- }
130
-
131
- return $ clientMessage ;
132
- }
133
-
134
- public function sendToProcessor (Message $ message ): void
135
- {
136
- if (false == $ message ->getProperty (Config::PARAMETER_PROCESSOR_NAME )) {
137
- throw new \LogicException ('Processor name parameter is required but is not set ' );
138
- }
139
-
140
- if (false == $ queueName = $ message ->getProperty (Config::PARAMETER_PROCESSOR_QUEUE_NAME )) {
141
- throw new \LogicException ('Queue name parameter is required but is not set ' );
142
- }
143
-
144
- $ transportMessage = $ this ->createTransportMessage ($ message );
145
- $ destination = $ this ->createQueue ($ queueName );
146
-
147
- if ($ message ->getDelay ()) {
148
- $ destination = $ this ->createDelayedTopic ($ destination );
149
- }
150
-
151
- $ this ->context ->createProducer ()->send ($ destination , $ transportMessage );
152
- }
153
-
154
65
/**
155
66
* @return StompDestination
156
67
*/
@@ -169,22 +80,22 @@ public function setupBroker(LoggerInterface $logger = null): void
169
80
$ logger ->debug (sprintf ('[RabbitMqStompDriver] ' .$ text , ...$ args ));
170
81
};
171
82
172
- if (false == $ this ->config ->getTransportOption ('management_plugin_installed ' , false )) {
83
+ if (false == $ this ->getConfig () ->getTransportOption ('management_plugin_installed ' , false )) {
173
84
$ log ('Could not setup broker. The option `management_plugin_installed` is not enabled. Please enable that option and install rabbit management plugin ' );
174
85
175
86
return ;
176
87
}
177
88
178
89
// setup router
179
- $ routerExchange = $ this ->config ->createTransportRouterTopicName ($ this ->config ->getRouterTopicName ());
90
+ $ routerExchange = $ this ->getConfig () ->createTransportRouterTopicName ($ this ->getConfig () ->getRouterTopicName ());
180
91
$ log ('Declare router exchange: %s ' , $ routerExchange );
181
92
$ this ->management ->declareExchange ($ routerExchange , [
182
93
'type ' => 'fanout ' ,
183
94
'durable ' => true ,
184
95
'auto_delete ' => false ,
185
96
]);
186
97
187
- $ routerQueue = $ this ->config ->createTransportQueueName ($ this ->config ->getRouterQueueName ());
98
+ $ routerQueue = $ this ->getConfig () ->createTransportQueueName ($ this ->getConfig () ->getRouterQueueName ());
188
99
$ log ('Declare router queue: %s ' , $ routerQueue );
189
100
$ this ->management ->declareQueue ($ routerQueue , [
190
101
'auto_delete ' => false ,
@@ -198,11 +109,11 @@ public function setupBroker(LoggerInterface $logger = null): void
198
109
$ this ->management ->bind ($ routerExchange , $ routerQueue , $ routerQueue );
199
110
200
111
// setup queues
201
- foreach ($ this ->queueMetaRegistry -> getQueuesMeta () as $ meta ) {
202
- $ queue = $ this ->config -> createTransportQueueName ( $ meta -> getClientName () );
112
+ foreach ($ this ->getRouteCollection ()-> all () as $ route ) {
113
+ $ queue = $ this ->createRouteQueue ( $ route );
203
114
204
- $ log ('Declare processor queue: %s ' , $ queue );
205
- $ this ->management ->declareQueue ($ queue , [
115
+ $ log ('Declare processor queue: %s ' , $ queue-> getStompName () );
116
+ $ this ->management ->declareQueue ($ queue-> getStompName () , [
206
117
'auto_delete ' => false ,
207
118
'durable ' => true ,
208
119
'arguments ' => [
@@ -212,10 +123,10 @@ public function setupBroker(LoggerInterface $logger = null): void
212
123
}
213
124
214
125
// setup delay exchanges
215
- if ($ this ->config ->getTransportOption ('delay_plugin_installed ' , false )) {
216
- foreach ($ this ->queueMetaRegistry -> getQueuesMeta () as $ meta ) {
217
- $ queue = $ this ->config -> createTransportQueueName ( $ meta -> getClientName () );
218
- $ delayExchange = $ queue .'.delayed ' ;
126
+ if ($ this ->getConfig () ->getTransportOption ('delay_plugin_installed ' , false )) {
127
+ foreach ($ this ->getRouteCollection ()-> all () as $ route ) {
128
+ $ queue = $ this ->createRouteQueue ( $ route );
129
+ $ delayExchange = $ queue-> getStompName () .'.delayed ' ;
219
130
220
131
$ log ('Declare delay exchange: %s ' , $ delayExchange );
221
132
$ this ->management ->declareExchange ($ delayExchange , [
@@ -227,18 +138,49 @@ public function setupBroker(LoggerInterface $logger = null): void
227
138
],
228
139
]);
229
140
230
- $ log ('Bind processor queue to delay exchange: %s -> %s ' , $ queue , $ delayExchange );
231
- $ this ->management ->bind ($ delayExchange , $ queue , $ queue );
141
+ $ log ('Bind processor queue to delay exchange: %s -> %s ' , $ queue-> getStompName () , $ delayExchange );
142
+ $ this ->management ->bind ($ delayExchange , $ queue-> getStompName () , $ queue-> getStompName () );
232
143
}
233
144
} else {
234
145
$ log ('Delay exchange and bindings are not setup. if you \'d like to use delays please install delay rabbitmq plugin and set delay_plugin_installed option to true ' );
235
146
}
236
147
}
237
148
149
+ /**
150
+ * @param StompProducer $producer
151
+ * @param StompDestination $topic
152
+ * @param StompMessage $transportMessage
153
+ */
154
+ protected function doSendToRouter (PsrProducer $ producer , PsrTopic $ topic , PsrMessage $ transportMessage ): void
155
+ {
156
+ // We should not handle priority, expiration, and delay at this stage.
157
+ // The router will take care of it while re-sending the message to the final destinations.
158
+ $ transportMessage ->setHeader ('expiration ' , null );
159
+ $ transportMessage ->setHeader ('priority ' , null );
160
+ $ transportMessage ->setHeader ('x-delay ' , null );
161
+
162
+ $ producer ->send ($ topic , $ transportMessage );
163
+ }
164
+
165
+ /**
166
+ * @param StompProducer $producer
167
+ * @param StompDestination $destination
168
+ * @param StompMessage $transportMessage
169
+ */
170
+ protected function doSendToProcessor (PsrProducer $ producer , PsrQueue $ destination , PsrMessage $ transportMessage ): void
171
+ {
172
+ if ($ delay = $ transportMessage ->getProperty ('X-Enqueue-Delay ' )) {
173
+ $ producer ->setDeliveryDelay (null );
174
+ $ destination = $ this ->createDelayedTopic ($ destination );
175
+ }
176
+
177
+ $ producer ->send ($ destination , $ transportMessage );
178
+ }
179
+
238
180
private function createDelayedTopic (StompDestination $ queue ): StompDestination
239
181
{
240
182
// in order to use delay feature make sure the rabbitmq_delayed_message_exchange plugin is installed.
241
- $ destination = $ this ->context ->createTopic ($ queue ->getStompName ().'.delayed ' );
183
+ $ destination = $ this ->getContext () ->createTopic ($ queue ->getStompName ().'.delayed ' );
242
184
$ destination ->setType (StompDestination::TYPE_EXCHANGE );
243
185
$ destination ->setDurable (true );
244
186
$ destination ->setAutoDelete (false );
0 commit comments