@@ -33,20 +33,24 @@ class KafkaQueue extends Queue implements QueueContract
3333 */
3434 private $ producer ;
3535 /**
36- * @var \RdKafka\KafkaConsumer
36+ * @var \RdKafka\Consumer
3737 */
3838 private $ consumer ;
3939 /**
4040 * @var array
4141 */
42- private $ subscribedQueueNames = [];
42+ private $ topics = [];
43+ /**
44+ * @var array
45+ */
46+ private $ queues = [];
4347
4448 /**
4549 * @param \RdKafka\Producer $producer
4650 * @param \RdKafka\KafkaConsumer $consumer
4751 * @param array $config
4852 */
49- public function __construct (\RdKafka \Producer $ producer , \RdKafka \KafkaConsumer $ consumer , $ config )
53+ public function __construct (\RdKafka \Producer $ producer , \RdKafka \Consumer $ consumer , $ config )
5054 {
5155 $ this ->defaultQueue = $ config ['queue ' ];
5256 $ this ->sleepOnError = isset ($ config ['sleep_on_error ' ]) ? $ config ['sleep_on_error ' ] : 5 ;
@@ -140,12 +144,16 @@ public function pop($queue = null)
140144 {
141145 try {
142146 $ queue = $ this ->getQueueName ($ queue );
143- if (!in_array ($ queue , $ this ->subscribedQueueNames )) {
144- $ this ->subscribedQueueNames [] = $ queue ;
145- $ this ->consumer ->subscribe ($ this ->subscribedQueueNames );
147+ if (!array_key_exists ($ queue , $ this ->queues )) {
148+ $ this ->queues [$ queue ] = $ this ->consumer ->newQueue ();
149+ $ topicConf = new \RdKafka \TopicConf ();
150+ $ topicConf ->set ('auto.offset.reset ' , 'largest ' );
151+
152+ $ this ->topics [$ queue ] = $ this ->consumer ->newTopic ($ queue , $ topicConf );
153+ $ this ->topics [$ queue ]->consumeQueueStart (0 , RD_KAFKA_OFFSET_STORED , $ this ->queues [$ queue ]);
146154 }
147155
148- $ message = $ this ->consumer ->consume (1000 );
156+ $ message = $ this ->queues [ $ queue ] ->consume (1000 );
149157
150158 if ($ message === null ) {
151159 return null ;
@@ -155,7 +163,7 @@ public function pop($queue = null)
155163 case RD_KAFKA_RESP_ERR_NO_ERROR :
156164 return new KafkaJob (
157165 $ this ->container , $ this , $ message ,
158- $ this ->connectionName , $ queue ?: $ this ->defaultQueue
166+ $ this ->connectionName , $ queue ?: $ this ->defaultQueue , $ this -> topics [ $ queue ]
159167 );
160168 case RD_KAFKA_RESP_ERR__PARTITION_EOF :
161169 case RD_KAFKA_RESP_ERR__TIMED_OUT :
@@ -255,7 +263,7 @@ protected function reportConnectionError($action, Exception $e)
255263 }
256264
257265 /**
258- * @return \RdKafka\KafkaConsumer
266+ * @return \RdKafka\Consumer
259267 */
260268 public function getConsumer ()
261269 {
0 commit comments