3
3
namespace Enqueue \AmqpBunny ;
4
4
5
5
use Bunny \Channel ;
6
- use Bunny \Client ;
7
6
use Bunny \Message ;
8
7
use Interop \Amqp \AmqpConsumer as InteropAmqpConsumer ;
9
8
use Interop \Amqp \AmqpMessage as InteropAmqpMessage ;
10
9
use Interop \Amqp \AmqpQueue as InteropAmqpQueue ;
11
- use Interop \Amqp \Impl \AmqpMessage ;
12
- use Interop \Queue \Exception ;
13
10
use Interop \Queue \InvalidMessageException ;
14
11
use Interop \Queue \PsrMessage ;
15
12
16
13
class AmqpConsumer implements InteropAmqpConsumer
17
14
{
15
+ /**
16
+ * @var AmqpContext
17
+ */
18
+ private $ context ;
19
+
18
20
/**
19
21
* @var Channel
20
22
*/
@@ -30,11 +32,6 @@ class AmqpConsumer implements InteropAmqpConsumer
30
32
*/
31
33
private $ buffer ;
32
34
33
- /**
34
- * @var bool
35
- */
36
- private $ isInit ;
37
-
38
35
/**
39
36
* @var string
40
37
*/
@@ -51,36 +48,26 @@ class AmqpConsumer implements InteropAmqpConsumer
51
48
private $ consumerTag ;
52
49
53
50
/**
54
- * @var Message
55
- */
56
- private $ bunnyMessages = [];
57
-
58
- /**
59
- * @param Channel $channel
51
+ * @param AmqpContext $context
60
52
* @param InteropAmqpQueue $queue
61
53
* @param Buffer $buffer
62
54
* @param string $receiveMethod
63
55
*/
64
- public function __construct (Channel $ channel , InteropAmqpQueue $ queue , Buffer $ buffer , $ receiveMethod )
56
+ public function __construct (AmqpContext $ context , InteropAmqpQueue $ queue , Buffer $ buffer , $ receiveMethod )
65
57
{
66
- $ this ->channel = $ channel ;
58
+ $ this ->context = $ context ;
59
+ $ this ->channel = $ context ->getBunnyChannel ();
67
60
$ this ->queue = $ queue ;
68
61
$ this ->buffer = $ buffer ;
69
62
$ this ->receiveMethod = $ receiveMethod ;
70
63
$ this ->flags = self ::FLAG_NOPARAM ;
71
-
72
- $ this ->isInit = false ;
73
64
}
74
65
75
66
/**
76
67
* {@inheritdoc}
77
68
*/
78
69
public function setConsumerTag ($ consumerTag )
79
70
{
80
- if ($ this ->isInit ) {
81
- throw new Exception ('Consumer tag is not mutable after it has been subscribed to broker ' );
82
- }
83
-
84
71
$ this ->consumerTag = $ consumerTag ;
85
72
}
86
73
@@ -154,9 +141,7 @@ public function receive($timeout = 0)
154
141
public function receiveNoWait ()
155
142
{
156
143
if ($ message = $ this ->channel ->get ($ this ->queue ->getQueueName (), (bool ) ($ this ->getFlags () & InteropAmqpConsumer::FLAG_NOACK ))) {
157
- $ this ->bunnyMessages [$ message ->deliveryTag ] = $ message ;
158
-
159
- return $ this ->convertMessage ($ message );
144
+ return $ this ->context ->convertMessage ($ message );
160
145
}
161
146
}
162
147
@@ -167,11 +152,8 @@ public function acknowledge(PsrMessage $message)
167
152
{
168
153
InvalidMessageException::assertMessageInstanceOf ($ message , InteropAmqpMessage::class);
169
154
170
- if (isset ($ this ->bunnyMessages [$ message ->getDeliveryTag ()])) {
171
- $ this ->channel ->ack ($ this ->bunnyMessages [$ message ->getDeliveryTag ()]);
172
-
173
- unset($ this ->bunnyMessages [$ message ->getDeliveryTag ()]);
174
- }
155
+ $ bunnyMessage = new Message ('' , $ message ->getDeliveryTag (), '' , '' , '' , [], '' );
156
+ $ this ->channel ->ack ($ bunnyMessage );
175
157
}
176
158
177
159
/**
@@ -182,41 +164,8 @@ public function reject(PsrMessage $message, $requeue = false)
182
164
{
183
165
InvalidMessageException::assertMessageInstanceOf ($ message , InteropAmqpMessage::class);
184
166
185
- if (isset ($ this ->bunnyMessages [$ message ->getDeliveryTag ()])) {
186
- $ this ->channel ->reject ($ this ->bunnyMessages [$ message ->getDeliveryTag ()], $ requeue );
187
-
188
- unset($ this ->bunnyMessages [$ message ->getDeliveryTag ()]);
189
- }
190
- }
191
-
192
- /**
193
- * @param Message $bunnyMessage
194
- *
195
- * @return InteropAmqpMessage
196
- */
197
- private function convertMessage (Message $ bunnyMessage )
198
- {
199
- $ headers = $ bunnyMessage ->headers ;
200
-
201
- $ properties = [];
202
- if (isset ($ headers ['application_headers ' ])) {
203
- $ properties = $ headers ['application_headers ' ];
204
- }
205
- unset($ headers ['application_headers ' ]);
206
-
207
- if (array_key_exists ('timestamp ' , $ headers )) {
208
- /** @var \DateTime $date */
209
- $ date = $ headers ['timestamp ' ];
210
-
211
- $ headers ['timestamp ' ] = (int ) $ date ->format ('U ' );
212
- }
213
-
214
- $ message = new AmqpMessage ($ bunnyMessage ->content , $ properties , $ headers );
215
- $ message ->setDeliveryTag ($ bunnyMessage ->deliveryTag );
216
- $ message ->setRedelivered ($ bunnyMessage ->redelivered );
217
- $ message ->setRoutingKey ($ bunnyMessage ->routingKey );
218
-
219
- return $ message ;
167
+ $ bunnyMessage = new Message ('' , $ message ->getDeliveryTag (), '' , '' , '' , [], '' );
168
+ $ this ->channel ->reject ($ bunnyMessage , $ requeue );
220
169
}
221
170
222
171
/**
@@ -244,34 +193,12 @@ private function receiveBasicGet($timeout)
244
193
*/
245
194
private function receiveBasicConsume ($ timeout )
246
195
{
247
- if (false === $ this ->isInit ) {
248
- $ callback = function (Message $ message , Channel $ channel , Client $ bunny ) {
249
- $ receivedMessage = $ this ->convertMessage ($ message );
250
- $ receivedMessage ->setConsumerTag ($ message ->consumerTag );
251
-
252
- $ this ->bunnyMessages [$ message ->deliveryTag ] = $ message ;
253
- $ this ->buffer ->push ($ receivedMessage ->getConsumerTag (), $ receivedMessage );
254
-
255
- $ bunny ->stop ();
256
- };
257
-
258
- $ frame = $ this ->channel ->consume (
259
- $ callback ,
260
- $ this ->queue ->getQueueName (),
261
- $ this ->getConsumerTag () ?: $ this ->getQueue ()->getConsumerTag (),
262
- (bool ) ($ this ->getFlags () & InteropAmqpConsumer::FLAG_NOLOCAL ),
263
- (bool ) ($ this ->getFlags () & InteropAmqpConsumer::FLAG_NOACK ),
264
- (bool ) ($ this ->getFlags () & InteropAmqpConsumer::FLAG_EXCLUSIVE ),
265
- (bool ) ($ this ->getFlags () & InteropAmqpConsumer::FLAG_NOWAIT )
266
- );
267
-
268
- $ this ->consumerTag = $ frame ->consumerTag ;
269
-
270
- if (empty ($ this ->consumerTag )) {
271
- throw new Exception ('Got empty consumer tag ' );
272
- }
196
+ if (false == $ this ->consumerTag ) {
197
+ $ this ->context ->subscribe ($ this , function (InteropAmqpMessage $ message ) {
198
+ $ this ->buffer ->push ($ message ->getConsumerTag (), $ message );
273
199
274
- $ this ->isInit = true ;
200
+ return false ;
201
+ });
275
202
}
276
203
277
204
if ($ message = $ this ->buffer ->pop ($ this ->consumerTag )) {
@@ -281,7 +208,7 @@ private function receiveBasicConsume($timeout)
281
208
while (true ) {
282
209
$ start = microtime (true );
283
210
284
- $ this ->channel -> getClient ()-> run ( $ timeout / 1000 );
211
+ $ this ->context -> consume ( $ timeout );
285
212
286
213
if ($ message = $ this ->buffer ->pop ($ this ->consumerTag )) {
287
214
return $ message ;
0 commit comments