1111use Interop \Queue \Impl \ConsumerPollingTrait ;
1212use Interop \Queue \Message ;
1313use Interop \Queue \Queue ;
14- use Ramsey \Uuid \Uuid ;
1514
1615class DbalConsumer implements Consumer
1716{
@@ -49,16 +48,6 @@ public function __construct(DbalContext $context, DbalDestination $queue)
4948 $ this ->redeliveryDelay = 1200000 ;
5049 }
5150
52- public function getContext (): DbalContext
53- {
54- return $ this ->context ;
55- }
56-
57- public function getConnection (): Connection
58- {
59- return $ this ->dbal ;
60- }
61-
6251 /**
6352 * Get interval between retry failed messages in milliseconds.
6453 */
@@ -68,7 +57,7 @@ public function getRedeliveryDelay(): int
6857 }
6958
7059 /**
71- * Interval between retry failed messages in seconds .
60+ * Get interval between retrying failed messages in milliseconds .
7261 */
7362 public function setRedeliveryDelay (int $ redeliveryDelay ): self
7463 {
@@ -87,13 +76,12 @@ public function getQueue(): Queue
8776
8877 public function receiveNoWait (): ?Message
8978 {
90- $ deliveryId = (string ) Uuid ::uuid1 ();
9179 $ redeliveryDelay = $ this ->getRedeliveryDelay () / 1000 ; // milliseconds to seconds
9280
9381 $ this ->redeliverMessages ();
9482
9583 // get top message from the queue
96- if ($ message = $ this ->fetchMessage ([$ this ->queue ->getQueueName ()], $ deliveryId , $ redeliveryDelay )) {
84+ if ($ message = $ this ->fetchMessage ([$ this ->queue ->getQueueName ()], $ redeliveryDelay )) {
9785 if ($ message ['redelivered ' ] || empty ($ message ['time_to_live ' ]) || $ message ['time_to_live ' ] > time ()) {
9886 return $ this ->getContext ()->convertMessage ($ message );
9987 }
@@ -107,6 +95,8 @@ public function receiveNoWait(): ?Message
10795 */
10896 public function acknowledge (Message $ message ): void
10997 {
98+ InvalidMessageException ::assertMessageInstanceOf ($ message , DbalMessage::class);
99+
110100 $ this ->deleteMessage ($ message ->getDeliveryId ());
111101 }
112102
@@ -126,6 +116,16 @@ public function reject(Message $message, bool $requeue = false): void
126116 $ this ->deleteMessage ($ message ->getDeliveryId ());
127117 }
128118
119+ protected function getContext (): DbalContext
120+ {
121+ return $ this ->context ;
122+ }
123+
124+ protected function getConnection (): Connection
125+ {
126+ return $ this ->dbal ;
127+ }
128+
129129 private function deleteMessage (string $ deliveryId ): void
130130 {
131131 if (empty ($ deliveryId )) {
@@ -138,19 +138,4 @@ private function deleteMessage(string $deliveryId): void
138138 ['delivery_id ' => Type::STRING ]
139139 );
140140 }
141-
142- private function redeliverMessages (): void
143- {
144- $ this ->getConnection ()->createQueryBuilder ()
145- ->update ($ this ->getContext ()->getTableName ())
146- ->set ('delivery_id ' , ':deliveryId ' )
147- ->set ('redelivered ' , ':redelivered ' )
148- ->andWhere ('delivery_id IS NOT NULL ' )
149- ->andWhere ('redeliver_after < :now ' )
150- ->setParameter (':now ' , (int ) time (), Type::BIGINT )
151- ->setParameter ('deliveryId ' , null , Type::STRING )
152- ->setParameter ('redelivered ' , true , Type::BOOLEAN )
153- ->execute ()
154- ;
155- }
156141}
0 commit comments