@@ -45,13 +45,19 @@ class RdKafkaContext implements Context
45
45
*/
46
46
private $ kafkaConsumers ;
47
47
48
+ /**
49
+ * @var RdKafkaConsumer[]
50
+ */
51
+ private $ rdKafkaConsumers ;
52
+
48
53
/**
49
54
* @param array $config
50
55
*/
51
56
public function __construct (array $ config )
52
57
{
53
58
$ this ->config = $ config ;
54
59
$ this ->kafkaConsumers = [];
60
+ $ this ->rdKafkaConsumers = [];
55
61
56
62
$ this ->setSerializer (new JsonSerializer ());
57
63
}
@@ -102,26 +108,33 @@ public function createConsumer(Destination $destination): Consumer
102
108
{
103
109
InvalidDestinationException::assertDestinationInstanceOf ($ destination , RdKafkaTopic::class);
104
110
105
- $ this -> kafkaConsumers [] = $ kafkaConsumer = new KafkaConsumer ( $ this -> getConf () );
111
+ $ queueName = $ destination -> getQueueName ( );
106
112
107
- $ consumer = new RdKafkaConsumer (
108
- $ kafkaConsumer ,
109
- $ this ,
110
- $ destination ,
111
- $ this ->getSerializer ()
112
- );
113
+ if (!isset ($ this ->rdKafkaConsumers [$ queueName ])) {
114
+ $ this ->kafkaConsumers [] = $ kafkaConsumer = new KafkaConsumer ($ this ->getConf ());
115
+
116
+ $ consumer = new RdKafkaConsumer (
117
+ $ kafkaConsumer ,
118
+ $ this ,
119
+ $ destination ,
120
+ $ this ->getSerializer ()
121
+ );
122
+
123
+ if (isset ($ this ->config ['commit_async ' ])) {
124
+ $ consumer ->setCommitAsync ($ this ->config ['commit_async ' ]);
125
+ }
113
126
114
- if (isset ($ this ->config ['commit_async ' ])) {
115
- $ consumer ->setCommitAsync ($ this ->config ['commit_async ' ]);
127
+ $ this ->rdKafkaConsumers [$ queueName ] = $ consumer ;
116
128
}
117
129
118
- return $ consumer ;
130
+ return $ this -> rdKafkaConsumers [ $ queueName ] ;
119
131
}
120
132
121
133
public function close (): void
122
134
{
123
135
$ kafkaConsumers = $ this ->kafkaConsumers ;
124
136
$ this ->kafkaConsumers = [];
137
+ $ this ->rdKafkaConsumers = [];
125
138
126
139
foreach ($ kafkaConsumers as $ kafkaConsumer ) {
127
140
$ kafkaConsumer ->unsubscribe ();
0 commit comments